Communication Between MainWindow & Thread class [Solved]



  • Hello Geeks,

    I'm writing a sample application. I have a thread class. I m creating an instance of thread class in main window. I want to connect a main window signal to thread class slot. Below is my implementation.

    Threadclass
    @
    class CThreadClass : public QThread
    {
    Q_OBJECT

    public :
    CThreadClass ();
    ~CThreadClass ();

    private:
    void run();

    public slots:
    void onUpdate(QString);

    };
    @

    Main Window.h

    @
    signals:
    void valueChanged(QString);

    CThreadClass *     m_ptrMyThread;
    

    @

    Main Window.cpp
    @
    CMainWindow::CMainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_ptrMyThread(new CThreadClass())
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    m_ptrMyThread->start();
    connect(this, SIGNAL(valueChanged(QString)), m_ptrMyThread, SLOT(onUpdate(QString)));
    }
    @

    I am emitting valueChanged inside a method. But the connected slot is never called.

    Please let me know what is the wrong in my code.

    Thank you.



  • Hello,

    where do You init the
    @m_ptrMyThread@ pointer??

    i.e. where do You have something like this:
    @m_ptrMyThread = new CThreadClass()@
    ??

    Maybe this is something what You are missing.

    Another question: when You start the application, aren't there any console information about unsuccessful connections?

    One more hint: I would start the thread after making all the needed connections.

    with best regards,
    poorBob



  • Hello dear qt_sud

    where did you emit signal in mainwindow class?



  • Hi PoorBob,

    I am initializing m_ptrMyThread inside my constructor.

    CMainWindow::CMainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_ptrMyThread(new CThreadClass())
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    m_ptrMyThread->start();
    connect(this, SIGNAL(valueChanged(QString)), m_ptrMyThread, SLOT(onUpdate(QString)));
    }



  • Thank you all. I find the error. My signal name was missing after emit statment.



  • Ok,

    mark the post title with [SOLVED]

    best wishes,
    poorBob



  • Glad to hear.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.