In Qt 4.8, I'm trying to get a signal in one class to communicate with a slot in the MainWindow and, while the code is executing, I'm not getting the desired result. I have two connect statements, one connects a signal in MainWindow to a slot in ser. The first connect statement is working fine and executing testprint() in ser (it stops at a break point in there when run). The second connect is for a signal in ser to a slot in MainWindow. However, this second connect is not working as I don't the signal from testprint and printtext is just executed once.
Whats the issue here? Why is ser not emitting a signal? Regards, James
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ser.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ser *j = new ser;
j->init();
connect (this, SIGNAL(click()), j, SLOT(testprint()));
// connect testsignal from ser to printtext on MainWindow
connect(j,
SIGNAL(testsignal),
this,
SLOT(printtext(const QByteArray &data)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QByteArray ba1;
ba1.resize(6);
ba1[0]='H'; ba1[1]='e'; ba1[2]='l'; ba1[3]='l'; ba1[4]='o'; ba1[5]='\n';
this->printtext(ba1);
emit click();
}
void MainWindow::printtext(const QByteArray &data)
{
ui->textEdit->insertPlainText(QString(data));
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
MainWindow * myMain;
private slots:
void on_pushButton_clicked();
public slots:
void printtext(const QByteArray &data);
private:
Ui::MainWindow *ui;
signals:
void click();
};
#endif // MAINWINDOW_H
ser.cpp
#include "ser.h"
#include "mainwindow.h"
#include <QObject>
ser::ser(QObject *parent) :
QObject(parent)
{
}
void ser::init()
{
}
void ser::testprint()
{
QByteArray ba1;
ba1.resize(8);
ba1[0]='S'; ba1[1]= '0'; ba1[2]= ' '; ba1[3]= 'l'; ba1[4]='o'; ba1[5]='n'; ba1[6]='g'; ba1[7]='\n';
emit testsignal(ba1);
}
ser.h
#ifndef SER_H
#define SER_H
#include "mainwindow.h"
#include <QObject>
class ser : public QObject
{
Q_OBJECT
public:
explicit ser(QObject *parent = 0);
void init();
signals:
void testsignal(const QByteArray &data);
private slots:
void testprint();
public slots:
};
#endif // SER_H
In your code: -
connect(j,
SIGNAL(testsignal),
this,
SLOT(printtext(const QByteArray& data)));
You're missing the parameters after testsignal, so it should be SIGNAL(testsignal(const QByteArray&)). Also, the parameters can not be named, so remove 'data' in the slot.
Note that the connect function returns a type of QMetaObject::Connection, which you can test for. If it returns NULL, the connection was not made.
Also, when the code runs over a connect function, a debugger output window will usually tell you if the connection failed.
This line is wrong
connect(j,
SIGNAL(testsignal),
this,
SLOT(printtext(const QByteArray &data)));
You need to specify signal parameters like this:
connect(j, SIGNAL(testsignal(QByteArray)), this, SLOT(printtext(QByteArray)));
Note that you don't need to provide parameter names (like const QByteArray &data
), just type is enough. Also, connect
method returns bool
result, so you can check it's result that to make sure all connects are done. When connection fails, you can also check debug output for detailed error information
You've got your SIGNAL
and SLOT
parameters slightly mixed up.
connect(j,
SIGNAL(testsignal),
this,
SLOT(printtext(const QByteArray &data)));
In this statement you're missing two things, you need the parentheses after testsignal
, and also that testsignal
will send data, so it should read SIGNAL(testsignal(const QByteArray &))
, and in your slot you don't put the variable name only the data type. So it should read SLOT(printtext(const QByteArray &))
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.