码迷,mamicode.com
首页 > 其他好文 > 详细

QT学习笔记3

时间:2016-02-17 22:32:08      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

对话框

   新建了一个对话框,在新建项目选择类型时是Dialog即可。

   添加的代码如下,注意QCheckBox 并没有添加头文件定义,只是加了类的前向声明。因为我们仅仅使用的是指针,并不涉及到这些类的函数,因此并不需要include它们的头文件——当然,你想直接引入头文件也可以,不过那样的话编译速度就会慢一些。不过实验发现 还是需要添加头文件

 1 #ifndef FINDDIALOG_H
 2 #define FINDDIALOG_H
 3 
 4 #include <QtGui/QDialog>
 5 #include "ui_finddialog.h"
 6 
 7 class QCheckBox; 
 8 class QLabel; 
 9 class QLineEdit; 
10 class QPushButton; 
11 
12 class FindDialog : public QDialog
13 {
14     Q_OBJECT
15 
16 public:
17     FindDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
18     ~FindDialog();
19 signals:
20     void findNext(const QString &str,Qt::CaseSensitive cs);
21     void findPrevious(const QString &str,Qt::CaseSensitive cs);
22 private slots:
23     void findClicked();
24     void enableFindButton(const QString &text);
25 private:
26     Ui::FindDialogClass ui;
27     QLabel *label;
28     QLineEdit *lineEdit;
29     QCheckBox *caseCheckBox;
30     QCheckBox *backwardCheckBox;
31     QPushButton *findButton;
32     QPushButton *closeButton;
33 };
34 
35 #endif // FINDDIALOG_H

cpp文件代码:

1 字符串用tr()函数进行转换。

2 信号和槽函数可以自己定义。

   QObject::connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(enableFindButton(const QString& )));

这句话中,槽函数就是自己定义的,而且还可以获取信号中的参数,也就是进行参数传递
当然也可以自己定义信号:
signals:
  void findNext(const QString &str,Qt::CaseSensitive cs);自己定义信号,同时可以设置传入的参数
 通过emit findNext (text,cs);进行发射信号
 1 FindDialog::FindDialog(QWidget *parent, Qt::WFlags flags)
 2     : QDialog(parent, flags)
 3 {
 4     ui.setupUi(this);
 5     label=new QLabel(tr("Find &what:"));
 6     lineEdit=new QLineEdit;
 7     label->setBuddy(lineEdit);
 8 
 9     caseCheckBox=new QCheckBox(tr("Match &chase"));
10     backwardCheckBox=new QCheckBox(tr("Search &backford"));
11 
12     findButton=new QPushButton(tr("&Find"));
13     findButton->setDefault(true);
14     findButton->setEnabled(false);
15     closeButton=new QPushButton(tr("Close"));
16 
17     QObject::connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(enableFindButton(const QString& )));
18     connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
19     connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
20 
21     QHBoxLayout *topLeftLayout=new QHBoxLayout;
22     topLeftLayout->addWidget(label);
23     topLeftLayout->addWidget(lineEdit);
24 
25     QVBoxLayout *leftLayout=new QVBoxLayout;
26     leftLayout->addLayout(topLeftLayout);
27     leftLayout->addWidget(caseCheckBox);
28     leftLayout->addWidget(backwardCheckBox);
29 
30     QVBoxLayout *rightLayout=new QVBoxLayout;
31     rightLayout->addWidget(findButton);
32     rightLayout->addWidget(closeButton);
33     rightLayout->addStretch();
34     
35     QHBoxLayout *mainLayout=new QHBoxLayout;
36     mainLayout->addLayout(leftLayout);
37     mainLayout->addLayout(rightLayout);
38 
39     this->setLayout(mainLayout);
40     this->setWindowTitle(tr(("Find")));
41     setFixedHeight(sizeHint().height());
42     
43 }
44 
45 FindDialog::~FindDialog()
46 {
47 
48 }
49 
50 
51 void FindDialog::findClicked()
52 {
53    QString text=lineEdit->text();
54    Qt::CaseSensitivity cs=caseCheckBox->isChecked()?Qt::CaseInsensitive:Qt::CaseSensitive;
55    if (backwardCheckBox->isChecked())
56    {
57        emit findPrevious(text,cs);
58    }else emit findNext(text,cs);
59 }
60 
61 void FindDialog::enableFindButton(const QString &text)
62 {
63    findButton->setEnabled(!text.isEmpty());
64 }

运行结果

技术分享

QT学习笔记3

标签:

原文地址:http://www.cnblogs.com/love6tao/p/5196601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!