Pages

A modal dialog

We want to improve our application, giving the user a way to insert a text in the main window background, and even chosing if the text should be showed in a normal font or in italic.

A dialog is nothing more than a window used for letting the user a chance to input some values required by the application, and a modal dialog is just a dialog that takes the control of the application. That is, when we open a modal dialog we have no chance of going on doing some other stuff in our application. We have to close it (confirming or canceling our input) before continuing.

We have already seen how to design a dialog with Qt Designer, so I'm not about to say much on the matter. Just assume we have a dialog, DlgMessage, that shows us a line edit, to input the message, a check box, to specify if we want the text displayed in italic or not, and the standard OK/Cancel buttons.

The only slightly interesting stuff on our new DlgMessage class is that it would have a couple of methods that the user code would use to retrive the useful values.

Here is how looks the class definition:

class DlgMessage : public QDialog
{
Q_OBJECT
public:
explicit DlgMessage(QWidget *parent = 0);
~DlgMessage();

QString getMessage();
bool getItalic();
protected:
void changeEvent(QEvent *e);

private:
Ui::DlgMessage *ui;
};

All the code above is generated by Qt Creator (good guy) but the two methods getMessage()and getItalic(), that we implement in this way:

QString DlgMessage::getMessage()
{
return ui->lineEdit->text();
}

bool DlgMessage::getItalic()
{
return ui->checkBox->isChecked();
}

Even though I didn't said much about the DlgMessage implementation details, I'd say this piece of code is quite straightforward.

Given that, the changes we have to introduce in WinMain are minimal and well localized:

void WinMain::newMessage()
{
DlgMessage dlgMessage(this); // 1.

if(dlgMessage.exec()) // 2.
{
QLabel* pLb = dynamic_cast<QLabel*>(centralWidget());
if(pLb == 0)
QMessageBox::information(this, this->windowTitle(), "No label available", QMessageBox::Ok);
else // 3.
{
QFont f(pLb->font());
f.setItalic(dlgMessage.getItalic());
pLb->setFont(f); // 4.

pLb->setText(dlgMessage.getMessage()); // 5.
}
}
}

The newMessage() slot contains all the required changes.
1. We create an instance of our dialog. Since this dialog has a very short life - at the end of this function there is no interest in keeping it alive - we put it on the stack.
2. Calling exec() we are specifying that we want to executing it in modal way. We'll see in another post how to make available to the user a non-modal dialog.
3. After we have ensured that the central widget is what we actually expect, we are about to do the real job.
4. We create a local copy of the font used by the label, set its italic attribute accordingly to the user requirement - notice the call to the DlgMessage::getItalic() function - and say to the label to use this font instead of the old one.
5. We retrieve the message as specified by the user in the DlgMessage, using its getMessage() function, and say the label to use it as its text.

No comments:

Post a Comment