Qt对话框Dialog之查找对话框2

这段代码展示了如何创建一个名为GoToCell的对话框,该对话框包含一个用于输入单元格位置的QLineEdit。通过QRegExpValidator,对话框限制了输入,只接受A到Z的字母和1到999的数字,确保输入符合单元格坐标格式。

一、查找对话框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());
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值