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 ? )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
-
Hi,
I suggest to make Tick private and use the signal forwarding technique.
In IFB_Com you have to define a new signalmySignal()
and connect theQTimer::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()));
-
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 ? -
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 ofIFB_Com
(for example generatingmySignal()
in different way) you don't need to change the MainWindow code.