第二章:创建对话框
2016-02-24 17:47
246 查看
1、手动创建对话框
1)finddialog.h头文件
#ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QDialog> #include <QHBoxLayout> #include <QVBoxLayout> #include <QMessageBox> class QCheckBox; class QLabel; class QLineEdit; class QPushButton; class FindDialog:public QDialog { Q_OBJECT public: FindDialog(QWidget *parent = 0); public: void findNext(const QString &str, Qt::CaseSensitivity cs); void findPrevious(const QString &str, Qt::CaseSensitivity cs); private slots: void findClicked(); void enableFindButton(const QString &text); private: QLabel *label; QLineEdit *lineEdit; QCheckBox *caseCheckBox; QCheckBox *backwardCheckBox; QPushButton *findButton; QPushButton *closeButton; }; #endif
2)finddialog.cpp
<pre name="code" class="cpp">#include <QtGui> #include "finddialog.h" FindDialog::FindDialog(QWidget *parent) : QDialog(parent) { label = new QLabel( tr( "Find &what" ) ); lineEdit = new QLineEdit; label->setBuddy( lineEdit ); caseCheckBox = new QCheckBox( tr( "Match &case" ) ); backwardCheckBox = new QCheckBox( tr( "Search &backward" ) ); findButton = new QPushButton( tr( "&Find" ) ); findButton->setDefault( true ); findButton->setEnabled( false ); closeButton = new QPushButton( tr( "Close" ) ); connect( lineEdit, SIGNAL( textChanged( const QString & ) ), this, SLOT( enableFindButton( const QString & ) ) ); connect( findButton, SIGNAL( clicked() ), this, SLOT( findClicked() ) ); connect( closeButton, SIGNAL( clicked() ), this, SLOT( close() ) ); QHBoxLayout * topLeftLayout = new QHBoxLayout; topLeftLayout->addWidget( label ); topLeftLayout->addWidget( lineEdit ); QVBoxLayout *leftLayout = new QVBoxLayout; leftLayout->addLayout( topLeftLayout ); leftLayout->addWidget( caseCheckBox ); leftLayout->addWidget( backwardCheckBox ); QVBoxLayout *rightLayout = new QVBoxLayout; rightLayout->addWidget( findButton ); rightLayout->addWidget( closeButton ); rightLayout->addStretch(); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout( leftLayout ); mainLayout->addLayout( rightLayout ); setLayout( mainLayout ); setWindowTitle( tr( "Find" ) ); setFixedHeight( sizeHint().height() ); } void FindDialog::findClicked() { QString text = lineEdit->text(); Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; if ( backwardCheckBox->isChecked() ){ emit findPrevious( text, cs ); } else { emit findNext( text, cs ); } } void FindDialog::enableFindButton( const QString &text ) { findButton->setEnabled( !text.isEmpty() ); } void FindDialog::findNext(const QString &str, Qt::CaseSensitivity cs) { if ( cs == Qt::CaseSensitive ) { QMessageBox::about( this, tr( "information tips " ), tr( "Previous:" ) + str + tr( "Sensitivite!" ) ); } else{ QMessageBox::about( this, tr( "information tips " ), tr( "Previous" ) + str + tr( "InSensitivite!" ) ); } } void FindDialog::findPrevious(const QString &str, Qt::CaseSensitivity cs) { if ( cs == Qt::CaseSensitive ){ QMessageBox::about( this, tr( "information tips " ), tr( "Backward" ) + str + tr( "Sensitivite!" ) ); } else{ QMessageBox::about( this, tr( "information tips " ), tr( "Backward" ) + str + tr( "InSensitivite!" ) ); } }
<pre name="code" class="cpp" style="font-size: 11.8181819915771px;">在finddialog.cpp源文件中,
Qmessage::about()函数具体入口参数, QMessageBox::about(NULL, "string", "string1");
这样就会在最后就会出现提示窗口。
3)main.cpp源文件
#include "finddialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); FindDialog *dialog = new FindDialog; dialog->show(); return app.exec(); }运行结果为:
在编辑框输入相应数据时反馈结果为:
相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- Qt 5.6更新至RC版,最终版本近在咫尺
- Qt定时器和随机数详解
- Qt实现图片移动实例(图文教程)
- Qt for Android开发实例教程
- QModelIndex/Role/Model介紹<二>
- 基于PyQt5的快速开发框架QFramer
- OSX下安装PyQt
- ok6410开发板移植DirectFB手记
- 【笔记】给Qt内嵌一个Chrome吧
- 【算法】最短路径之A*搜索
- qt入门必备
- 在 Qt4 中使用 C++11
- Hello Word ~ v0.2.2 背单词软件发布 -- By WHYPRO
- ubuntu下opencv和qt的安装配置
- linux下opengl的安装(with qt)
- qt 学习小节
- QT编译时Cannot find -lGL
- QT的命名风格