'QMessageBox :: critical':4个重载中没有一个可以转换所有参数类型

ven*_*914 2 qt arguments overloading qmessagebox

每当我的独立线程在特定.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
Run Code Online (Sandbox Code Playgroud)

mythread.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
Run Code Online (Sandbox Code Playgroud)

dialog.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();
}
Run Code Online (Sandbox Code Playgroud)

mythread.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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#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();
}
Run Code Online (Sandbox Code Playgroud)

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

嗨Zlatomir,我选择接受你的第一个建议.我创建了一个信号,线程将发出并将其连接到QDialog的插槽.如果我的理解是正确的,请告诉我,因为我不知道在哪里实现connect(),因为信号在mythread.h和dialog.h中的插槽中声明.connect的连接类型参数是Qt :: QueuedConnection,因此来自另一个线程的gui元素与主线程不同.没有创建.这个陈述是否正确?我在哪里放这个?

connect( mThread, SIGNAL(alertSignal(QString)), this, SLOT(alertSlot(QString)), Qt::QueuedConnection);
Run Code Online (Sandbox Code Playgroud)

mythread.h

//....
signals:
void alertSignal(QString message);
//....
Run Code Online (Sandbox Code Playgroud)

dialog.h

//....
public slots:
void alertSlot(QString message);
//....
Run Code Online (Sandbox Code Playgroud)

mythread.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"));
    }
Run Code Online (Sandbox Code Playgroud)

dialog.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"));
}
Run Code Online (Sandbox Code Playgroud)

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

Zla*_*mir 6

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

要解决这个问题,您可以QMessageBox::criticalDialog主窗口中显示一个插槽(代码中的类,在那里传递主窗口的实例,即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();
//...
Run Code Online (Sandbox Code Playgroud)