Important: Please read the Qt Code of Conduct -

When will a widget show up, after calling .show() or after entering its paint event?

  • Hi all, as stated in the title, I'm not quite clear about the difference between a paint event and
    the .show() method of widgets. As I see it, the widget window will show up after entering its
    paint event. Because in my recent coding, I found in the debug mode that after steping over
    .show() method, the widget stays hidden until it enters the paint event method.

    But I'm not quite sure whether I'm right, or the project I encountered is just a special (or different) case.
    Could anyone please give some clarifications?

  • Lifetime Qt Champion


    Show will set the Widgets state to visible and
    update() that will post a paint event to the event queue.

    When you are debugging (stepping), you are stopping the loop so here its clear
    to see that show() does not make it be on screen. The paint will.

    So paint event is the actual function that shows something on the screen.

  • @mrjj
    It makes sense now!

  • Lifetime Qt Champion

    This is also why looong
    for loops in code will make the app paint slow/ not at all.

  • @mrjj
    could you please elaborate a bit on your statement?
    Sorry but I'm quite new in this Qt world.

  • Lifetime Qt Champion

    Oh yes. ofc.
    Qt is what called event driven , like most desktop programs are.

    If we look at a normal main

    int mainx(int argc, char* argv[]) {
    QApplication a(argc, argv);
    MainWindow w;;
    return a.exec(); <<<<< this is the event loop. programs is mostly in here.

    So the event loop is running in the background and is used for sending signals / events from widgets to widgets.

    so if you have a loop in your code

    for (int var = 0; var < VERY_BIG_NUMBER; ++var) {
    do heavy calculations

    You will not allow the loop ( a.exec() ) to get any work done and
    hence the application will appear to be hanging / not responding etc.
    Just like when you pause it with debugger.

Log in to reply