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

时间:2014-03-12 06:10:18

标签: qt arguments overloading qmessagebox

每当我的独立线程遇到单词" alert1"时,我想显示一条错误消息。在特定的.txt文件中。但是我在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

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

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

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

最新更新* * ** * ** * ** * ** < EM> * ** * ** * ** * ** * ** < EM> * ** * ** * ** * ** * ** < EM> * ** * ** * ** * ** * ** < EM> * ** * ** * ****

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

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

mythread.h

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

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

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

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

1 个答案:

答案 0 :(得分:6)

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

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

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

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

LE2 - 回答更新 只要您可以拥有要连接的两个实例,就可以进行连接。在您的情况下,main.cpp是连接它们的好地方(不要忘记完全限定连接的名称:{{1 }}):

QObject::connect