简体   繁体   English

显示错误消息框时出错。 如何显示?

[英]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. 问题似乎是您的smtp_listener正在另一个线程中执行。 A fairly simple Qt solution would be to not try displaying the error from smtp_listener . 一个相当简单的Qt解决方案是不尝试显示smtp_listener的错误。 Instead give your smtp_listener an error signal. 而是给您的smtp_listener一个错误信号。 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. Qt的信号系统会将信号排队,以便在gui线程中执行。

Errors handling inside internal class methods is not a good practice. 内部类方法内部的错误处理不是一个好习惯。

The @Eelke solution is good if your class smtp_listener extends from QObject. 如果您的类smtp_listener从QObject扩展的,则@Eelke解决方案很好。 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). 但是,如果您具有“清除”类(例如,没有任何Qt关系,例如来自外部库),则应引发异常返回错误值 (或表示错误状态的对象)。

The approach provides you possibilities to organize the errors handling in one place (GUI class in your case). 该方法为您提供了在一个地方组织错误处理的可能性(您的情况下为GUI类)。 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: 顺便说一下,您可以使用以下代码将@Eelke的答案与我的结合起来:

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. 我认为您可以通过信号/插槽或事件将任何消息传递到主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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