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. connect signal slot between two classes ?
Forum Updated to NodeBB v4.3 + New Features

connect signal slot between two classes ?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.8k Views 2 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.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by deleted28
    #1

    Hello,

    i have problems to get the SIGNAL of class IFB_Com connected to a SLOT Mainwindow. (Put the QTimer "Tick1 in class Mainwindow is not the solution i 'm searching for :)
    here's the code:
    (btw: where are the useful "quote" "code" buttons to insert got lost ? )

    mainwindow.h:

    #include <QMainWindow>
    #include <QDebug>
    #include "ifb_com.h"
    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        IFB_Com *ifb;
    public slots:
        void on_Got_CMD();
    private:
        Ui::MainWindow *ui;
    };
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ifb = new IFB_Com(this);
    
    //*1    connect(IFB_Com::Tick1, SIGNAL(timeout()), this, SLOT(on_Got_CMD()));
    }
    
    void MainWindow::on_Got_CMD() {
        qDebug() << "[on_Got_CMD_1] :" << "got command";
    }
    

    ifb_com.h:

    #include <QObject>
    #include <QTimer>
    
    class IFB_Com : public QObject
    {
        Q_OBJECT
    
    public:
        explicit IFB_Com(QObject *parent = 0);
        QTimer *Tick1;
    };
    

    ifb_com.cpp:

    #include "ifb_com.h"
    #include "mainwindow.h"
    
    IFB_Com::IFB_Com(QObject *parent) : QObject(parent) {
    
        Tick1 = new QTimer(this);
        Tick1->setInterval(2000);
        Tick1->start();
    
    //*2    connect(Tick1, SIGNAL(timeout()), MainWindow::MainWindow, SLOT(on_Got_CMD()));
    
    }
    

    If line //*1 is not commented, but //*2 , Error output is:

    /home/.../ifb_com.h:15: error: invalid use of non-static data member 'IFB_Com::Tick1'
    QTimer *Tick1;
    ^
    /home/.../mainwindow.cpp:13: error: from this location
    connect(IFB_Com::Tick1, SIGNAL(timeout()), this, SLOT(on_Got_CMD()));
    ^
    If line //*2 is not commented, but //*1 , Error output is:

    /home/.../ifb_com.cpp:11: error: no matching function for call to 'IFB_Com::connect(QTimer*&, const char*, <unresolved overloaded function type>, const char*)'
    connect(Tick1, SIGNAL(timeout()), MainWindow::MainWindow, SLOT(on_Got_CMD()));
    ^

    Any suggestion or link to example highly welcome, thx.

    test
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      I think your first connect statement should be:

      connect(ifb->Tick1, SIGNAL(timeout()), this, SLOT(on_Got_CMD()));
      
      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        yep, thank you !

        K 1 Reply Last reply
        0
        • D deleted28

          yep, thank you !

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @wally123
          I have added some code markers to your initial post. Please check out the "Editor markdown tags" at the end of the page respectively the "Cheatsheet". You are right the buttons are gone and conncet be implemented here easily AFAIK.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deleted28
            wrote on last edited by deleted28
            #5
            thank You :)
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by mcosta
              #6

              Hi,

              I suggest to make Tick private and use the signal forwarding technique.
              In IFB_Com you have to define a new signal mySignal() and connect the QTimer::timeout() signal to it

              //ifb_com.h
              
              ....
              signals:
                  void mySignal();
              ....
              
              //ifb_com.cpp
              connect(Tick1, SIGNAL(timeout()), this, SIGNAL(mySignal()));
              

              So in MainWindow you can do

              connect (ifb, SIGNAL(mySignal()), this, SLOT(on_Got_CMD()));
              

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deleted28
                wrote on last edited by deleted28
                #7

                @mchinand
                @mcosta

                both suggestions works fine and I get the "got command" output.
                First line of application output for both solutions is:

                QMetaObject::connectSlotsByName: No matching signal for on_Got_CMD()
                [on_Got_CMD_1] : got command
                ... and so on
                

                What's the matter with this "No matching signal for on_Got_CMD()" ?
                ( I assume i should not use "on_Got_CMD" but something without "on_"
                in slotname, right ?

                @mcosta
                What is the advantage of using the signal forwarding technique ?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mcosta
                  wrote on last edited by
                  #8

                  Hi,

                  QMetaObject::connectSlotsByName: No matching signal for on_Got_CMD()

                  Qt tries to create automatic connection using this schema on_<objectName>_<signalName>(); the warning is because your slot has a name that confuses the MetaObject system.
                  You can Ignore the warning or simply change the slot name.

                  What is the advantage of using the signal forwarding technique ?

                  Basic Information Hiding; better not show how an object is implemented.
                  If in the future you'll need or want change the Implementation of IFB_Com (for example generating mySignal() in different way) you don't need to change the MainWindow code.

                  Once your problem is solved don't forget to:

                  • Mark the thread as SOLVED using the Topic Tool menu
                  • Vote up the answer(s) that helped you to solve the issue

                  You can embed images using (http://imgur.com/) or (http://postimage.org/)

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    deleted28
                    wrote on last edited by deleted28
                    #9

                    Yes, convincing, thanks :)

                    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