Results 1 to 7 of 7

Thread: Lost widget

  1. #1
    Join Date
    Jul 2006
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Lost widget

    This is code fragment:

    Qt Code:
    1. ExcitThread eThread(this, eInfo);
    2. eThread.start(QThread::LowestPriority);
    3.  
    4. QWidget* widget = new QWidget(this);
    5. l->addWidget(new QLabel("Working..."));
    6. widget->setLayout(l);
    7. widget->show();
    8.  
    9. while(eThread.isRunning())
    10. usleep(100);
    11.  
    12. widget->close();
    13.  
    14. if(eThread.isOk())
    15. ...
    To copy to clipboard, switch view to plain text mode 

    The thing is I don't see "Working..." message. The (non GUI) ExcitThread does it's work fine.
    Where is "Working..."?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Lost widget

    "Working" is waiting in event queue until you change
    Qt Code:
    1. while(eThread.isRunning())
    2. usleep(100);
    To copy to clipboard, switch view to plain text mode 
    to something more appropriate (like using a timer's signal connected to a slot or QApplication::processEvents), because you are blocking your application in this loop until the thread exists and show() doesn't act immediately but instead posts an event to the event queue.

  3. #3
    Join Date
    Jul 2006
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Lost widget

    Quote Originally Posted by wysota
    "Working" is waiting in event queue until you change
    Qt Code:
    1. while(eThread.isRunning())
    2. usleep(100);
    To copy to clipboard, switch view to plain text mode 
    to something more appropriate (like using a timer's signal connected to a slot or QApplication:rocessEvents), because you are blocking your application in this loop until the thread exists and show() doesn't act immediately but instead posts an event to the event queue.
    Frankly speaking, I don't understand the answer completely :-)

    At any case I have tried to add to new-thread-initiator few slots (showStatus(), showCritical() and such), and emited appropriate signals inside QThread::run() implementation. It works.

    BTW, what is the best place to call connect() for such case? I have done it inside QThread descendant constructor. Last one has QWidget* with mentioned slots as a parameter.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Lost widget

    Why are you using a separate thread here if the main thread doesn't do anything but wait for the other thread?

  5. #5
    Join Date
    Jul 2006
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Lost widget

    Quote Originally Posted by wysota
    Why are you using a separate thread here if the main thread doesn't do anything but wait for the other thread?
    Now, after separate thread starting, main thread is ready to work with a user, and status bar shows something like "I'm doing this task...". Besides, background thread work can take noticeable period with different result opportunities: OK (for status bar) or critical error (modal dialog). All cases are covered now.

    I think, my case is very common case in UI coding. Isn't it?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Lost widget

    Qt Code:
    1. while(eThread.isRunning())
    2. usleep(100);
    To copy to clipboard, switch view to plain text mode 

    But what is this for? It stops the main thread. And if so, why do you need another thread? You can move its functionality to the main thread and the result will be identical. Better yet, you can split the work you do in the other thread into smaller chunks and use a QTimer with 0 interval to trigger next steps of the process and you'll gain this, that you won't block the event handling and your gui will remain responsive.

    I think, my case is very common case in UI coding. Isn't it?
    "Common" doesn't mean "proper". You can achieve the same without using a separate thread which only slows down the process.

  7. The following user says thank you to wysota for this useful post:

    anli (29th July 2006)

  8. #7
    Join Date
    Jul 2006
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Lost widget

    Quote Originally Posted by wysota
    Qt Code:
    1. while(eThread.isRunning())
    2. usleep(100);
    To copy to clipboard, switch view to plain text mode 

    But what is this for? It stops the main thread. And if so, why do you need another thread? You can move its functionality to the main thread and the result will be identical. Better yet, you can split the work you do in the other thread into smaller chunks and use a QTimer with 0 interval to trigger next steps of the process and you'll gain this, that you won't block the event handling and your gui will remain responsive.


    "Common" doesn't mean "proper". You can achieve the same without using a separate thread which only slows down the process.
    I have rewrote the code. My previous msg is in accordans with these fragments:

    Starting a thread:

    Qt Code:
    1. ExcitThread* eThread = new ExcitThread(this, eInfo, this);
    2. eThread->start(QThread::LowestPriority);
    To copy to clipboard, switch view to plain text mode 

    QThread implementation:

    Qt Code:
    1. ExcitThread::ExcitThread(QObject* parent, const ExcitInfo& anInfo, QWidget* aFeedback) : QThread(parent) {
    2. this->info = anInfo;
    3. this->feedback = aFeedback;
    4. connect(this, SIGNAL(showStatus(const QString&)), this->feedback, SLOT(showStatus(const QString&)));
    5. connect(this, SIGNAL(showStatus(const QString&, int)), this->feedback, SLOT(showStatus(const QString&, int)));
    6. connect(this, SIGNAL(showCritical(const QString&)), this->feedback, SLOT(showCritical(const QString&)));
    7. }
    8.  
    9. void ExcitThread::run() {
    10. emit showStatus("Generating excitation...");
    11. try {
    12. Excitation::generate( this->info.workDir.toStdString(),
    13. this->info.duration,
    14. this->info.sampleRate,
    15. this->info.bitDepth,
    16. this->info.minFreq,
    17. this->info.maxFreq
    18. );
    19. } catch(QLE e) {
    20. emit showCritical(e.msg.c_str());
    21. emit showStatus("Excitation generating failed!", 2000);
    22. return;
    23. }
    24. emit showStatus("Excitation and inverse filter are generated!", 2000);
    25. }
    To copy to clipboard, switch view to plain text mode 

    That "while" was with the aim to understand how all this works :-)

Similar Threads

  1. Replies: 19
    Last Post: 22nd December 2007, 11:23
  2. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16
  5. advanced split widget
    By vitaly in forum Qt Programming
    Replies: 10
    Last Post: 24th January 2006, 20:00

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.