Results 1 to 15 of 15

Thread: Multithreading on window constructors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 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.

  2. #2
    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.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 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.

  4. #4
    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

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 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.

  6. #6
    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

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 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
  •  
Qt is a trademark of The Qt Company.