简体   繁体   English

信号和插槽以及向主窗口发出信号

[英]Signal and Slot and emitting signal to mainwindow

The mouse class creates a process in a new thread and runs evtest command to listen to my mouse button presses.鼠标类在一个新线程中创建一个进程并运行 evtest 命令来监听我的鼠标按钮按下。 ButtonReceived in the mouse class receives the process's output line by line as I press buttons.当我按下按钮时,鼠标类中的 ButtonReceived 逐行接收进程的输出。 I added some of the output I get as a comment.我添加了一些我得到的输出作为评论。 Now I am trying to emit a signal depending on the output back to the mainwindow class.现在我正在尝试根据输出向主窗口类发出信号。 In the mainwindow class constructor, I create a mouse class and connect its signal to my ButtonReceived slot but it never fires.在主窗口类构造函数中,我创建了一个鼠标类并将其信号连接到我的 ButtonReceived 插槽,但它从不触发。 What am I doing wrong?我究竟做错了什么?

main.cpp主文件

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    w.show();
    return a.exec();
}

mainwindow.h主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QObject>
#include "mouse.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    Mouse *m;
private slots:
    void ButtonReceived(const QString &);

};

#endif // MAINWINDOW_H

mainwindow.cpp主窗口.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "mouse.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{

    qDebug() << "MainWindow created";
    ui->setupUi(this);

    m = new Mouse;     // new Mouse class then connect its signal to this class's slot but it's not working
    bool ctn = connect(p,SIGNAL(readyReadStandardOutput()),this,SLOT(ButtonPressed()));
    qDebug() << ctn;  //true

}
MainWindow::~MainWindow()
{
    qDebug() << "MainWindow destroyed";
    delete m;
    delete ui;
}

void MainWindow::ButtonReceived(const QString &btn)
{
   qDebug() << "ButtonReceived";
}

mouse.h鼠标.h

#ifndef MOUSE_H
#define MOUSE_H

#include <QObject>
#include <QProcess>

using namespace std;

class Mouse : public QObject
{
    Q_OBJECT

public:
    explicit Mouse(QObject *parent = nullptr);
    ~Mouse();

private:
    QProcess *p;
    QThread *t;

private slots:
    void ButtonPressed();   // when evtest has output


signals:
    void ButtonPressedSignal(const QString&);


};

#endif // MOUSE_H

mouse.cpp鼠标.cpp

#include "mouse.h"
#include <QDebug>
#include <QThread>

Mouse::Mouse(QObject *parent) : QObject{parent}
{
    t = new QThread;           // new Thread
    p = new QProcess();        // new Process
    p->moveToThread(t);        // run Process on Thread

    // connect process output to slot ButtonPressed
    connect(p,SIGNAL(readyReadStandardOutput()),this,SLOT(ButtonPressed()));

    // run evtest with mouse's device ID
    // TODO: find mouse ID with mouse name since ID can change
    p->startCommand("sudo evtest /dev/input/event24");
    p->waitForFinished(-1);
    t->start();
}

Mouse::~Mouse()
{
    delete p;
    delete t;
}


void Mouse::ButtonPressed()
{
    QString out = p->readAllStandardOutput();
    if (out.indexOf("EV_KEY") > 0 && out.indexOf("value 1") > 0)
    {
        qDebug() << out;
/*
* Here I get output from the process like
"Event: time 1658273579.607487, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90001\nEvent: time 1658273579.607487, type 1 (EV_KEY), code 272 (BTN_LEFT), value 1\n"
"Event: time 1658273581.285479, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90002\nEvent: time 1658273581.285479, type 1 (EV_KEY), code 273 (BTN_RIGHT), value 1\nEvent: time 1658273581.285479, -------------- SYN_REPORT ------------\n"
"Event: time 1658273585.465477, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90003\nEvent: time 1658273585.465477, type 1 (EV_KEY), code 274 (BTN_MIDDLE), value 1\nEvent: time 1658273585.465477, -------------- SYN_REPORT ------------\n"
"Event: time 1658273587.670479, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90004\nEvent: time 1658273587.670479, type 1 (EV_KEY), code 275 (BTN_SIDE), value 1\nEvent: time 1658273587.670479, -------------- SYN_REPORT ------------\n"
"Event: time 1658273588.404471, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90005\nEvent: time 1658273588.404471, type 1 (EV_KEY), code 276 (BTN_EXTRA), value 1\nEvent: time 1658273588.404471, -------------- SYN_REPORT ------------\n"
"Event: time 1658273591.211491, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90006\nEvent: time 1658273591.211491, type 1 (EV_KEY), code 277 (BTN_FORWARD), value 1\nEvent: time 1658273591.211491, -------------- SYN_REPORT ------------\n"
"Event: time 1658273591.852480, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90007\nEvent: time 1658273591.852480, type 1 (EV_KEY), code 278 (BTN_BACK), value 1\nEvent: time 1658273591.852480, -------------- SYN_REPORT ------------\n"
"Event: time 1658273593.851492, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90009\nEvent: time 1658273593.851492, type 1 (EV_KEY), code 280 (?), value 1\nEvent: time 1658273593.851492, -------------- SYN_REPORT ------------\n"
"Event: time 1658273594.704493, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90008\nEvent: time 1658273594.704493, type 1 (EV_KEY), code 279 (BTN_TASK), value 1\nEvent: time 1658273594.704493, -------------- SYN_REPORT ------------\n"



so that works
now I want to emit a signal to my mainwindow



*/
        if (out.indexOf("BTN_LEFT") > 0)
            emit ButtonPressedSignal("Left");
        if (out.indexOf("BTN_RIGHT") > 0)
            emit ButtonPressedSignal("Right");
        if (out.indexOf("BTN_EXTRA") > 0)
            emit ButtonPressedSignal("G4");
        if (out.indexOf("BTN_SIDE") > 0)
            emit ButtonPressedSignal("G3");
        if (out.indexOf("BTN_BACK") > 0)
            emit ButtonPressedSignal("G6");
        if (out.indexOf("BTN_FORWARD") > 0)
            emit ButtonPressedSignal("G5");
        if (out.indexOf("BTN_TASK") > 0)
            emit ButtonPressedSignal("G7");
        if (out.indexOf("?") > 0)
            emit ButtonPressedSignal("G8");
        if (out.indexOf("BTN_MIDDLE") > 0)
            emit ButtonPressedSignal("Middle");
    }
}

Is this project compiled correctly?这个项目编译正确吗? The variable p is declared as private in mouse class, but how can it be called in MainWindow class?变量 p 在鼠标类中被声明为私有,但是在 MainWindow 类中如何调用它呢?

I think this code will help you.我认为这段代码会对你有所帮助。

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new ui::MainWindow)
 {
    ...
    m = new Mouse;
    bool ctn = connect(m,SIGNAL(ButtonPressedSignal(const QString&)),this,SLOT(ButtonReceived(const QString&)));
    ... 
 }

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

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