Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Communication Between MainWindow & Thread class [Solved]
QtWS25 Last Chance

Communication Between MainWindow & Thread class [Solved]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qt_sud
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      poorBob
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MohammadReza
        wrote on last edited by
        #3

        Hello dear qt_sud

        where did you emit signal in mainwindow class?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qt_sud
          wrote on last edited by
          #4

          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)));
          }

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qt_sud
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • P Offline
              P Offline
              poorBob
              wrote on last edited by
              #6

              Ok,

              mark the post title with [SOLVED]

              best wishes,
              poorBob

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MohammadReza
                wrote on last edited by
                #7

                Glad to hear.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved