Question

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.

Was it helpful?

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.

OTHER TIPS

I cannot reproduce your problem. What you should to is make checkNumber a member of a class, and do the same for your msg object.

Here is a working example:

mainwinodw.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QErrorMessage>
#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void showErrorMsg();

private:
    Ui::MainWindow *ui;
    QErrorMessage msg;
    QTimer timer;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&timer, SIGNAL(timeout()), this, SLOT(showErrorMsg()));
    timer.start(3000); // we use a timer to show an error message
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showErrorMsg()
{
    msg.showMessage("My message");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top