简体   繁体   中英

C++, Qt Signals & slots

I'm trying to understand Qt 4.8 signals and slots so I wrote some code to test it out for myself. Eventually, I want to be able to use a common source file in my project so that serial ports can be accessed from any source file in the project.

I set up a Qt GUI application and added a C++ class header and source file, shown below.

When I try to build, I get the error message when I try to emit the signal.

/home/user/QTProjects/stest1/stest1/ser.cpp:25: error: invalid use of 'this' in non-member function

I haven't even gotten to the stage of setting up the connections yet!

My newbie status is obvious, I'd be grateful for any help.

Thanks, James


The following is the 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()), ser, SLOT(testprint()));

}

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);
}

void MainWindow::printtext(const QByteArray &data)
{
    ui->textEdit->insertPlainText(QString(data));
}

The following is the 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();

private slots:
    void on_pushButton_clicked();
    void printtext(const QByteArray &data);

private:
    Ui::MainWindow *ui;

signals:
//    void click;
};

#endif // MAINWINDOW_H

The following is ser.cpp:-

#include "ser.h"

#include <QObject>

ser::ser(QObject *parent) :
    QObject(parent)
{
}

void ser::init()
{

 //   connect(this->, SIGNAL(testsignal), MainWindow, SLOT(printtext(const QByteArray &data)));

}

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 this->testsignal(ba1);
}

The following is 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

Your method is implemented as void testprint() { ... } , but it should be void ser::testprint() { ... } . It's in your cpp file.

Also note that you don't need to use this-> to refer to class members. emit testsignal(ba1); will fork fine.

I think should be

connect (this, SIGNAL(click()), j, SLOT(testprint()));

instead of

connect (this, SIGNAL(click()), ser, SLOT(testprint()));

that apart, I can't spot where you connect testsignal

Great, that worked. connect (this, SIGNAL(click()), j, SLOT(testprint()));

My next problem is connecting the signal in ser to the slot in the MainWindow. I used

connect(j,
        SIGNAL(testsignal),
        this,
        SLOT(printtext(const QByteArray &data)));

It was inserted immediately after the other connect statement. This does not print out the expected message "Slong". It also does not give me any error! What is the problem? James

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM