简体   繁体   中英

QPaintEvent painting region in Qt?

This is a basic doubt regarding the Painting done in Qt. I have a QScrollArea as my centralWidget in Main Window of my Application. I have added a QFrame frame to the scrollarea . The Layout of the QFrame is QGridLayout . When I add widgets to the layout like this:

MainWindow::AddLabel()
{
    setUpdatesEnabled(false);
    QGridLayout *myGrid = (QGridLayout *)ui->frame->layout();
    for(int i = 0; i < 1000; i++)
    {
        QLabel *label = new QLabel();
        QString str;
        str.SetNum(i);
        label->SetText(str);
        myGrid->AddWidget(label, 0, i, 0);//add label to i'th column of row 0
    }
    setUpdatesEnabled(true);
    repaint();
}

Please dont worry about the memory leak as it is not the focus of the question. So my doubt's are:

  1. Is setting the updates disabled during adding widgets to layout any helpful?

  2. Even if I maximise the window not all the QLabel's will be visible to me. So when the code flow leaves the above function & goes to the event loop then are all the QLabel's & the enormous area of QFrame painted? Or only those QLabel's which are visible & only that much area of QFrame which is visible painted?

If you are using a form (.ui) then the widgets inside the ui are not children of your widget MainWindow . Well , setUpdatesEnabled() only affect the current widget as well as its children, so the object ui->frame will still receive updates after myGrid->AddWidget . Change to

  ui->frame->setUpdatesEnabled(false);
  ...
  ui->frame->setUpdatesEnabled(true);

Btw, when you enable updates, then screen will be updated. So you dont need to call repaint(); on any widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM