Results 1 to 7 of 7

Thread: 'QMessageBox::critical' : none of the 4 overloads could convert all the argument type

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default 'QMessageBox::critical' : none of the 4 overloads could convert all the argument type

    I want to display an error message whenever my independent thread encounters the word "alert1" in a specific .txt file. But I get the above error inside the monitorForAlerts() inside mythread.cpp file. The line expectedly executes if I were to place it inside dialog.cpp. So I guess this is due to non-inheritance of this object. Can you please advise me how to solve this error for the given code?

    Here is the code:
    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtCore>
    6. #include "mythread.h"
    7. namespace Ui {
    8. class Dialog;
    9. }
    10.  
    11. class Dialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Dialog(QWidget *parent = 0);
    17. ~Dialog();
    18.  
    19. public slots:
    20.  
    21. private:
    22. Ui::Dialog *ui;
    23.  
    24. private slots:
    25. void on_pushButton_clicked();
    26. void on_pushButton_2_clicked();
    27. };
    28.  
    29. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QtCore>
    6. #include <QDebug>
    7. #include <QFile>
    8. #include <Windows.h>
    9. #include <QMessageBox>
    10. #include <QTimer>
    11. #define ALERTS_MESSAGE_STORAGE_PATH "E:\\QT1\\simpleGUIThread2\\simpleGUIThread2\\usbAlert.txt"
    12. #define TIMER_VALUE 500
    13. class MyThread : public QThread
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit MyThread(QObject *parent = 0);
    18. void run();
    19. QString name;
    20. void monitorForAlerts();
    21. int exec();
    22.  
    23. public slots:
    24.  
    25. signals:
    26. void testSignal(QString message);
    27.  
    28. public slots:
    29.  
    30. };
    31.  
    32. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Dialog::~Dialog()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Dialog::on_pushButton_clicked()
    17. {
    18. ui->label->show();
    19. }
    20.  
    21. void Dialog::on_pushButton_2_clicked()
    22. {
    23. ui->label->hide();
    24. }
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include "dialog.h"
    3. MyThread::MyThread(QObject *parent) :
    4. QThread(parent)
    5. {
    6. }
    7.  
    8. void MyThread::run()
    9. {
    10. exec();
    11. }
    12.  
    13. int MyThread::exec()
    14. {
    15. while(1)
    16. {
    17. monitorForAlerts();
    18. emit(testSignal("hello world!!"));
    19. sleep(1);
    20. }
    21. }
    22.  
    23. void MyThread::monitorForAlerts()
    24. {
    25. QString response = ALERTS_MESSAGE_STORAGE_PATH;
    26. QFile resp(response);
    27. resp.open(QIODevice::WriteOnly);
    28. resp.close();
    29. QFile resp1(response);
    30. char buf[121];
    31. char buf1[] = "alert1";
    32. char buf2[] = "alert2";
    33.  
    34. resp1.open(QIODevice::ReadOnly);
    35. while(resp1.size() == 0)
    36. {
    37. Sleep(3000);
    38. }
    39. qint64 lineLength = resp1.readLine(buf, sizeof(buf));
    40. resp1.close();
    41. if(strcmp(buf,buf1) == 0)
    42. {
    43. QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
    44. qDebug()<<"warning 1!!";
    45. QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
    46. }
    47. if(strcmp(buf,buf2) == 0)
    48. {
    49. QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
    50. qDebug()<<"warning 2!!";
    51. QMessageBox::critical(this,tr("ERROR"),tr("The camera position has been moved or an object is obscuring its view.\nPlease check the device.\n"));
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include <QApplication>
    3. #include "mythread.h"
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. MyThread mThread1;
    9. mThread1.name = "mThread1";
    10. mThread1.start();
    11.  
    12. Dialog w;
    13. w.show();
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 'QMessageBox::critical' : none of the 4 overloads could convert all the argument

    You cannot access UI classes from a secondary thread. Only the main thread, the one running QApplication::exec() is allowed to do that.

    One option that you have is to let the thread emit a signal that you connect to your dialog class and let the dialog class show the message box.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 'QMessageBox::critical' : none of the 4 overloads could convert all the argument

    Hi anda_skoa,
    I took your advice. I have created a signal that the thread will emit and connect it to a slot for QDialog. Please let me know if my understanding is correct, because I do not know where to implement the connect(), since the signal is declared in mythread.h and the slot in dialog.h. The connection type argument for connect is Qt::QueuedConnection, so that gui elements from another thread different than main-thread are NOT created. Is this statement correct? and where do I place this?
    Qt Code:
    1. connect( mThread, SIGNAL(alertSignal(QString)), this, SLOT(alertSlot(QString)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    mythread.h
    Qt Code:
    1. //....
    2. signals:
    3. void alertSignal(QString message);
    4. //....
    To copy to clipboard, switch view to plain text mode 
    dialog.h
    Qt Code:
    1. //....
    2. public slots:
    3. void alertSlot(QString message);
    4. //....
    To copy to clipboard, switch view to plain text mode 
    mythread.cpp
    Qt Code:
    1. //....
    2. if(strcmp(buf,buf1) == 0)
    3. {
    4. QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
    5. qDebug()<<"warning 1!!";
    6. emit(alertSignal("alert1"));
    7. }
    8. else if(strcmp(buf,buf2) == 0)
    9. {
    10. QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
    11. qDebug()<<"warning 2!!";
    12. emit(alertSignal("alert2"));
    13. }
    To copy to clipboard, switch view to plain text mode 
    dialog.cpp
    Qt Code:
    1. //...
    2. void Dialog::alertSlot(QString message)
    3. {
    4. if(strcmp(message, "alert1"))
    5. QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
    6. else if(strcmp(message, "alert2"))
    7. QMessageBox::critical(this,tr("ERROR"),tr("The camera position has been moved or an object is obscuring its view.\nPlease check the device.\n"));
    8. }
    To copy to clipboard, switch view to plain text mode 
    Now if this were correct, how do i implement the connect() and in which file?
    Last edited by Sai Kamat; 12th March 2014 at 09:42.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 'QMessageBox::critical' : none of the 4 overloads could convert all the argument

    This looks reasonable.

    You can do the connect in main()
    Qt Code:
    1. QObject::connect( &mThread, SIGNAL(alertSignal(QString)), &w, SLOT(alertSlot(QString)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'QMessageBox::critical' : none of the 4 overloads could convert all the argument

    And the last parameter is not needed because its default value is Qt::AutoConnection which means that for objects from different threads it is Qt::QueuedConnection.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 'QMessageBox::critical' : none of the 4 overloads could convert all the argument

    Quote Originally Posted by Lesiok View Post
    And the last parameter is not needed because its default value is Qt::AutoConnection which means that for objects from different threads it is Qt::QueuedConnection.
    True, however in this case both objects belong to the same thread. It will still work with auto connection because another check is which thread is executing the emit and that is a different thread then the owner of the reciever object.

    Doesn't hurt to explicitly specify Qt::QueuedConnection though.

    Cheers,
    _

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: 'QMessageBox::critical' : none of the 4 overloads could convert all the argument

    GUI objects in threads aside, the original error message:
    QMessageBox::critical' : none of the 4 overloads could convert all the argument
    is caused by:
    Qt Code:
    1. QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
    To copy to clipboard, switch view to plain text mode 
    because the first argument, this, is not a pointer to a QWidget and cannot be converted to one.

Similar Threads

  1. How to convert image data type to plain text and back ?
    By aircraftstories in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2011, 16:00
  2. Why? Invalid type argument of unary *
    By progman in forum Newbie
    Replies: 4
    Last Post: 21st March 2011, 19:31
  3. unable to convert from one type to another
    By sachinmcajnu in forum Qt Programming
    Replies: 3
    Last Post: 16th March 2011, 00:34
  4. Replies: 0
    Last Post: 19th March 2009, 13:51
  5. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.