qt - 'QMessageBox::critical' : none of the 4 overloads could convert all the argument types -
i want display error message whenever independent thread encounters word "alert1" in specific .txt file. above error inside monitorforalerts() inside mythread.cpp file. line expectedly executes if place inside dialog.cpp. guess due non-inheritance of object. can please advise me how solve error given code?
here code: 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 object obscuring view.\nplease check 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(); } latest update*********************************************************************
hi zlatomir, choose take 1st advice. have created signal thread emit , connect slot qdialog. please let me know if understanding correct, because not know implement connect(), since signal declared in mythread.h , slot in dialog.h. connection type argument connect qt::queuedconnection, gui elements thread different main-thread. not created. statement correct? , place this?
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 object obscuring view.\nplease check device.\n")); } now if correct, how implement connect() , in file?
the first argument problem, in case this not argument, because there this pointer mythread instance, , mythread not qwidget (is not derived qwidget).
to solve can show qmessagebox::critical slot in mainwindow (the dialog class in code, there pass instance of main-window qwidget) , connect slot signal emit thread, make sure connection type argument connect qt::queuedconnection, don't try create gui elements thread different main-thread.
another option validate data before start second thread , tell user needs provide right files.
le: check qthread's documentation recommended way use class, it's recommended not derive qthread.
le2 - answer update connect can made ever can have 2 instances want connect, in case main.cpp place connect (don't forget qualify name connect: 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(); //...
Comments
Post a Comment