Important: Please read the Qt Code of Conduct -

connect signal slot between two classes ?

  • 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 ? )


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


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        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";


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


    #include "ifb_com.h"
    #include "mainwindow.h"
    IFB_Com::IFB_Com(QObject *parent) : QObject(parent) {
        Tick1 = new QTimer(this);
    //*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.


  • I think your first connect statement should be:

    connect(ifb->Tick1, SIGNAL(timeout()), this, SLOT(on_Got_CMD()));

  • yep, thank you !

  • @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.

  • thank You :)

  • 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

        void mySignal();
    connect(Tick1, SIGNAL(timeout()), this, SIGNAL(mySignal()));

    So in MainWindow you can do

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

  • @mchinand

    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 ?

    What is the advantage of using the signal forwarding technique ?

  • 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.

  • Yes, convincing, thanks :)

Log in to reply