×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    43

    QThread and MainWindow (Qt designer)

    Hi!

    I am developing my first application with Qt and I would like to ask you the way to update the MainWindow from a thread.

    The main window has been created with Qt designer, and now I want to update one of its Widgets (QTextEdit) from a thread. I have managed to do that in Mac OS X, creating a public function for MainWindow that updates QTextEdit. The pointer to MainWindow is passed to the thread and the thread is using the function. I am sure that is not the best way to do this. It seems that when I am passing the pointer of the MainWindow to the thread and using the function the program is not working.

    How could I update the QTextEdit? (I want to append a QString)

    Thank you in advance!

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    53

    Re: QThread and MainWindow (Qt designer)

    Hi darksoul_e!

    You can use the signal / slot to connect between your thread and main thread. emit signal from your thread and slot in main thread will update QTextEdit.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    43

    Re: QThread and MainWindow (Qt designer)

    Thank you for your answer Zombieisme!

    I was thinking in that option, but I do not understand how can I pass the QString using signals. I will try to do it, otherwise any suggestion or help is welcome

    Thank you!

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    43

    Re: QThread and MainWindow (Qt designer)

    Ok! reading the Qt book again, I realized that I can pass parameters in the signals... This way I can pass the QString

  5. #5
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QThread and MainWindow (Qt designer)

    Quote Originally Posted by darksoul_e View Post
    I was thinking in that option, but I do not understand how can I pass the QString using signals. I will try to do it, otherwise any suggestion or help is welcome
    Refer to the documentation
    http://doc.trolltech.com/4.6/signalsandslots.html
    This is how you emit a signal with QString as a parameter:
    Code:
    QString stringParameter = "Test string";
    emit signalName(stringParameter);

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    43

    Re: QThread and MainWindow (Qt designer)

    I think I am missing something important with signals

    I am using the book C++ GUI programming with Qt3, but the example is very similar to the one in the link. MyThread.h looks like this:

    Code:
    #include "mainwindow.h"
    #include <QObject>
    #include <QThread>
    
    class MyThread : public QThread
        {
        Q_OBJECT
    
        public:
            MyThread(QObject* parent = 0);
            virtual ~MyThread();
    
        public: // From QThread
            void run();
            QString str;
    
        signals:
            void sendString(const QString &str);
        public slots:
    
        private:
        };
    The slot of the MainWindow is:

    Code:
    #include <QMainWindow>
    
    namespace Ui {
        class MainWindow;
    }
    
    class MainWindow : public QMainWindow {
        Q_OBJECT
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void UpdateChat(QString str);
    
    protected:
        void changeEvent(QEvent *e);
    
    private:
        Ui::MainWindow *ui;
    private slots:
       (many)
    public slots:
        void display(const QString &str);
    };
    And now, in MyThread.cpp I do not know how to connect the signal

    Code:
        connect(this, SIGNAL(sendString(const QString &)),
                ?, SLOT(display(const QString &)));
    Do I have to pass the reference of the instance of MainWindow to the thread to send the signal? How can I connect both, the thread and the MainWindow?

    The files that I have are: mainwindow.cpp , main.cpp and mythread.cpp

    Thank you in advance

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QThread and MainWindow (Qt designer)

    Quote Originally Posted by darksoul_e View Post
    And now, in MyThread.cpp I do not know how to connect the signal

    Code:
        connect(this, SIGNAL(sendString(const QString &)),
                ?, SLOT(display(const QString &)));
    You connect Thread's signal o Window's slot in main.cpp.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    43

    Re: QThread and MainWindow (Qt designer)

    Sorry! I am doing the connection within the thread, so this refers to my thread.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    43

    Re: QThread and MainWindow (Qt designer)

    Finally I achieved that, but I do not know if the solution is very elegant...

    From the main I have passed to the Thread the reference of MainWindow:

    Code:
    class MyThread : public QThread
        {
        Q_OBJECT
    
        public:
            MyThread(QObject* parent = 0);
            virtual ~MyThread();
    
        public: // From QThread
            void run();
            QString str;
            MainWindow *w;
    
        signals:
            void sendString(const QString &str);
        public slots:
    
        private:
        };
    Main:

    Code:
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        MyThread *thread;
    
        w.show(); 
        thread = new MyThread();
        thread->w=&w;
        thread->start();
        //thread.wait();
        return a.exec();
    }
    And I connect this way in MyThread.cpp

    Code:
    QObject::connect(this, SIGNAL(sendString(const QString &)),
                w, SLOT(display(const QString &)));

  10. #10
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QThread and MainWindow (Qt designer)

    Could you consider this way of doing a thing?
    Code:
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        MyThread thread;
    
        QObject::connect(&thread, SIGNAL(sendString(const QString &)),
                         &w, SLOT(display(const QString &)));
        w.show(); 
        thread.start();
    
        return a.exec();
    }

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    43

    Re: QThread and MainWindow (Qt designer)

    Yes This way is better. Thank you!

    I have changed your code to use the heap for the thread.

    Quote Originally Posted by divanov View Post
    Could you consider this way of doing a thing?
    Code:
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        MyThread *thread;
    
        thread=new MyThread();
        QObject::connect(thread, SIGNAL(sendString(const QString &)),
                         &w, SLOT(display(const QString &)));
        w.show(); 
        thread.start();
    
        return a.exec();
    }

  12. #12
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QThread and MainWindow (Qt designer)

    There is no really good reason to use heap, but if you allocating your thread object dynamically, then you should free memory at some point, otherwise you have a memory leak.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×