日志
2.1 子类化QDialog
2015-11-26 19:00
//UIFindDialog.h
#ifndef UIFINDDIALOG #define UIFINDDIALOG #include <QDialog> //类的前置声明 //对于该类中的所有私有变量来说前置声明是可行的 //前置声明告知编译器类的存在,而不需要提供类的实现细节 //可以加快编译速度 class QLabel; class QCheckBox; class QLineEdit; class QPushButton; class UIFindDialog : public QDialog { //对于所有定义了信号和槽的类 //都必须在类定义的开始处声明宏 Q_OBJECT Q_OBJECT public: explicit UIFindDialog(QWidget *pParent = 0); //自定义信号函数 //信号函数只需要声明,不需要实现 signals: void sigFindNext(const QString& str, Qt::CaseSensitivity cs); void sigFindPrevious(const QString& str, Qt::CaseSensitivity cs); //自定义槽函数 private slots: void slotFindClicked(); void slotEnableFindButton(const QString& strText); private: QLabel *m_pTextLabel; QLineEdit *m_pKeywordEdit; QCheckBox *m_pCaseCheckBox; QCheckBox *m_pBackwardCheckBox; QPushButton *m_pFindButton; QPushButton *m_pCloseButton; }; #endif //UIFindDialog.cpp #include <QtGui> #include "UIFindDialog.h" UIFindDialog::UIFindDialog(QWidget* pParent) : QDialog(pParent) { m_pTextLabel = new QLabel(tr("查找")); m_pKeywordEdit = new QLineEdit; m_pTextLabel->setBuddy(m_pKeywordEdit); m_pCaseCheckBox = new QCheckBox(tr("区分大小写")); m_pBackwardCheckBox = new QCheckBox(tr("从前向后")); m_pFindButton = new QPushButton(tr("查找")); m_pFindButton->setDefault(true); m_pFindButton->setEnabled(false); m_pCloseButton = new QPushButton(tr("关闭")); QHBoxLayout* pTopLeftLayout = new QHBoxLayout; pTopLeftLayout->addWidget(m_pTextLabel); pTopLeftLayout->addWidget(m_pKeywordEdit); QVBoxLayout* pLeftLayout = new QVBoxLayout; pLeftLayout->addLayout(pTopLeftLayout); pLeftLayout->addWidget(m_pCaseCheckBox); pLeftLayout->addWidget(m_pBackwardCheckBox); QVBoxLayout* pRightLayout = new QVBoxLayout; pRightLayout->addWidget(m_pFindButton); pRightLayout->addWidget(m_pCloseButton); QHBoxLayout* pMainLayout = new QHBoxLayout; pMainLayout->addLayout(pLeftLayout); pMainLayout->addLayout(pRightLayout); this->setLayout(pMainLayout); //调用QObject::connect(); 函数进行槽连接时,注意参数是否匹配 //1.信号函数的参数列表与槽函数的参数列表完全相同 //2.信号函数的参数个数大于槽函数参数个数, // 且两个参数列表中的参数类型从左向右相匹配 // 多余的参数将被忽略掉 //3.注意参数列表中const属性 connect(m_pKeywordEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slotEnableFindButton(const QString&))); connect(m_pCloseButton, SIGNAL(clicked()), this, SLOT(close())); } void UIFindDialog::slotFindClicked() { QString strKeyword = m_pKeywordEdit->text(); //判断是否区分大小写 Qt::CaseSensitivity cs = //区分 //不区分 m_pCaseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; if (m_pBackwardCheckBox->isChecked()) { //向前查找 emit sigFindPrevious(strKeyword, cs); } else { //向后查找 emit sigFindNext(strKeyword, cs); } } void UIFindDialog::slotEnableFindButton(const QString& strText) { m_pFindButton->setEnabled(!strText.isEmpty()); } //main.cpp #include <QApplication> #include <QTextCodec> #include "UIFindDialog.h" int main(int argc, char** argv) { QApplication app(argc, argv); //对汉字进行编解码 QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); UIFindDialog dialog; dialog.show(); return app.exec(); } |
下一篇: 2.2 深入介绍信号和槽
上一篇: 1.3 窗口部件的布局