0

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.

liquid.pizza
  • 505
  • 1
  • 10
  • 26
  • 1
    In your code snippet, your `msg` object will be destroyed once it goes out of scope. It should just flash on your screen. Are you sure you have enough time to uncheck the checkbox? Other than that, I cannot reproduce your problem. Perhaps [SSCCE](http://sscce.org) would help. – thuga Jun 05 '14 at 12:31
  • The `QErrorMessage` stays on screen, till I click `OK`. And as I wrote, the next time it appears, the checkbox is unchecked – liquid.pizza Jun 05 '14 at 12:35
  • 1
    That is not what should happen, at least not with the code snippet you provided. [`QErrorMessage::showMessage`](http://qt-project.org/doc/qt-5/qerrormessage.html#showMessage) returns immediately. If it stays on your screen, then you must be calling `QDialog::exec` somewhere. – thuga Jun 05 '14 at 12:40
  • I just reverted all files and tested this example again. Now the `QErrorMessage` is flashing, as you told. – liquid.pizza Jun 05 '14 at 12:41
  • Now if you make `checkNumber` a member of a class and do the same for `msg`, does it still show when you uncheck the check box? – thuga Jun 05 '14 at 12:43
  • I made `msg` a member of the same class as `checkNumber(...)` and added a `msg.exec();` below `msg.showMessage(...);` but the message keeps on appearing... – liquid.pizza Jun 05 '14 at 12:51
  • Do **not** add `msg.exec`. That will make the dialog appear every time. – thuga Jun 05 '14 at 13:01

2 Answers2

1

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.

liquid.pizza
  • 505
  • 1
  • 10
  • 26
  • 2
    You can filter the messages out by type too. See [`QErrorMessage::QErrorMessage::showMessage(const QString & message, const QString & type)`](http://qt-project.org/doc/qt-5/qerrormessage.html#showMessage-2). This seems ideal in your case. – thuga Jun 25 '14 at 13:27
0

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");
}
thuga
  • 12,601
  • 42
  • 52
  • I just tried a fast test, but I got some library-linking problems. I will let you know, when I got it working or solve this question. But I made ``msg` and `checkNumber()` both a member. Thanks so far! – liquid.pizza Jun 10 '14 at 08:34