简体   繁体   中英

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. ButtonReceived in the mouse class receives the process's output line by line as I press buttons. 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. 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

#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

#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

#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

#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?

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

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