Lesson 3 Part Dialog Subclasing with Q Designer
By Old Crow
This article deals with
using QtDesigner.It allows the programming to rapid prototying of widgets and
dialogs in your program without having to recompile. You can see them in action
and check out the code.
I slighty modify the example given in C++ Gui Programming with Qt to popup a message box stating the spreadsheet cell you typed in.
First lets start and build the widget.
Start Qt Designer. The New form dialog will pop up. Create a dialog without windows form. Your screen with the unsized form will vary and look like.the pic labeled qt form.
You will need two push buttons , one edit field and a vertical spacer. The following is a reshash from the book :
1.Click on the text label. Set the objectName property to label. Next set the text property to "&Cell location:"
2. Click the line editor. Set the objectName property to lineEdit
3. Click the first button..Set theobjectName property to okButton, In the propery box set teh enabled property to false. The text property to Ok and the default propertyto True
4. Click the second button. Set theproperty to cancelButton. The text property to Cancel.
5. Click on the form itself and .set the objectName to GoToCellDialog and windowTitle to Go to Cell
Your project directory should look like the pic labeled proj windows. Make the dialog box look the following the pic labeled widget
Lets work on the functionality. Rune a Qmake on your project. It will hopefully generate the the include file UI_GoToCellDialog,h
Now lets get to work and fix up our project.
Now go to gotocelldialog.h and add the following function to the private slots: int say_message();
The original code for gototcelldialog.cpp looks like;
#include <QtGui>
#include
"GotToCellDialog.h"
GoToCellDialog::GoToCellDialog(QWidget *parent)
:
QDialog(parent)
{
setupUi(this);
QRegExp
regExp("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new
QRegExpValidator(regExp, this));
connect(okButton, SIGNAL(clicked()), this,
SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this,
SLOT(reject()));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
In order to have a popup message box change :
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
to
connect(okButton, SIGNAL(presed()), this, SLOT(say_message()));
and add this:
int GoToCellDialog::say_message()
{
QMessageBox msgbox;
msgbox.setText(“You typed key:“).
msgbox.setInformativeText(lineEdit->text()).
msgBox.exec();
}
When you run it ,your dialog box should look like the one labeled message. Typing in anything other than a proper spreadsheet cell will not activate the ok button. Type in x11, for example gives you the message box show by the pic labeled widget.
I hope this lesson has informative to you.
Comments
No comments yet.