一、查找对话框UI
代码如下(示例):
#ifndef GOTOCELL_H
#define GOTOCELL_H
#include <QDialog>
#include <QObject>
class QPushButton;
class QLabel;
class QLineEdit;
class GoToCell : public QDialog
{
Q_OBJECT
public:
explicit GoToCell(QWidget *parent = 0);
private slots:
void slotTextChanged();
private:
QPushButton * mPtr_ok;
QPushButton * mPtr_cancel;
QLabel * mPtr_label;
QLineEdit * mPtr_lineEdit;
};
#endif // GOTOCELL_H
代码如下(示例):
#include "go_to_cell.h"
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QRegExpValidator>
GoToCell::GoToCell(QWidget *parent):QDialog(parent)
{
this->setWindowTitle(tr("Go To Cell"));
mPtr_ok = new QPushButton(tr("OK"));
mPtr_cancel = new QPushButton(tr("Cancel"));
mPtr_label = new QLabel(tr("&Cell Location"));
//正则
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
mPtr_lineEdit = new QLineEdit;
//lineEdit 添加内置检验器
mPtr_lineEdit->setValidator(new QRegExpValidator(regExp,this));
mPtr_label->setBuddy(mPtr_lineEdit);
QHBoxLayout * top = new QHBoxLayout;
top->addWidget(mPtr_label);
top->addWidget(mPtr_lineEdit);
QHBoxLayout * bottom = new QHBoxLayout;
bottom->addStretch();
bottom->addWidget(mPtr_ok);
bottom->addWidget(mPtr_cancel);
QVBoxLayout * main = new QVBoxLayout;
main->addLayout(top);
main->addLayout(bottom);
this->setLayout(main);
connect(mPtr_ok,SIGNAL(clicked()),this,SLOT(accept()));
connect(mPtr_cancel,SIGNAL(clicked()),this,SLOT(reject()));
connect(mPtr_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(slotTextChanged()));
}
void GoToCell::slotTextChanged()
{
mPtr_ok->setEnabled(mPtr_lineEdit->hasAcceptableInput());
}
这段代码展示了如何创建一个名为GoToCell的对话框,该对话框包含一个用于输入单元格位置的QLineEdit。通过QRegExpValidator,对话框限制了输入,只接受A到Z的字母和1到999的数字,确保输入符合单元格坐标格式。
545

被折叠的 条评论
为什么被折叠?



