简体   繁体   中英

Error when display an error message box. How can i display it?

I try to display an error message box but an error has been occurred. Can anyone help to check my coding?

void smtp_listener::pop3Stat(QString reply)
{

    print_D(FUNC);
    if(reply.contains("+OK"))
    {
        *t << "stat" <<"\r\n";
        t->flush();

        setState(POP3_Read);
    }
    else
    {
        print_E("ERROR :"+reply,FUNC,__LINE__);
        QMessageBox msgBox;
        msgBox.setWindowTitle("Error");
        msgBox.setText("Please check it.");
        msgBox.exec();
        quitConn();
        setState(POP3_Quit);
    }
}

Error occurred as below:

Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
ASSERT failure in QWidget: "Widgets must be created in the GUI thread.", file kernel\qwidget.cpp, line 1118

Problem seems to be that your smtp_listener is being executed in another thread. A fairly simple Qt solution would be to not try displaying the error from smtp_listener . Instead give your smtp_listener an error signal. Connect this signal to a slot in your form which takes care of displaying the error. Qt's signal system will queue the signal for execution in the gui thread.

Errors handling inside internal class methods is not a good practice.

The @Eelke solution is good if your class smtp_listener extends from QObject. But if you have a "clear" class (without any Qt relations, from external library, for example) you should throw an exception or return an error value (or an object representing the error state).

The approach provides you possibilities to organize the errors handling in one place (GUI class in your case). It is a good advantage both for you and other programmer who reads your code.

By the way you can combine @Eelke's answer with mine using the code like this:

function returning an error:

int smtp_listener::pop3Stat(QString reply)
{
    if(reply.contains("+OK"))
    {
        *t << "stat" <<"\r\n";
        t->flush();

        setState(POP3_Read);
        return 0;  // success
    }

    return 1;      // return not null value with error
}

Code to handle the error:

/// slot to handle an error
/// don't forget to connect errorSignal with it
void MainWidnow::errorSignalSlot(int status)
{
    QMessageBox::critical(this, "Error", "Error code: " + QString::number(status));
}

void MainWidnow::button_onClick()
{
    int status = listener.pop3Stat(reply);

    if (status != 0) // not null value means error
    {
        emit(errorSignal(status)); // emit the signal with error code
    }
    else
    {
        qDebug() << "success";
    }
}

widgets must created in main thread. I think you can pass any messages to main thread with signal/slot or event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM