Chinaunix首页 | 论坛 | 博客
  • 博客访问: 33589
  • 博文数量: 16
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-26 14:56
文章分类

全部博文(16)

文章存档

2015年(1)

2014年(15)

我的朋友

分类: 嵌入式

2014-03-26 21:10:24

效果:c++ GUI QT4编程  一书第二章第一个例子
代码如下:
findddialog.h

点击(此处)折叠或打开

  1. #ifndef FINDDIALOG_H
  2. #define FINDDIALOG_H

  3. #include <QDialog>

  4. class QCheckBox;
  5. class QLabel;
  6. class QLineEdit;
  7. class QPushButton;

  8. class FindDialog : public QDialog
  9. {
  10.     Q_OBJECT

  11. public:
  12.     FindDialog(QWidget *parent = 0);

  13. signals:
  14.     void findNext(const QString &str, Qt::CaseSensitivity cs);
  15.     void findPrevious(const QString &str, Qt::CaseSensitivity cs);

  16. private slots:
  17.     void findClicked();
  18.     void enableFindButton(const QString &text);

  19. private:
  20.     QLabel *label;
  21.     QLineEdit *lineEdit;
  22.     QCheckBox *caseCheckBox;
  23.     QCheckBox *backwardCheckBox;
  24.     QPushButton *findButton;
  25.     QPushButton *closeButton;
  26. };

  27. #endif
上面代码解析:1.预处理防止这个头文件的多重包含
2.前置声明几个类,提高程序效率
3.声明信号和槽函数 注意这个信号出现在了槽函数(往后看)中
4.私有变量
第二个文件  finddialog.cpp

点击(此处)折叠或打开

  1. #include <QtGui>

  2. #include "finddialog.h"

  3. FindDialog::FindDialog(QWidget *parent)
  4.     : QDialog(parent)
  5. {
  6.     label = new QLabel(tr("Find &what:"));
  7.     lineEdit = new QLineEdit;
  8.     label->setBuddy(lineEdit);

  9.     caseCheckBox = new QCheckBox(tr("Match &case"));
  10.     backwardCheckBox = new QCheckBox(tr("Search &backward"));

  11.     findButton = new QPushButton(tr("&Find"));
  12.     findButton->setDefault(true);
  13.     findButton->setEnabled(false);

  14.     closeButton = new QPushButton(tr("Close"));

  15.     connect(lineEdit, SIGNAL(textChanged(const QString &)),
  16.             this, SLOT(enableFindButton(const QString &)));
  17.     connect(findButton, SIGNAL(clicked()),
  18.             this, SLOT(findClicked()));
  19.     connect(closeButton, SIGNAL(clicked()),
  20.             this, SLOT(close()));

  21.     QHBoxLayout *topLeftLayout = new QHBoxLayout;
  22.     topLeftLayout->addWidget(label);
  23.     topLeftLayout->addWidget(lineEdit);

  24.     QVBoxLayout *leftLayout = new QVBoxLayout;
  25.     leftLayout->addLayout(topLeftLayout);
  26.     leftLayout->addWidget(caseCheckBox);
  27.     leftLayout->addWidget(backwardCheckBox);

  28.     QVBoxLayout *rightLayout = new QVBoxLayout;
  29.     rightLayout->addWidget(findButton);
  30.     rightLayout->addWidget(closeButton);
  31.     rightLayout->addStretch();

  32.     QHBoxLayout *mainLayout = new QHBoxLayout;
  33.     mainLayout->addLayout(leftLayout);
  34.     mainLayout->addLayout(rightLayout);
  35.     setLayout(mainLayout);

  36.     setWindowTitle(tr("Find"));
  37.     setFixedHeight(sizeHint().height());
  38. }

  39. void FindDialog::findClicked()
  40. {
  41.     QString text = lineEdit->text();
  42.     Qt::CaseSensitivity cs =
  43.             caseCheckBox->isChecked() ? Qt::CaseSensitive
  44.                                       : Qt::CaseInsensitive;
  45.     if (backwardCheckBox->isChecked()) {
  46.         emit findPrevious(text, cs);
  47.     } else {
  48.         emit findNext(text, cs);
  49.     }
  50. }

  51. void FindDialog::enableFindButton(const QString &text)
  52. {
  53.     findButton->setEnabled(!text.isEmpty());
  54. }
注意1.setBuddy这是伙伴关系,和那个tab换到下一个输入口差不多2.&这是快捷键设置
解析:1.创建部件2.信号和槽函数3.布局 四个布局去看那本书
4.两个槽函数,注意上面的槽函数里面有信号,扩展#####可以继续用这个信号connect(信号,槽)来继续扩展槽,看书上写的第三章有


接下来
main.cpp

点击(此处)折叠或打开

  1. #include <QApplication>

  2. #include "finddialog.h"

  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication app(argc, argv);
  6.     FindDialog *dialog = new FindDialog;
  7.     dialog->show();
  8.     return app.exec();
  9. }

总结:之所以会发这个例子,因为这个例子提供了三方面QT的基础:1.信号与曹2.布局怎么布局3.就是一般的怎么写QT

写的不完善,具体内容参见c++ GUI QT4这本书第二章一开始






阅读(667) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:QT串口qextserialport类 使用UI

给主人留下些什么吧!~~