【发布时间】:2022-08-03 16:35:29
【问题描述】:
鼠标类在新线程中创建进程并运行evtest命令监听 我的鼠标按钮按下。当我按下按钮时,鼠标类中的 ButtonReceived 逐行接收进程的输出。我添加了一些我得到的输出作为评论。现在我正在尝试根据输出向主窗口类发出信号。在主窗口类构造函数中,我创建了一个鼠标类并将其信号连接到我的 ButtonReceived 插槽,但它从不触发。我究竟做错了什么?
主文件
#include \"mainwindow.h\"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.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
主窗口.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\";
}
鼠标.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
鼠标.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\");
}
}
--
CM-1207 我不知道为什么它说 p 但它在我的源代码中说 m ,但我把它改成了你所拥有的
我之前试过这个,但是在连接参数中添加了 const 和 & 但我得到了这个错误
变化
在 mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
qDebug() << \"MainWindow created\";
ui->setupUi(this);
m = new Mouse;
bool ctn = connect(m,SIGNAL(ButtonPressedSignal(const QString&)),this,SLOT(ButtonReceived(const QString&)));
qDebug() << \"MainWindow connection: \" << ctn;
}
在鼠标.cpp
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
bool ctn = connect(p,SIGNAL(readyReadStandardOutput()),this,SLOT(ButtonPressed()));
qDebug() << \"Mouse connection: \" << ctn;
// 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();
}
我认为我的错误出在鼠标类中,但我不确定如何解决。我永远有 p->waitForFinished ,因为每次我永远按下按钮或直到我终止它时,进程都会运行并输出。这对我来说似乎是必要的,因为没有 p->waitForFinished 永远 QProcess 会超时并死掉。但是,这会阻塞主窗口。我将 QProcess 放在一个线程中,认为它会永远异步运行,但它仍然会阻止 mainwindow 甚至显示。