[英]Signal and Slot and emitting signal to mainwindow
鼠標類在一個新線程中創建一個進程並運行 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");
}
}
這個項目編譯正確嗎? 變量 p 在鼠標類中被聲明為私有,但是在 MainWindow 類中如何調用它呢?
我認為這段代碼會對你有所幫助。
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.