Qt 第二章 创建对话框--快速设计对话框(2)

本文介绍了一个简单的Qt应用程序,该程序通过GoToCellDialog对话框让用户输入单元格位置,对话框验证输入并允许用户确认或取消操作。文章详细展示了如何使用QLabel、QLineEdit和QPushButton等组件创建定制化的对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gotocelldialog.h头文件
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QPushButton;
class GoToCellDialog:public QDialog
{
 Q_OBJECT
 public:
 GoToCellDialog(QDialog *parent =0);
 private slots:
 void on_lineEdit_textChanged(); 
 
 private:
 QLabel *label;
 QLineEdit *lineEdit;
 QPushButton *okButton;
 QPushButton *cancelButton;
};
#endif
gotocelldialog.cpp源文件
#include <QtGui>
#include "gotocelldialog.h"
GoToCellDialog::GoToCellDialog(QDialog *parent)
 :QDialog(parent)
{
 label = new QLabel(tr("&Cell Location"));
 lineEdit = new QLineEdit;
 label->setBuddy(lineEdit);
 
 okButton = new QPushButton(tr("OK"));
 okButton->setEnabled(false);
 okButton->setDefault(true);
 
 cancelButton = new QPushButton(tr("Cancel"));
  
 QHBoxLayout *topLayout = new QHBoxLayout;
 topLayout->addWidget(label);
 topLayout->addWidget(lineEdit);
 
 QHBoxLayout *lowerLayout = new QHBoxLayout;
 lowerLayout->addStretch();
 lowerLayout->addWidget(okButton);
 lowerLayout->addWidget(cancelButton);
 
 QVBoxLayout *mainLayout = new QVBoxLayout;
 mainLayout->addLayout(topLayout);
 mainLayout->addLayout(lowerLayout);
 
 setLayout(mainLayout);
 setWindowTitle(tr("Go to Cell"));
 setFixedHeight(sizeHint().height()); 
 
 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()));
 connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(on_lineEdit_textChanged()));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
 okButton->setEnabled(lineEdit->hasAcceptableInput());
}
 
main.cpp头文件
#include <QApplication>
#include "gotocelldialog.h"
int main(int argc,char *argv[])
{
 QApplication app(argc,argv);
 GoToCellDialog dialog;
 dialog.show();
 return app.exec();
}









本文转自 chen138 51CTO博客,原文链接:http://blog.51cto.com/chenboqiang/316718,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值