Results 1 to 7 of 7

Thread: Widget creation slows GUI thread

  1. #1
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Question Widget creation slows GUI thread

    Hi folks,

    I am developing an application using Qt/embedded 4.8.

    At runtime, depending on various events, widgets of type MainView (see simplified example code below) needs to be created and displayed in a tab view.
    The situation I am now facing is, that the creation of an instance for MainView lasts approx. 1 second.
    Because widgets may only be created in the GUI thread, this causes the thread to 'hang' for this second and the GUI is not responsive.

    Qt Code:
    1. class MainView : public QWidget {
    2. MainView() {
    3. a = new MyWidgetA(); b = new MyWidgetB(); c = new MyKeyboard();
    4. layout.add(a); layout.add(b); layout.add(c);
    5. setLayout(&layout);
    6. }
    7. MyWidgetA* a;
    8. MyWidgetB* b;
    9. MyKeyboard* c;
    10. };
    To copy to clipboard, switch view to plain text mode 

    The widgets which are created are e.g. a virtual keyboard which again consists of approx. 120 child widgets of type QPushButton....

    Does anyone know a 'typical' pattern to avoid this problem? Or are there any other solutions - maybe 'speed-up widget creation'?

    Thanks in advance, mike

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Widget creation slows GUI thread

    Have you measured which of the widgets is causing this?
    Or are all three equally "bad" (each needing 330ms)?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Widget creation slows GUI thread

    I have not yet measured each widget separately...

    When the keyboard is populated with the buttons 3 pages in a stacked layout, on each page 4 lines
    with each 10 keys on it; for every key a global style is used to set the background image.

    Can generally be said that setStyleSheet() is an expensive operation on widgets?

    thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Widget creation slows GUI thread

    Quote Originally Posted by MrGentleman View Post
    I have not yet measured each widget separately...
    You need to find out where you are losing the time, otherwise you spend optizing things that have had no impact anyway.

    Quote Originally Posted by MrGentleman View Post
    Can generally be said that setStyleSheet() is an expensive operation on widgets?
    Likely depends on the complexity, but I haven't heard about it being a problem so far.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Widget creation slows GUI thread

    Hi,

    I have now measured the time required to create my pages.

    MyWidgetA: 15316950 ns
    MyWidgetB: 16899540 ns
    MyWidgetC: 14246580 ns
    MyWidgetD: 12074940 ns
    MyWidgetE: 202042562 ns
    MyWidgetF: 684948037 ns <- This widget contains the virtual keyboard
    MyWidgetG: 7859490 ns
    MyWidgetH: 7589730 ns


    All these widgets are put into the QStackedLayout to display a single entity (which are created at run time)
    When all these times are summed up, I get a delay of approx. nearly one second where the GUI is not responsive.
    It is also remarkable, that the creation of the widget which contains the VK is approx. 4 times longer compared to the other ones..

    Thanks and kind regards!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Widget creation slows GUI thread

    You try a delayed creation approach.
    E.g. only create the stacked widget pages when the tab is first activated.

    Cheers,
    _

  7. #7
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Widget creation slows GUI thread

    Thanks for the suggestion!

    I have now implemented a mechanism which creates the MainView instances strictly sequential.

    A background thread listens for events when to create main views. It then emits a signal to create the view, the GUI thread
    listens for this signal and creates the view. While the GUI thread is creating the view, the thread waits until the GUI thread
    notifies it via a signal that the main view has been created. Only then a new create request can be emitted.

    This does actually not solve the problem that the creation of widgets takes much time, but all in all the GUI remains much
    more responsive than creating all views at one time.

    Thanks, best regards!

Similar Threads

  1. Dynamic creation of widget with N images
    By davethomaspilot in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2014, 12:45
  2. SSL support slows the start of the app
    By dmitry64 in forum Qt Programming
    Replies: 2
    Last Post: 10th June 2013, 12:47
  3. Qt4.7.0 Phonon module slows down my app
    By Apsta in forum Qt Programming
    Replies: 2
    Last Post: 20th April 2012, 13:06
  4. Replies: 3
    Last Post: 18th July 2010, 13:53
  5. Dynamic widget creation from an XML-like file
    By ttvo in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 22:15

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.