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. One signal to two window
Forum Updated to NodeBB v4.3 + New Features

One signal to two window

Scheduled Pinned Locked Moved General and Desktop
27 Posts 4 Posters 7.6k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andreyc
    wrote on last edited by
    #6

    There are multiple solutions.
    For example

    1. create second window and connect all necessary signals inside first window constructor. I think signals are not delivered if a window is not shown.
    2. In QPushButton::clicked() show second window.
    1 Reply Last reply
    0
    • K Offline
      K Offline
      kdbcz
      wrote on last edited by
      #7

      I cant do it.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #8

        What do you mean. Are there any errors?
        Show what you have so far and we will help to fix it.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kdbcz
          wrote on last edited by
          #9

          How can I use "QPushButton::clicked() show second window" in the main, if I have an UI window with that pushbutton? (I can open the second window from the first window, but than I cant make the connection between the worker thread and the second window with the same signal what iI use for the first window in the worker thread (because I initialized the worker in the main)).
          I tried to send a signal from that pushbutton, but I cant do to the main.
          My program is same now what you give me in the example.
          Thank You!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #10

            Add a pointer to Worker to WindowOne class. Initialize it in WindowOne constructor.
            Use it when you opening second window.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kdbcz
              wrote on last edited by
              #11

              It is working now. I initialize the worker and the second window in the first window constructor. And connect everything there.
              Thank You!

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kdbcz
                wrote on last edited by
                #12

                Hello,
                I have an other conection problem now.
                I have a widget what I want to connect to my worker thread.
                My thread is initialized in other class (Mainwindow).

                widget .h:
                @class RoverSatView : public QWidget
                {
                Q_OBJECT
                public:
                RoverSatView(QWidget *parent = 0);

                protected:
                void paintEvent(QPaintEvent *event);

                private slots:
                void slot_rtk_rover_sat_azel_in(const SatList &sats);

                private:
                    SatList satellites;
                    rtk_thread worker;
                

                };

                widget .cpp:

                RoverSatView::RoverSatView(QWidget *parent) : QWidget(parent)
                {
                connect(&worker,SIGNAL(rtk_rover_sat_azel(const SatList)),this,SLOT(slot_rtk_rover_sat_azel_in(const SatList)));
                }@

                No error message, but not working. What is the misstake?
                Of course I have emit signal in the worker thread.

                1 Reply Last reply
                0
                • JohanSoloJ Offline
                  JohanSoloJ Offline
                  JohanSolo
                  wrote on last edited by JohanSolo
                  #13
                  connect(&worker,SIGNAL(rtk_rover_sat_azel(const SatList)),this,SLOT(slot_rtk_rover_sat_azel_in(const SatList)));
                  

                  Did you register your SatList type?
                  By the way, there is no need to use the const in the connect statement.

                  `They did not know it was impossible, so they did it.'
                  -- Mark Twain

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andreyc
                    wrote on last edited by
                    #14

                    How do you assign worker from MainWindow to RoverSatView?
                    Are there any message in a console when you start the app?

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kdbcz
                      wrote on last edited by
                      #15

                      Yes, the SatList type is registered.
                      No message in the consol.
                      I have this in the class RoverSatView header:
                      @ private:
                      rtk_thread worker;
                      @

                      @MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);

                      QThread* rtkthread=new QThread;
                      rtk_thread* worker=new rtk_thread;
                      

                      ...
                      worker->moveToThread(rtkthread);
                      connect(rtkthread, SIGNAL(started()), worker, SLOT(run()));
                      connect(worker, SIGNAL(finished()), rtkthread, SLOT(quit()));
                      connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
                      connect(rtkthread, SIGNAL(finished()), rtkthread, SLOT(deleteLater()));
                      rtkthread->start();

                      }
                      @
                      @class RoverSatView : public QWidget
                      {
                      Q_OBJECT
                      public:
                      RoverSatView(QWidget *parent = 0);

                      protected:
                      void paintEvent(QPaintEvent *event);

                      private slots:
                      void slot_rtk_rover_sat_azel_in(const SatList &sats);

                      private:
                          SatList satellites;
                          rtk_thread worker;
                      

                      };@

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andreyc
                        wrote on last edited by
                        #16

                        You start thread for one worker object in MainWindow and connect signals from different worker object in RoverSatView.

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kdbcz
                          wrote on last edited by
                          #17

                          How can I conect in RoverSatView to the same worker as in MainWindow?

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andreyc
                            wrote on last edited by
                            #18

                            If RoverSatView is a member of MainWindow then you can declare worker as member of MainWindow and then pass it RoverSatView constructor.
                            If it is not then allocate worker before MainWindow, pass it to MainWindow ctor and to RoverSatView.

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              kdbcz
                              wrote on last edited by
                              #19

                              Hello,

                              How can I pass it if RoverSatView is a widget in a generated UI tabwidget?

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                arsinte_andrei
                                wrote on last edited by
                                #20

                                then where you have the generation code you simply take that variable from there - from the constructor and connect it. just after it was constructed or generated

                                1 Reply Last reply
                                0
                                • K Offline
                                  K Offline
                                  kdbcz
                                  wrote on last edited by
                                  #21

                                  It is not working for me. "....was not declared in this scope"
                                  How can I pass a variable to a designer generated ui object?

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andreyc
                                    wrote on last edited by
                                    #22

                                    Here is an example based on the sources you posted before.
                                    See my comments below started with // <<

                                    • MainWindow.h
                                      @
                                      class MainWindow : public QMainWindow
                                      {
                                      ...
                                      rtk_thread* worker; // << Declare worker in MainWindow
                                      RoverSatView* roverSatView;
                                      };
                                      @

                                    • MainWindow.cpp
                                      @
                                      MainWindow::MainWindow(QWidget *parent)
                                      : QMainWindow(parent)
                                      , ui(new Ui::MainWindow)
                                      {
                                      ui->setupUi(this);

                                      QThread* rtkthread = new QThread;
                                      worker = new rtk_thread; // << Initialize it in ctor
                                      ...
                                      worker->moveToThread(rtkthread);
                                      connect(rtkthread, SIGNAL(started()), worker, SLOT(run()));
                                      connect(worker, SIGNAL(finished()), rtkthread, SLOT(quit()));
                                      connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
                                      connect(rtkthread, SIGNAL(finished()), rtkthread, SLOT(deleteLater()));
                                      rtkthread->start();
                                      }

                                    void MainWindow::createRoverSatView()
                                    {
                                    roverSatView = new RoverSatView(worker, this); // << pass it to RoverSatView
                                    ...
                                    }
                                    @

                                    • RoverSatView.h
                                      @
                                      class RoverSatView : public QWidget
                                      {
                                      Q_OBJECT
                                      public:
                                      RoverSatView(rtk_thread* rtkWorker, QWidget *parent = 0);
                                      // << ctor accepts the worker from MainWindow

                                      protected:
                                      void paintEvent(QPaintEvent *event);

                                      private slots:
                                      void slot_rtk_rover_sat_azel_in(const SatList &sats);

                                      private:
                                      SatList satellites;
                                      rtk_thread* worker; // << it will keep worker from MainWindow
                                      };
                                      @

                                    • RoverSatView.cpp
                                      @
                                      RoverSatView::RoverSatView(rtk_thread* rtkWorker, QWidget* parent)
                                      : QWidget(parent)
                                      , worker(rtkWorker) // << initialize using worker from MainWindow
                                      {
                                      // Initialize the Qt magic
                                      connect(worker, SIGNAL(rtk_rover_sat_azel(SatList)),
                                      this, SLOT(slot_rtk_rover_sat_azel_in(SatList)));
                                      }
                                      @

                                    1 Reply Last reply
                                    0
                                    • K Offline
                                      K Offline
                                      kdbcz
                                      wrote on last edited by
                                      #23

                                      Thank You for Your help!
                                      It is working now in a window, but I would like to add this RoverSatView to a tab of a tabwidget which is created in the creator. Is it possible, or I have to create the tabwidget in the code?

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        andreyc
                                        wrote on last edited by
                                        #24

                                        You can use "QTabWidget::addTab()":http://qt-project.org/doc/qt-5/qtabwidget.html#addTab to put your widget into generated QTabWidget

                                        1 Reply Last reply
                                        0
                                        • K Offline
                                          K Offline
                                          kdbcz
                                          wrote on last edited by
                                          #25

                                          Hello,

                                          This is working:
                                          @connect(worker,SIGNAL(active_window(double)),this,SLOT(slot_active_window(double)));@

                                          This is not working:
                                          @connect(this,SIGNAL(active_window(double)),worker,SLOT(slot_active_window(double)));@

                                          Both in same place. I want to connect the thread (worker) and a window (this) in both direction.
                                          Why the second not working? Or I have to put the "connect..." to the SLOT side?

                                          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