2

每当我的独立线程在特定的 .txt 文件中遇到“alert1”一词时,我想显示一条错误消息。但是我在 mythread.cpp 文件中的 monitorForAlerts() 中得到了上述错误。如果我将它放在 dialog.cpp 中,则预期会执行该行。所以我想这是由于这个对象的非继承。你能告诉我如何解决给定代码的这个错误吗?

这是代码:dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtCore>
#include "mythread.h"
namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

public slots:

private:
    Ui::Dialog *ui;

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
};

#endif // DIALOG_H

我的线程.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QtCore>
#include <QDebug>
#include <QFile>
#include <Windows.h>
#include <QMessageBox>
#include <QTimer>
#define ALERTS_MESSAGE_STORAGE_PATH "E:\\QT1\\simpleGUIThread2\\simpleGUIThread2\\usbAlert.txt"
#define TIMER_VALUE                      500
class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = 0);
    void run();
    QString name;
    void monitorForAlerts();
    int exec();

public slots:

signals:
    void testSignal(QString message);

public slots:

};

#endif // MYTHREAD_H

对话框.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    ui->label->show();
}

void Dialog::on_pushButton_2_clicked()
{
    ui->label->hide();
}

我的线程.cpp

#include "mythread.h"
#include "dialog.h"
MyThread::MyThread(QObject *parent) :
    QThread(parent)
{
}

void MyThread::run()
{
    exec();
}

int MyThread::exec()
{
    while(1)
    {
        monitorForAlerts();
        emit(testSignal("hello world!!"));
        sleep(1);
    }
}

void MyThread::monitorForAlerts()
{
    QString response = ALERTS_MESSAGE_STORAGE_PATH;
    QFile resp(response);
    resp.open(QIODevice::WriteOnly);
    resp.close();
    QFile resp1(response);
    char buf[121];
    char buf1[] = "alert1";
    char buf2[] = "alert2";

    resp1.open(QIODevice::ReadOnly);
    while(resp1.size() == 0)
    {
        Sleep(3000);
    }
    qint64 lineLength = resp1.readLine(buf, sizeof(buf));
    resp1.close();
    if(strcmp(buf,buf1) == 0)
    {
        QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
        qDebug()<<"warning 1!!";
        QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
    }
    if(strcmp(buf,buf2) == 0)
    {
        QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
        qDebug()<<"warning 2!!";
        QMessageBox::critical(this,tr("ERROR"),tr("The camera position has been moved or an object is obscuring its view.\nPlease check the device.\n"));
    }
}

主文件

#include "dialog.h"
#include <QApplication>
#include "mythread.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MyThread mThread1;
    mThread1.name = "mThread1";
    mThread1.start();

    Dialog w;
    w.show();

    return a.exec();
}

最新更新* * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * * * * ** * ** * ** * ** * ** * ****

嗨 Zlatomir,我选择采纳你的第一个建议。我创建了一个线程将发出的信号并将其连接到 QDialog 的插槽。请让我知道我的理解是否正确,因为我不知道在哪里实现 connect(),因为信号在 mythread.h 中声明,而 slot 在 dialog.h 中声明。connect 的连接类型参数是 Qt::QueuedConnection,因此来自不同于主线程的另一个线程的 gui 元素。未创建。这个说法正确吗?我在哪里放置这个?

connect( mThread, SIGNAL(alertSignal(QString)), this, SLOT(alertSlot(QString)), Qt::QueuedConnection);

我的线程.h

//....
signals:
void alertSignal(QString message);
//....

对话框.h

//....
public slots:
void alertSlot(QString message);
//....

我的线程.cpp

//....
    if(strcmp(buf,buf1) == 0)
    {
        QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
        qDebug()<<"warning 1!!";
        emit(alertSignal("alert1"));
    }
    else if(strcmp(buf,buf2) == 0)
    {
        QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
        qDebug()<<"warning 2!!";
        emit(alertSignal("alert2"));
    }

对话框.cpp

void Dialog::alertSlot(QString message)
{
    if(strcmp(message, "alert1"))
        QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
    else if(strcmp(message, "alert2"))
        QMessageBox::critical(this,tr("ERROR"),tr("The camera position has been moved or an object is obscuring its view.\nPlease check the device.\n"));
}

现在,如果这是正确的,我如何实现 connect() 以及在哪个文件中?

4

1 回答 1

6

第一个参数是问题,在您的情况下this不是一个好的参数,因为有this一个指向MyThread实例的指针,并且MyThread不是 QWidget(不是从 QWidget 派生的)。

为了解决这个问题,您可以在主窗口中显示QMessageBox::critical一个插槽(Dialog代码中的类,在那里传递作为 QWidget 的主窗口实例)并将该插槽与您从线程发出的信号连接,确保connect连接类型参数是,因此您不会尝试从不同于主线程的另一个线程创建 gui 元素。Qt::QueuedConnection

另一种选择是在启动第二个线程之前验证数据并告诉用户他需要提供正确的文件。

LE:还请查看QThread的文档以了解使用该类的推荐方式,现在建议不要从 QThread 派生。

LE2 - 对更新的回答 可以在任何你想要连接的两个实例的地方进行连接,在你的情况下main.cpp是连接这些实例的好地方(不要忘记完全限定连接的名称:)QObject::connect

//...
    MyThread mThread1;
    mThread1.name = "mThread1";
    mThread1.start();

    Dialog w;
    QObject::connect( &mThread1, SIGNAL(alertSignal(QString)), &w, SLOT(alertSlot(QString)), Qt::QueuedConnection);
    w.show();
//...
于 2014-03-12T06:33:23.083 回答