简体   繁体   English

C ++,Qt信号和插槽

[英]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. 我试图了解Qt 4.8信号和插槽,所以我写了一些代码自己测试。 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. 我设置了一个Qt GUI应用程序,并添加了一个C ++类头文件和源文件,如下所示。

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 /home/user/QTProjects/stest1/stest1/ser.cpp:25:错误:在非成员函数中无效使用了“ this”

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:- 以下是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 以下是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:- 以下是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 以下是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() { ... } . 您的方法实现为void testprint() { ... } ,但应为void ser::testprint() { ... } It's in your cpp file. 它在您的cpp文件中。

Also note that you don't need to use this-> to refer to class members. 还要注意,您不需要使用this->来引用类成员。 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 除此之外,我无法发现您在哪里连接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. 我的下一个问题是将ser中的信号连接到MainWindow中的插槽。 I used 我用了

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

It was inserted immediately after the other connect statement. 它是在另一个connect语句之后立即插入的。 This does not print out the expected message "Slong". 这不会打印出预期的消息“ Slong”。 It also does not give me any error! 它也不会给我任何错误! What is the problem? 问题是什么? James 詹姆士

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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