运行 designer.exe
新建gotocelldialog.ui
New File or Project ->Qt ->Qt Designer Form ->templates/forms ->Dialog Without Buttons
创建控件
一个:label, line editor, horizontal spacer
两个: push button
设置属性
label 的objectName 设为"label",text 属性设为"&Cell Location:"
line editor 的objectName 设为"lineEdit"
一个HorizontalSpacer
一个push button 的objectName 设为"okButton",enabled 属性设为"false",text 属性设为 "OK",default 属性设为"true"
另一个push button 的objectName 设为"cancelButton",text 属性设为 "Cancel"
点击选择窗体本身,objectName 设为GoToCellDialog,windowTitle 属性设为"Go to Cell"
设置label 的buddy
将label 和line editor 设为buddies,"&Cell Location" 中的"&"就会显示为下划线
Edit ->Edit buddies ->鼠标按住label拖动红色箭头到line editor
Edit ->Edit Widgets 离开buddy 模式
布局控件
选中label 和line editor ->Tools ->Form editor ->Lay Out Horizontally
选中HorizontalSpacer 和两个push button 和上面一样设为横向布局
选中两组横向布局,设为Lay Out Vertically
选中窗体本身,设为横向布局,然后Tools ->Form editor ->Adjust Size
设置Tab 顺序
Edit ->Edit Tab Order ->出现1,2,3 等数字,多点几下调整顺序
Edit ->Edit Widgets 离开Tab Order 模式
预览窗口
Tools ->Form editor ->Preview
按Tab 键检查一下Tab 顺序
保存
存为C:/gotocell/gotocelldialog.ui
在目录C:/gotocell 新建main.cpp
//
// main.cpp
#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
#pragma comment(lib, "QtCore4.lib")
#pragma comment(lib, "QtGui4.lib")
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();
}
//
将main.cpp 和gotocelldialog.ui 加入工程,右击gotocelldialog.ui ->属性 ->自定义生成步骤
命令行:uic ./src/gotocelldialog.ui -o ./src/ui_gotocelldialog.h
输出:./src/ui_gotocelldialog.h
F7 编译,然后将生成的ui_gotocelldialog.h 加入工程
现在程序已经可以正常运行起来了,不过只是显示出界面而没有功能。
新建 文件gotocelldialog.h,与uic 产生的ui_gotocelldialog.h 名字相同但是没有"ui_" 前缀
gotocelldialog.h 中声明一个类GoToCellDialog,继承自QDialog 和Ui::GoToCellDialog
命名习惯是将这个类命名为与 uic.exe产生的类的名字一样但是没有"Ui::" 前缀
//
// 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
//
从gotocelldialog.h 生成moc_gotocelldialog.cpp,加入工程
新建gotocelldialog.cpp,加入工程
//
// gotocelldialog.cpp
#include <QtGui>
#include "gotocelldialog.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());
}
//
改写前面的main.cpp
//
// main.cpp
#include <QApplication>
#include <QDialog>
#include "gotocelldialog.h"
#pragma comment(lib, "QtCore4.lib")
#pragma comment(lib, "QtGui4.lib")
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GoToCellDialog *dialog = new GoToCellDialog;
dialog->show();
return app.exec();
}
//
运行,enjoy!