Results 1 to 4 of 4

Thread: access to ui in a threaded application

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default access to ui in a threaded application

    Hi,

    I need to to manage serial port stream, and update my ui consequently.
    Reading some post here in the forum I wrote the attached example zufolo.tar.gz, where the thread zotthread simulates serial port management.

    here is the screenshot
    screenshot.png

    the application is very simple.

    that are slots in dialog:
    Qt Code:
    1. void Dialog::ovvaivai(void)
    2. {
    3. zotThread *zt;
    4.  
    5. zt = new zotThread;
    6.  
    7. connect(zt, SIGNAL(lancettasecondi(const QString&)), this, SLOT(cambiatestobottone(const QString&)));
    8. connect(zt, SIGNAL(uscita()), this, SLOT(chiusura()));
    9. zt->start();
    10. }
    11.  
    12. void Dialog::cambiatestobottone(const QString &s)
    13. {
    14. ui->pushButton->setText(s);
    15. }
    16.  
    17. void Dialog::chiusura(void)
    18. {
    19. close();
    20. }
    To copy to clipboard, switch view to plain text mode 

    and this is the thread taht simulates serial management
    Qt Code:
    1. void zotThread::run(void)
    2. {
    3. int i;
    4. QTimer qt;
    5. for(i=5;i>0;i--)
    6. {
    7. s.setNum(i);
    8. emit lancettasecondi(s);
    9. qt.setSingleShot(true);
    10. qt.start(1000);
    11. while(qt.isActive()) QCoreApplication::processEvents();
    12. }
    13. emit uscita();
    14. }
    To copy to clipboard, switch view to plain text mode 

    when clicking the pushbutton ovvaivai(void) is called, so then thread starts. It perform a 5 seconds countdown before close the application.

    I wonder if this is the right way to change the ui from a thread.
    An also I wonder if is correct to call QCoreApplication::processEvents(); from inside trhead main loop. Without that call timer dosn't walk.

    Now I want to read the spin value from thread. How can I perform that action? I don't think through signal/slot.

    thanks

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

    Default Re: access to ui in a threaded application

    Better and more elegant will be something like this :
    Qt Code:
    1. class zotThread : public QThread
    2. {
    3. .
    4. .
    5. private slots :
    6. void oneStep( void );
    7. private :
    8. int count_down;
    9. }
    10.  
    11. void zotThread::run(void)
    12. {
    13. count_down = 5;
    14. QTimer::singleShot(0,this, SLOT(oneStep()));
    15. QThread::run();//event loop is starting
    16. }
    17.  
    18. void zotThread::singleStep(void)
    19. (
    20. if( count_down < 0 )
    21. {
    22. quit();//event loop is stopped
    23. return;
    24. }
    25. s.setNum(count_down--);
    26. emit lancettasecondi(s);
    27. QTimer::singleShot(1000,this, SLOT(oneStep()));
    28. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: access to ui in a threaded application

    Quote Originally Posted by mastupristi View Post
    Now I want to read the spin value from thread. How can I perform that action? I don't think through signal/slot.
    Why not ? Just connect signal QSpinBox::valueChanged ( int i ) to slot in Yours thread and all.

  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: access to ui in a threaded application

    Quote Originally Posted by mastupristi View Post
    I wonder if this is the right way to change the ui from a thread.
    You can't access ui from a thread.

    An also I wonder if is correct to call QCoreApplication::processEvents(); from inside trhead main loop.
    It's not.
    Without that call timer dosn't walk.
    Use QThread::exec().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Memory leak in my threaded SSL server application
    By moosa in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2010, 07:49
  2. Replies: 1
    Last Post: 20th February 2010, 04:26
  3. Confusing bug with threaded application
    By kachofool in forum Newbie
    Replies: 1
    Last Post: 15th December 2009, 23:32
  4. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 16:32
  5. QSqlDatabase, threaded access and locks
    By neuron in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2009, 15:55

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.