From QDialog to statusBar

In the previous post I have used a dialog in modeless way. Here we see a modal dialog in action.

I want to let the user input a message he wants to display on the main window status bar, specifying also how long he'd like such a message being visible.

To do that I have slightly modified the dialog of the previous post, adding a QLineEdit element, named lineEdit, to let the user input the message to be displayed, and a QSpinBox, spinBox, to let him specify the requested time. There is not much to say about doing this stuff with the Qt Creator Design module, maybe it has some interest saying that I have set the spinBox property maximum to 10, so that the interval is now [0..10].

I have changed the DlgHello class declaration adding a couple of public methods like this:
class DlgHello : public QDialog
explicit DlgHello(QWidget *parent = 0);
QString getMessage();
int getDelay();
// ...

And here is their definition:
QString DlgHello::getMessage()
return ui->lineEdit->text();

int DlgHello::getDelay()
return ui->spinBox->value() * 1000;

Not much to say about getMessage(), but getDelay() is a bit more interesting since it returns the spinBox value multiplied by one thousand. The reason for that is that the status bar expects the time expressed in milliseconds, while in the spin box I expressed the value in seconds.

Here is the change in the main window:
void MainWindow::sayHello()
DlgHello dlg(this);
ui->statusBar->showMessage(dlg.getMessage(), dlg.getDelay());

Now we work on a DlgHello local variable defined in the method called as slot when the triggered() signal is emitted for the Say Hello action. Calling exec() on it makes us checking if the user click the OK button or not. In case of OK we show the message in the status bar with the specified delay. Notice that if user specifies 0 as delay, the message is shown as long as possible, until another element in the window requires to change the message on the status bar.

No comments:

Post a Comment