Qt Dialog 界面设计之 GoToCell

目录:

 一、GoToCell效果图

 二、实现代码

H:

CPP:

三、校验器

四、补充说明


一、GoToCell效果图

 二、实现代码

H:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QDialogButtonBox>

namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
    Q_OBJECT
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
signals:

private slots:
    void on_m_lineEdit_textChanged();
private:
    Ui::Dialog *ui;
private:
    QLabel *m_label;
    QLineEdit *m_lineEdit;
    QDialogButtonBox *m_buttonBox;

};

#endif // DIALOG_H

CPP:

#include "Dialog.h"
#include "ui_Dialog.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDialogButtonBox>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    //1、创建并初始化窗口部件。
    m_label = new QLabel("&Cell Location:");
    m_lineEdit = new QLineEdit();
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    //2、添加布局,把创建好的控件放入布局当中。
    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(m_label);
    topLayout->addWidget(m_lineEdit);

    QHBoxLayout *bottomLayout = new QHBoxLayout;
    bottomLayout->addStretch();
    bottomLayout->addWidget(m_buttonBox);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(bottomLayout);

    this->setLayout(mainLayout);

    //3、设置控件以及窗口的属性。
    m_label->setBuddy(m_lineEdit);
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    //设置正则表达式
    //允许一个大写或者小写的字母,后面跟着一个1-9的数字,后面在跟0、1或者2个0-9的数字
    QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
    //设置校验器
    m_lineEdit->setValidator(new QRegExpValidator(regExp,this));

    //设置窗口大小,以及tab键的顺序
    setFixedHeight(sizeHint().height());
    setTabOrder(m_label,m_buttonBox);

    //4、建立信号-槽之间的连接。
    //点击OK按钮触发
    connect(m_buttonBox,&QDialogButtonBox::accepted,this,&Dialog::accept);
    //点击Cancle按钮触发
    connect(m_buttonBox,&QDialogButtonBox::rejected,this,&Dialog::reject);

    connect(m_lineEdit,&QLineEdit::textChanged,this,&Dialog::on_m_lineEdit_textChanged);
}

Dialog::~Dialog()
{
    delete ui;
}

//5、实现自定义槽。
void Dialog::on_m_lineEdit_textChanged()
{
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_lineEdit->hasAcceptableInput());
}


三、校验器

QValidator类用于对输入文本进行校验,QValidator是一个抽象类。具有如下四个子类:

QIntValidator、QDoubleValidator、QRegExpValidator、QRegularExpressionValidator。

1、QIntValidator用于对整数文本进行校验;

2、QDoubleValidator用于对浮点数进行校验;

3、QRegExpValidator使用自定义的正则表达式对输入文本进行校验。

4、QRegularExpressionValidator通过正则表达式对输入的文本进行校验。

四、补充说明

1、创建label使用字符&设置快捷方式时,需要设置其与lineEdit为伙伴时才会有效。(具体原因未知)。可能是label单独不能设置快捷方式,button可以直接使用字符&设置快捷方式。

2、不必执着于完全使用代码来实现界面设计,代码和Qt设计师配合使用才会快速、高效的开发界面。使用代码设计界面有助于对Qt实现更多的了解。(个人理解)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值