对话框QDialog


main.cpp

#include <QApplication> #include "finddialog.h"

int main(int argc, char *argv[]) { QApplication app(argc, argv); FindDialog *dialog = new FindDialog; dialog->show(); return app.exec(); }

finddialog.h

#ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QDialog>

//前置声明一些用到的Qt类,会告诉编译器类的存在,而不用提供类定义中的所有细节,这可以使编译过程更快一些 class QCheckBox; class QLabel; class QLineEdit; class QPushButton; class FindDialog : public QDialog { Q_OBJECT  //对于所有定义了信号与槽的类,Q_OBJECT宏都是必须的 public: FindDialog(QWidget *parent = 0);  //parent参数指定了父窗口部件,默认值是一个空指针,意味着没有父对象 signals:  //信号 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

finddialog.cpp

#include <QtGui> #include "finddialog.h" FindDialog::FindDialog(QWidget *parent) : QDialog(parent)  //把parent传递给基类的构造函数 { label = new QLabel(tr("Find &what:"));  //tr()函数把它们翻译成其他语言的标记,在每个QObject对象以及包含有Q_OBJECT宏的子类中都有其声明 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);  //设置为默认按钮,即用户按下Enter键时能够按下对应的按钮 findButton->setEnabled(false);  //禁用 closeButton = new QPushButton(tr("Close")); 
   //由于QObject是FindDialog的父对象之一,所以可以省略connect()函数前面的QObject::前缀 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());  //sizeHint()返回一个窗口部件的理想的尺寸大小 } 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()); }

运行效果:

附加小知识:

1:

创建窗口部件和布局时使用的是new,理应写一个delete的析构函数,但在qt中,在删除父对象的时候会自动删除其所属的所有子对象

2:

默认的Tab键顺序就是创建窗口部件时的顺序,要改变这个顺序,可以使用QWidget::setTabOrder()函数

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM