【问题标题】:'QMessageBox::critical' : none of the 4 overloads could convert all the argument types'QMessageBox::critical' : 4 个重载都不能转换所有参数类型
【发布时间】:2014-04-16 02:31:23
【问题描述】:

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

代码如下: 对话框.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

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

对话框.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();
}

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"));
    }
}

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();
}

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

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

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

mythread.h

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

对话框.h

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

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"));
    }

对话框.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() 以及在哪个文件中?

【问题讨论】:

    标签: qt arguments overloading qmessagebox


    【解决方案1】:

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

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

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

    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();
    //...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2015-12-12
      相关资源
      最近更新 更多