admin管理员组

文章数量:1122846

void fonttiplineEdit::paintEvent(QPaintEvent *event)
{
    QLineEdit::paintEvent(event);

    QPainter painter(this);
    qDebug() << "event->rect().left():" << event->rect().left()
             << " event->rect().right():" << event->rect().right()
             << " event->rect().top():" << event->rect().top()
             << " event->rect().bottom():" << event->rect().bottom()
             << " event->rect().x():" << event->rect().x()
             << " event->rect().y():" << event->rect().y();

    QRectF textRect = event->rect().adjusted(11, 0, 0, 0);;
    QString text = m_text;

    QFont font("Microsoft YaHei"); // 设置字体
    font.setPixelSize(16);
    QPen pen(QColor("#3F3E3E")); // 设置画笔颜色

    painter.setFont(font);
    painter.setPen(pen);

    painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
}

I override a paintevent of Qlinedit, so that some texts can be displayed on the left side

Then I created a QLabel, used setMovie to place a gif, and set the background transparent, placing it on top of lineedit

void bindinfoWindow::setstyle()
{
    phonelineEdit->setmtext(zh("手机号"));
    dingtalklineEdit->setmtext(zh("钉钉"));

    m_gifWaitWidget->setFixedSize(ui->mycentralwidget->size());
    m_gifWaitWidget->raise();
    m_gifWaitWidget->hide();
    m_movie = new QMovie(":/VPNClientRes/1920/gif/wait1.gif", QByteArray(), this);
    m_gifWaitLabel->setMovie(m_movie);
}

Something strange happened, the text in the override paintEvent was incorrectly displayed in the GIF

It is found through debug that GIF calls Paintevent

This is normal Paintevent

event->rect().left(): 0  event->rect().right(): 302  event->rect().top(): 0  event->rect().bottom(): 35  event->rect().x(): 0  event->rect().y(): 0
event->rect().left(): 0  event->rect().right(): 302  event->rect().top(): 0  event->rect().bottom(): 35  event->rect().x(): 0  event->rect().y(): 0

This is the gif call

event->rect().left(): 178  event->rect().right(): 209  event->rect().top(): 32  event->rect().bottom(): 35  event->rect().x(): 178  event->rect().y(): 32
event->rect().left(): 178  event->rect().right(): 209  event->rect().top(): 0  event->rect().bottom(): 17  event->rect().x(): 178  event->rect().y(): 0

My temporary workaround is to add a line to block the gif call

void fonttiplineEdit::paintEvent(QPaintEvent *event)
{
    QLineEdit::paintEvent(event);

    QPainter painter(this);

    if(event->rect().left() != 0) return; //block gifcall

    QRectF textRect = event->rect().adjusted(11, 0, 0, 0);;
    QString text = m_text;

    QFont font("Microsoft YaHei"); // 设置字体
    font.setPixelSize(16);
    QPen pen(QColor("#3F3E3E")); // 设置画笔颜色

    painter.setFont(font);
    painter.setPen(pen);

    painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
}

But I don't know the fundamental reason. The QT version I am using is 5.15.2

本文标签: cAfter override painteventQMovie39s transparent background bugStack Overflow