1、首先还是给一个效果图:
然后就是各个部分的代码,首先这个界面是要自己画的,按钮的名字:
那个Cell Location 的名字是 label
文本框的名字是 : lineEdit
左边的按钮 OK : okButton
右边的按钮 : cancelButton
头文件dialog.h
/**
* <p>我tm终于练成了!Hello World终于打印出来了! %>_<% 我先哭(激动)会~~~<p>
*
*/
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
private slots:
void on_lineEdit_textChanged(); //这个是一个函数
};
#endif // DIALOG_H
dialog.cpp定义文件
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this); //初始化界面,刚刚建立的伙伴关系,这个会自动对slot进行匹配
//就是这个connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(on_lineEdit_textChanged()));
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); //正则表达式限制输入字元的范围,记住这里很严格0和2之间只能有个逗号“,”多余的一点都不能
ui->lineEdit->setValidator(new QRegExpValidator(regExp, this)); //设定正则表达式,这个就是只允许第一个字元输入大小写英文字母,后面接一位非0的数字,再接0~2位可为0的数字
connect(ui->okButton, SIGNAL(clicked()), this, SLOT(accept())); //按下确定按钮做出的反应
connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject())); //按下取消按钮,就会关闭视窗
}
//实现那个定义的槽
void Dialog::on_lineEdit_textChanged()
{
ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput());
}
Dialog::~Dialog()
{
delete ui;
}
主函数main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
好的今天Qt就到这了,哎!期末了时间是越来越紧了,但是越是忙学的东西越是多,努力!!!