Results 1 to 6 of 6

Thread: thread question

  1. #1
    Join Date
    Jan 2009
    Posts
    78
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default thread question

    Hi
    I was reading one example about threads(wish i know almost nothing about) and got carried away by the imagination

    I have one dialog:

    ubmsr.h
    Qt Code:
    1. #ifndef UBMSR_H
    2. #define UBMSR_H
    3.  
    4. #include <QtGui/QDialog>
    5.  
    6. #include "senderThread.h"
    7.  
    8. namespace Ui
    9. {
    10. class UbmsrClass;
    11. }
    12.  
    13. class Ubmsr : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. Ubmsr(QWidget *parent = 0);
    19. ~Ubmsr();
    20. private slots:
    21. void startOrStopSenderThread();
    22. private:
    23. Ui::UbmsrClass *ui;
    24. senderThread *sender;
    25. };
    26.  
    27. #endif // UBMSR_H
    To copy to clipboard, switch view to plain text mode 

    ubmsr.cpp
    Qt Code:
    1. #include "ubmsr.h"
    2. #include "ui_ubmsr.h"
    3.  
    4. Ubmsr::Ubmsr(QWidget *parent)
    5. : QDialog(parent), ui(new Ui::UbmsrClass)
    6. {
    7. ui->setupUi(this);
    8. sender = new senderThread;
    9. sender->setMessage("Hello!");
    10. connect(ui->senderButton, SIGNAL(clicked()), this, SLOT(startOrStopSenderThread()));
    11. }
    12.  
    13. Ubmsr::~Ubmsr()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void Ubmsr::startOrStopSenderThread(){
    19. ui->senderButton->setText("asdasd");
    20. if (sender->isRunning()) {
    21. sender->stop();
    22. ui->senderButton->setText(tr("Start Sender"));
    23. ui->senderMsg->setEnabled(true);
    24. ui->senderPort->setEnabled(true);
    25. }else{
    26. sender->start();
    27. ui->senderButton->setText(tr("Stop Sender"));
    28. ui->senderMsg->setEnabled(false);
    29. ui->senderPort->setEnabled(false);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    senderThread.h
    Qt Code:
    1. #ifndef SENDERTHREAD_H
    2. #define SENDERTHREAD_H
    3.  
    4. #include <QThread>
    5.  
    6. class senderThread : public QThread
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. senderThread();
    12. void setMessage(const QString &message);
    13. void stop();
    14.  
    15. protected:
    16. void run();
    17.  
    18. private:
    19. volatile bool senderStopped;
    20. QString messageStr;
    21.  
    22. };
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    senderThread.cpp
    Qt Code:
    1. #include <QtCore>
    2. #include <iostream>
    3.  
    4. #include "senderThread.h"
    5.  
    6. using namespace std;
    7.  
    8. senderThread::senderThread()
    9. {
    10. senderStopped = false;
    11. }
    12.  
    13. void senderThread::setMessage(const QString &message)
    14. {
    15. messageStr = message;
    16. }
    17.  
    18. void senderThread::run()
    19. {
    20. while (!senderStopped)
    21. cerr << qPrintable(messageStr);
    22. senderStopped = false;
    23. cerr << endl;}
    24.  
    25. void senderThread::stop()
    26. {
    27. senderStopped = true;
    28. }
    To copy to clipboard, switch view to plain text mode 

    I can start and stop the thread and change the button label.

    Is it possible to access the objects in the ui from a member function in the thread class?!

    The final idea is to have 2 diferent threads sending and reading from a local network and showin all in the dialog i created.

    I already managed the network issue ... the problem is putting this all togheter

    How can i do it?

    Thanks

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

    Default Re: thread question

    No, you can't access GUI objects from non-gui thread. You should do "worker" stuff in the "worker" thread and then send a signal or event to the main thread that it needs to render the data on screen.
    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.


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

    gt.beta2 (13th March 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: thread question

    Quote Originally Posted by gt.beta2 View Post
    Is it possible to access the objects in the ui from a member function in the thread class?!
    Hi, this is not a very mature answer due to I am also not familiar with threads, but accessing a member is only possible by setting a pointer in the thread to the "main" class. But that's a bad idea, so leave it! But you can even use the signal and slot mechanism through the thread border. That should probably be the solution to your problem.

    Lykurg

    Edit: too late...

  5. The following user says thank you to Lykurg for this useful post:

    gt.beta2 (13th March 2009)

  6. #4
    Join Date
    Jan 2009
    Posts
    78
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default Re: thread question

    I'm having trouble with the translation.
    What does thread-safe or reentrant mean?
    In short words if possible
    Thanks

    opss ... found it http://doc.trolltech.com/4.5/threads...eading-classes
    Last edited by gt.beta2; 13th March 2009 at 19:26.

  7. #5
    Join Date
    Jan 2009
    Posts
    78
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default Re: thread question

    Still about this subject... i've been reading about QtConcurrentRun.
    Would it be possible for me to launch 2 member functions of the initial Ubmsr class in 2 different thread using this?
    Imagine that f1() is broadcasting every 1 second and f2() is listening in other port!? This way i would have access to the GUI objects.

    Does this make sense or showld i forget it?

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

    Default Re: thread question

    In theory you could do that. But my personal opinion is that it's not worth the trouble. If you want to have multiple threads, then use a classical threaded approach and use signals and slots to communicate with the main thread that will manipulate the GUI. The real question is if you should use threads at all.
    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. QTableWidget Design & Thread Safety Question
    By bpetty in forum Qt Programming
    Replies: 4
    Last Post: 28th March 2008, 00:09
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02

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.