Results 1 to 15 of 15

Thread: Multithreading on window constructors

  1. #1
    Join Date
    Oct 2017
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Multithreading on window constructors

    Hi All,
    I am developing an Widgetapp, Which has MainMenuWindow(480*800 pix) with QStackedwidget(480*500 pix) in it. I have added all my other windows(around 10 each with multiple widget's inside them) to stacked widget. The reason for this approach being, The top and bottom menu options are common across all windows, which I added to MainMenuWindow, based on Menu selected I am changing stacked widget index to show the corresponding window.
    But the problem with this approach is, On bootup of the system, I am calling MainMenuWindow constructor which calls all other window constructors inside it. But all this process taking up around 1-2 Minutes, Which is causing my app fall outside acceptable bootup delay range of 15-30 secs.
    I am thinking of initializing threads for each window constructor to run them in parallel, and wait for all threads to finish after constructor is done. But not sure how feasible this solution would be.
    Any help in this prospect of how to do this in a graceful way or any better idea to solve this unwanted delay?

    Thanks,
    Nagendra.
    Last edited by nagendrababu; 3rd July 2018 at 03:38.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading on window constructors

    You have to tell what takes so much time. Only then can you wonder how to solve the problem.

  3. #3
    Join Date
    Oct 2017
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multithreading on window constructors

    Hi @Lesiok,
    it's window constructors taking that much time. As I mentioned previously,I added all my windows to stacked widget of MainMenuWindow(My main window). So When I Invoke MainMenuWindow constructor, constructors of all other windows which to be added to stacked widget are also called there, and added to stacked widget So all the time is to finish the MainMenuWindow constructor alone.

    -Nagendra

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,133
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multithreading on window constructors

    Widget constructors take almost no time to execute. I have apps with many tens of widgets that start up almost instantly. You are probably doing something wrong. But since you haven't shown any code at all, it's impossible to tell you what that might be.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Oct 2017
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multithreading on window constructors

    Hi @d_stranz,
    I was also in impression that constructors don't take much time, But In my current application I am seeing it. So to give you clear Idea I have profiled time taken by each window constructor alone.

    // Time taken by each screen constructor in ms
    Elapsed time Screen1 1609
    Elapsed time Screen2 0
    Elapsed time Screen3 343
    Elapsed time Screen4 2178
    Elapsed time Screen5 44
    Elapsed time Screen6 2916
    Elapsed time Screen7 179
    Elapsed time Screen8 563
    Elapsed time Screen9 26077
    Elapsed time Screen10 1790
    TOTAL: 35699ms

    // Time to add all these objects to mainMenuWindow stacked widget
    Total Stacked widgets: 10
    Elapsed time to stack all widgets 15761


    My MainMenuWindow constructor is as follows:
    Qt Code:
    1. LoginScreen = new LoginWindow(this);
    2. welcomewindow = new Welcomewindow(this);
    3. AutoControlScreen = new AutoControl(this);
    4. AdjScreen = new Adjustment(this);
    5. HeatScreen = new Heat(this);
    6. InfoScreen = new Information(this);
    7. IntensityScreen = new Intensity(this);
    8. ManualScreen = new Manualcontrol(this);
    9. SettingsScreen = new Settings(this);
    10. UsersScreen = new UsersMemory(this);
    11.  
    12. #ifdef DEBUG
    13. if(welcomewindow == NULL)
    14. qDebug() << "Dummy Welcome window Object is Null \n";
    15. if(LoginScreen == NULL)
    16. qDebug() << "Login screen object is Null \n";
    17. if(AutoControlScreen == NULL)
    18. qDebug() << "AutoControl screen object is Null \n";
    19. if(AdjScreen == NULL)
    20. qDebug() << "Adj screen object is Null \n";
    21. if(HeatScreen == NULL)
    22. qDebug() << "HeatScreen object is Null \n";
    23. if(InfoScreen == NULL)
    24. qDebug() << "InfoScreen object is Null \n";
    25. if(IntensityScreen == NULL)
    26. qDebug() << "IntensityScreen object is Null \n";
    27. if(ManualScreen == NULL)
    28. qDebug() << "ManualScreen object is Null \n";
    29. if(SettingsScreen == NULL)
    30. qDebug() << "SettingsScreen object is Null \n";
    31. if(UsersScreen == NULL)
    32. qDebug() << "UsersScreen object is Null \n";
    33. #endif
    34. if(welcomewindow == NULL || LoginScreen == NULL || AutoControlScreen == NULL || AdjScreen == NULL || \
    35. HeatScreen == NULL || InfoScreen == NULL || IntensityScreen == NULL || ManualScreen == NULL || \
    36. SettingsScreen == NULL || UsersScreen == NULL)
    37. {
    38. qDebug() << "Windows creation Failed, Exiting the Application\n";
    39. exit(1);
    40. }
    41. //*******************************************************************
    42. // Setting All Mainscreen as stackable objects to MainMenu Screens
    43. //*******************************************************************
    44.  
    45. ui->stackedWidget->insertWidget(S_Welcome,welcomewindow);
    46. ui->stackedWidget->insertWidget(S_Auto,AutoControlScreen);
    47. ui->stackedWidget->insertWidget(S_Manual,ManualScreen);
    48. ui->stackedWidget->insertWidget(S_Settings,SettingsScreen);
    49. ui->stackedWidget->insertWidget(S_Info,InfoScreen);
    50. ui->stackedWidget->insertWidget(S_Heat,HeatScreen);
    51. ui->stackedWidget->insertWidget(S_Adj,AdjScreen);
    52. ui->stackedWidget->insertWidget(S_Intensity,IntensityScreen);
    53. ui->stackedWidget->insertWidget(S_Users,UsersScreen);
    54. qDebug() << "Total Stacked widgets: " << ui->stackedWidget->count();
    To copy to clipboard, switch view to plain text mode 

    Please Suggest how can I improve the timeline.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading on window constructors

    Show Settings() constructor - if this is screen9.

  7. #7
    Join Date
    Oct 2017
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multithreading on window constructors

    Hi @Lesiok,
    this is my settings page constructor. This screen is having around a total of 100 widgets arranged as pages.

    Qt Code:
    1. Settings::Settings(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::Settings)
    4. {
    5. this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    6. PasswordScreenPage = new PasswordScreen(this);
    7. ui->setupUi(this);
    8. this->setCursor(Qt::BlankCursor);
    9. ui->stackedWidget->insertWidget(PasswordChangePage,PasswordScreenPage);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Nagendra.

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading on window constructors

    Are You running debug or release code ? The debug code contains many asserts, which makes it slow, especially container operations.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,133
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multithreading on window constructors

    Qt Code:
    1. Settings::Settings(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::Settings)
    To copy to clipboard, switch view to plain text mode 

    Why are you calling the QMainWindow constructor from the Settings constructor? Does this widget really inherit from QMainWindow? And if so, why are you adding something based on QMainWindow into a stacked widget which is itself inside a QMainWindow?
    Last edited by d_stranz; 5th July 2018 at 21:18.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #10
    Join Date
    Oct 2017
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multithreading on window constructors

    Hi @d_stranz,
    Yes all these are inherited from QMainWindow. The evolution of this application is, Initially started with creating individual screens of full screen dimension(800*480) But when changing between screens there is visible transition which looks like application running very slow. So then We have changed all windows to dimension of stacked widget(500*480) and added them to stacked widget of MainMenuWindow. From the qt forum posts I read, I understand this can be done so we went ahead with that approach instead of rewriting entire application.

    Do you think this approach is causing this much delay? If that's the case from which template these screens should be inherited. QDialog or QWidget?

    Thanks,
    Nagendra.

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,133
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multithreading on window constructors

    That must be a very confusing GUI for users. You have the outer QMainWindow (that contains the stacked widget) and it has a menu. toolbar, and status bar, then inside that you have -more- QMainWindow classes, and each of them has a menu bar, toolbar, and status bar. Is that really what it looks like?

    A typical application has a window hierarchy like this:

    Qt Code:
    1. QWidget (central widget) - in your case this is QStackedWidget
    2. QWidget(s) - one for each page in the stack, including layouts
    To copy to clipboard, switch view to plain text mode 
    Your application must look like this:

    Qt Code:
    1. QStackedWidget (as central widget)
    2. and what as the central widget?
    3. and a different central widget?
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Sounds very strange to me. I would replace all of the QMainWindow instance you use in the stacked widget with plain QWidget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  12. #12
    Join Date
    Oct 2017
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multithreading on window constructors

    Hi d_stranz,
    I agree It might not be a good way, But as I told previously The evolution of app made us to take this kind of decision. And coming to UI, it don't has menubar, toolbar or status bar, So it looks cleaner and easy.
    Qt Code:
    1. centralwidget
    2. all widgets under this screen
    3. centralwidget
    4. all widgets under this screen
    To copy to clipboard, switch view to plain text mode 

    I agree with the change, that All these windows should be Qwidgets, But it's like rewriting all my application again.
    So do you think the constructors delay is caused by my approach only?

    Thanks,
    Nagendra

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,133
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multithreading on window constructors

    So do you think the constructors delay is caused by my approach only?
    I can't see any other reason for it, but then you haven't showed much code. If your sub-main windows don't have menus or toolbars, then it makes no sense at all to do it the way you are doing it. It's a lot of overhead for absolutely nothing. You could just simply add your central widgets to the stacked widget and achieve the same thing.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #14
    Join Date
    Oct 2017
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multithreading on window constructors

    Hi @d_stranz,
    I tried creating one of the window as Qwidget type instead of QMainWindow type and calculated time to execute the constructor in each case.
    In case of QMainWinow:
    Elapsed time Screen1: 1600ms
    In case of QWidget:
    Elapsed time Screen1: 1589ms

    So from these metrics, I don't see much difference in time taken by constructor.

    To be additional Question,
    Is there any way we can implement threads to divide constructors between them to run them in parallel.

    I tried something like this:
    Qt Code:
    1. // Thread1 to init screens except Settings
    2. Thread1 = new QThread;
    3. connect(Thread1, SIGNAL(started()), this, SLOT(thread1_run()),Qt::DirectConnection);
    4. connect(this, SIGNAL(thread1Finished()), this, SLOT(thread1_finish()));
    5. // Thread2 to init Settings screen(Heavy Screen)
    6. Thread2 = new QThread;
    7. connect(Thread2, SIGNAL(started()), this, SLOT(thread2_run()),Qt::DirectConnection);
    8. connect(this, SIGNAL(thread2Finished()), this, SLOT(thread2_finish()));
    9. // Stack widgets
    10. connect(this,SIGNAL(threadsDone()),this,SLOT(stackWidgets()));
    11. Thread1->start(QThread::HighestPriority);
    12. Thread2->start(QThread::HighestPriority);
    To copy to clipboard, switch view to plain text mode 
    But from Above code, The threads are executing one after another. They aren't running parallel. Can you help how to run these parallel?

    Thanks,
    Nagendra

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,133
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multithreading on window constructors

    Can you help how to run these parallel?
    Not possible in Qt. GUI objects (things inheriting from QWidget or QWindow) can be created only in the thread that owns QApplication, and you can have only a single QApplication instance.

    You need to do an experiment, I think. Create a new QMainWindow-based application, put a QStackedWidget as the central widget for the main window, and then just add a bunch of QWidget-based pages to them. Don't use your existing classes. Use QTreeView, QTableView, QGraphicsView, whatever. Time how long that takes. I think you'll find that your bottleneck is not in building your GUI, it's whatever you are doing in your application's sub-window constructors that's the cause.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Some weird constructors
    By highnergy in forum Qt Programming
    Replies: 7
    Last Post: 23rd December 2012, 08:52
  2. Qwt 6.0.0 and removal of copy constructors.
    By sdhengsoft in forum Qwt
    Replies: 4
    Last Post: 21st April 2011, 04:33
  3. Using template in constructors
    By estanisgeyer in forum General Programming
    Replies: 4
    Last Post: 18th November 2008, 18:41
  4. constructors, new, return
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 29th September 2006, 14:43
  5. copying and assignment constructors
    By TheKedge in forum General Programming
    Replies: 3
    Last Post: 17th August 2006, 15:09

Tags for this Thread

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.