QT base:QT Designer

QT creator: IDE including Qt Designer

 

 

Using the Reference Documentation

The documentation is available in HTML format in Qt's doc/html directory;You can also use Qt Assistant,type assistant on the command line on Unix,

 

To launch Qt Designer,type designer on the command line on Unix,

The first step is to create the child widgets,Create one label, one line editor,
one horizontal spacer, and two push buttons.

(一)QT设计:

1. Click the text label. Make sure that its objectName property is "label" and set the text property
to "&Cell Location:".
2. Click the line editor. Make sure that the objectName property is "lineEdit".
3. Click the first button. Set the objectName property to "okButton", the enabled property to
"false", the text property to "OK", and the default property to "true".
4. Click the second button. Set the objectName property to "cancelButton" and the text property
to "Cancel".
5. Click the form's background to select the form itself. Set the objectName property to
"GoToCellDialog" and the windowTitle property to "Go to Cell".

 

 

except the text label, which shows &Cell Location.:

Click Edit|Edit Buddies to enter a special mode that allows you to set buddies.

click the label and drag the red arrow line to the line editor, then release.

Click Edit|Edit Widgets to leave buddy mode.

 

&Cell Location变为Cell Location

 

Relay out widgets:

1Select the first line,Click Form|Lay Out Horizontally.

2Select the Second line,Click Form|Lay Out Horizontally.

3Click the background of the form to deselect any selected items, then click Form|Lay Out Vertically.

4Click Form|Adjust Size to resize the form to its preferred size.

red lines that appear on the form show the layouts that have been created,

 

 

 

Now click Edit|Edit Tab Order. A number in a blue rectangle will appear next to every widget that can accept
focus,To preview the dialog, click the Form|Preview menu option. Check the tab order by pressing Tab repeatedly.
Close the dialog using the close button in the title bar.

 

(二)Save the dialog as gotocelldialog.ui((an XML-based file format),)

create a main.cpp file

 

 

#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
}

 

qmake, qmake, make,

The uic tool converts gotocelldialog.ui into
C++ and puts the result in ui_gotocelldialog.h.

If you run the program now, the dialog will work, but it doesn't function exactly as we want:
 The OK button is always disabled.
 The Cancel button does nothing.
 The line editor accepts any text, instead of accepting only valid cell locations.

(三)只是框架有了,但是一些功能没有,点OK,Cancel没有反应的,因此derived from class。

(上一步不加2个button,可以加一个个button box就可以OK,cancel有反应了)

create a file called gotocelldialog.h

 

#ifndef GOTOCELLDIALOG_H

#define GOTOCELLDIALOG_H
#include <QDialog>
#include "ui_gotocelldialog.h"
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
Q_OBJECT
public:
GoToCellDialog(QWidget *parent = 0);
private slots:
void on_lineEdit_textChanged();
};
#endif

 

The implementation belongs in the gotocelldialog.cpp file:

 

#include <QtGui>
#include "gotocelldialog.h"
GoToCellDialog::GoToCellDialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);     //setupUi() to initialize the form.


QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");

/*Allow one uppercase or lowercase letter, followed by one digit in the range 1 to 9, followed by zero, one, or two
digits each in the range 0 to 9.*/


lineEdit->setValidator(new QRegExpValidator(regExp, this));

/*passing this to the QRegExpValidator constructor, we make it a child of the GoToCellDialog object.
By doing so, we don't have to worry about deleting the QRegExpValidator later; it will be deleted
automatically when its parent is deleted.*/


connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

/*Both slots close the dialog,accept() sets the dialog's result value to
QDialog::Accepted (which equals 1), and reject() sets the result to QDialog::Rejected (which equals
0).*/


}
void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());

/*The on_lineEdit_textChanged() slot enables or disables the OK button, according to whether the line
editor contains a valid cell location.*/
}

 

This completes the dialog. We can now rewrite main.cpp to use it:
#include <QApplication>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GoToCellDialog *dialog = new GoToCellDialog;
dialog->show();
return app.exec();
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值