Results 1 to 11 of 11

Thread: How to write a processevents loop until mainwindow become visible ?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to write a processevents loop until mainwindow become visible ?

    I need to write a loop which mission is to loop until the mainwindow was visible.
    Now I have :

    Qt Code:
    1. do
    2. {QApplication::processEvents(QEventLoop::AllEvents);}
    3. while (MY_qtutil().w_tell_me_the_mainwindow()->isVisible()==false);
    To copy to clipboard, switch view to plain text mode 
    MY_qtutil().w_tell_me_the_mainwindow() returns the Qmainwindow from any part of my app (it is verified)

    But allways I have visible == false....
    What's happen ? processEvents does not let to the mainwindow visualization process was executed ?

    I need to do this not from the mainwindow (That is I cant use the showevent method), but at custom setup for custom widgets.

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    You could implement a showEvent handler for your main window, and in that handler, emit a signal: "mainWindowVisible()". You can connect to this signal somewhere else in your code that constructs the custom widgets.

    You don't need your processEvents loop at all.

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    Yes, I know this way.
    But I want to make my custom widget showevent independent.
    In other words, how can I do to do a effective loop until the mainwindow was visible ?
    Thanks for your time.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    You did not say where you put your processEvents loop, so it is impossible to give any advice about why it doesn't work.

    But once again, you keep trying to defeat Qt. The usual Qt way to know when something has occurred is to either implement an event handler (in the widget itself) or to have the widget send a signal that can be connected to something else that needs to know. If you have to implement a processEvents loop to continuously watch for some change, then you really aren't letting Qt do what it does best. Let Qt tell you when the window has become visible, don't keep asking and asking and asking in your loop. If you have a method to retrieve the main window pointer from anywhere, then you also have the ability to connect to a main window signal from anywhere.

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    The processEvents loop which I want to write is located at a custom setup function inside my custom widget.

    I have at any place (It can be a designer form, another widget, created by code, etc.)
    Qt Code:
    1. My_custom_widget = new Custom_widget(this);
    To copy to clipboard, switch view to plain text mode 
    Inside it (my custom constructor), I need to setup some properties that depends on the mainwindow was visible. I need to access to some properties and explore the widgets contained, etc... I need to do these things after the mainwindow was visible and before my custom widged was visible.

    And I dont want to depend on write emit signal for every widget where I' m going to use this custom widgets. This is the reason because I need to write this loop. (Imagine I give this custom widget to some friend and I have to tell him 'Hey, remember to create I_am_visible signal for every widget where you plan to use the custom widget ... ).

    Thanks again d_stranz

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    OK, if you insist that this design is the only way to implement what you want, I don't know the answer to your question. Sorry.

  7. #7
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    Thanks and excuse me If I'm persistent....
    The main question I have is 'processevents all events does not work to let the mainwindow was showed ?

  8. #8
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    Re-implement the showEvent of your custom widget.
    According to the documentation there is an internal event that is generated just before your widget becomes visible.
    I think that at that time the mainwindow or any other widget containing your custom widget is already visible, so you can start exploring their properties.

    Regards,
    Marc

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    The main question I have is 'processevents all events does not work to let the mainwindow was showed ?
    It is impossible to answer your question without more details. When is this loop running? After the main window is created but before it is shown? After it is shown? Before the main app event loop is even running? Is the main window actually visible when you are inside this loop? Are you certain that the QWidget pointer that is being returned by your MY_qtutil().w_tell_me_the_mainwindow() method *really is* the main window and not some child or frame? Do you even know if the loop is executing? (Did you set a breakpoint inside it with the debugger?)

    Simply posting a small piece of code and asking "why doesn't this work?" doesn't give us many clues to help you.

  10. #10
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    I have a simple and typical example :

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. w.show();
    6. return a.exec();
    7. }
    8. .....
    9. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. new_panel= new A_panel(this);
    13. new_panel->w_setup();
    14. }
    To copy to clipboard, switch view to plain text mode 

    - The A_panel w_setup is the method which have to do things when Qmainwindow was visible.
    - The MY_qtutil().w_tell_me_the_mainwindow() returns the QMainwindow (I'm sure).
    - I think that the problem can be related with I cant do processevents until a.exec() has reached . (I have read something related)

    Now I'm doing a test using a Qtimer with an intervla of 1 ms. After 16 ms. I have the QmainWindow already visible .
    What is your opinion ? Thanks

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to write a processevents loop until mainwindow become visible ?

    The QAppliccation main event loop does not start until line 6 (a.exec()), so inside your MainWindow constructor there is no event loop yet. In addition, MainWindow "w" is actually not visible when you call w.show() in line 5, but only become visibles when the event loop starts in line 6.

    So basically, nothing happens in A_panel::setup() because 1) there is no event loop and 2) your MainWindow constructor has never exited (you jumped out of it to call A_panel::setup() and your processEvents loop never exits), so you will never get to lines 5 or 6.

    The way you use a QTimer is not good, because the timing will depend on the PC and on the load of other processes running at the time. So maybe 16ms is OK one time, but not good another time. You can use QTimer a different way.

    Implement a slot for your MainWindow class:

    Qt Code:
    1. // .h
    2. class MainWIndow //...
    3. {
    4. protected slots:
    5. void onStart();
    6.  
    7. //...
    8. };
    9.  
    10. // .cpp
    11.  
    12. MainWindow::MainWindow(QWidget *parent)
    13. : QMainWindow(parent)
    14. , ui(new Ui::MainWindow)
    15. {
    16. ui->setupUi(this);
    17.  
    18. QTimer::singleShot( 0, this, SLOT( onStart() );
    19. }
    20.  
    21.  
    22. void MainWindow::onStart()
    23. {
    24. new_panel= new A_panel(this);
    25. new_panel->w_setup();
    26. }
    To copy to clipboard, switch view to plain text mode 

    The timer will be started with a zero timeout just before the MainWindow constructor exits. However, because the event loop isn't running yet, the timeout() signal can't be processed until the event loop starts. After you call a.exec(), the event loop is then running and the timeout signal will get queued up. The MainWindow isn't necessarily visible in onStart(), but I think the event loop in w_setup() should probably work then and eventually the MainWindow will become visible.
    Last edited by d_stranz; 7th December 2011 at 21:24.

  12. The following user says thank you to d_stranz for this useful post:

    tonnot (8th December 2011)

Similar Threads

  1. Replies: 0
    Last Post: 6th November 2011, 09:22
  2. Replies: 4
    Last Post: 6th August 2011, 01:40
  3. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  4. Replies: 2
    Last Post: 2nd November 2010, 05:15
  5. Replies: 2
    Last Post: 19th August 2008, 09:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.