QT Repaint/Redraw/Update/Do Something

前端 未结 4 1713
清酒与你
清酒与你 2020-12-10 00:59

I\'m New to QT. I understand that you can force a display refresh, but I\'ve pulled all my hair out trying to figure out how. Here is what I\'m specifically trying to do.

相关标签:
4条回答
  • 2020-12-10 01:41

    I was using sleep to emulate a brief amount of time the computer was waiting for something to happen.

    As I stated in my question, I didn't want to use events because it's a whole lot of unnecessary work to accomplish something extremely simply.

    Also, the 'event' that needs to take place for the program to continue, is a USB event. Since I'm using an HID class device, there is no way to set an event to happen without a wait loop. USB HID classes don't permit setting interrupts, the OS claims the device.

    I managed to get the above to work. I walked through the debugger and noticed the display would refresh before the sleep function. Running the program independently, I got random results with the display refreshing 1% of the time. I got rid of the sleep function, and added some other code in it's place to emulate a delay, and it was fine.

    Just for everyone's knowledge, this is possible, it's not forbidden, and it's easy to do with the following:

    qApp->processEvents();
    

    qApp is a global external variable in the QApplication header.

    Because this USB event is making my flow tricky, I stumbled upon the QWaitCondition Class. I was going to start a process waiting for the USB event. I would wait until the process releases the wait condition for my routine to continue.

    But if anyone thinks this is a bad idea, please, speak out. I really do appreciate your feedback PiedPiper and Hostile Fork.

    Thank you.

    0 讨论(0)
  • 2020-12-10 01:43

    If i understood correctly, you have a slot and in this slot, you update the image shown in a QLabel. But you want this change to be displayed before the slot finishes.

    If that is the case, issue an update() event, and call qApp->processEvents(). This method processes events that are waiting in the event queue and then returns, therefore this may be what you are after.

    PS: an update() may not be necessary at all, i am not sure.

    0 讨论(0)
  • 2020-12-10 01:44

    You shouldn't be waiting for input in your event handler. You need to rethink the logic of your program to use events the way they were intended. All the update() and repaint() calls in your code are unnecessary if you return to the event loop.

    0 讨论(0)
  • 2020-12-10 01:45

    I noticed sometimes when you have multiple layered widgets, or widgets inside of widgets it helps to call their repaint() events.

    For example

    this->repaint();
    this->parentWidget()->repaint();
    this->parentWidget()->parentWidget()->repaint();
    

    This is far easier then pushing out any processing to another Thread, or creating additional event handlers.

    0 讨论(0)
提交回复
热议问题