Search code examples
c++qtcheckboxqmessagebox

QErrorMessage keeps on appearing


I count some entries and want to emit a message, when the user has many entries, since it will be confusing.

Nevertheless the other hand the user should have the option to disable this warning.

That's why I wanted to use a QErrorMessage.

But my QErrorMessage kept on appearing even when it should not (/when the checkbox is unchecked).

This is the shortest code I wrote:

void checkNumber(int const &n)
{
    if(n > CriticalNumber)
    {
        QErrorMessage msg(this);
        msg.showMessage("too much!");
    }
}

Did I forget anything?

The funny thing is, after you once unchecked the checkbox, it is unchecked in every next call...

// edit:

This error happens even when the QErrorMessage is a member of my class and not initialised in every call.

// edit2:

By now I am pretty sure, that this error only occurs, when I use QString::arg. I did not use this in the example code, since I thought this would make no difference. So the example should look like this:

void showError(int const &n, QErrorMessage *msg)
{
    msg->showMessage(tr("%1 is too big").arg(n));
}

showError() is called in the previous if-statement.


Solution

  • I solved this problem (specified in edit2).

    The problem is, that the QErrorMessage saves all the QStringsthat should not be shown again.

    Since my arg() creates nearly every time a new QStringthe QErrorMessageis shown each time it is changed.

    Example:

    QErrorMessage msg(this);
    showError(1, msg);
    showError(2, msg);
    showError(1, msg);
    

    The first showError(1, msg) will show the QErrorMessage. If you uncheck the checkbox, showError(2, msg) will be shown (because a different QString is shown), but not showError(1, msg) (since the shown QString is the same as the first one.