qt入门——find对话框

来源:互联网 发布:淘宝直播截图抽奖技巧 编辑:程序博客网 时间:2024/05/22 00:39

本文程序源自C++ GUI Qt4编程第二章。

1.首先建立finddialog.h。

#ifndef FINDDIALOG_H#define FINDDIALOG_H#include <QDialog>class QCheckBox;class QLabel;class QLineEdit;class QPushButton;//前向声明,一般放在头文件中,告诉编译器此类类型存在class FindDialog: public QDialog{Q_OBJECTpublic:FindDialog(QWidget *parent=0);signals:void findNext(const QString &str,Qt::CaseSensitivity cs); //查找下一个void findPrevious(const QString &str,Qt::CaseSensitivity cs);//查找上一个 public slots:void findClicked();//查找按钮按下void enableFindButton(const QString &text);//使能查找按钮private:QLabel *label;//因为是指针,所以前面只需前向声明,无需完整定义,参见c++primer,使用前向声明可以加快编译QLineEdit *lineEdit;QCheckBox *caseCheckBox;QCheckBox *backwardCheckBox;QPushButton *findButton;QPushButton *closeButton;};#endif


2.建立finddialog.cpp。

#include <QtGui>#include <QLabel>//用到什么就包含什么是一种较好的编程风格#include <QLineEdit>#include <QPushButton>#include <QCheckBox>#include <QHBoxLayout>#include <QVBoxLayout>#include "finddialog.h"FindDialog::FindDialog(QWidget *parent):QDialog(parent){label = new QLabel("find &what");lineEdit = new QLineEdit;label->setBuddy(lineEdit);caseCheckBox = new QCheckBox("Match &case");backwardCheckBox = new QCheckBox("Search &backward");findButton = new QPushButton("&find");findButton->setDefault(true);findButton->setEnabled(false);closeButton = new QPushButton("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("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);//未实现elseemit findNext(text,cs);//未实现}void FindDialog::enableFindButton(const QString &text){findButton->setEnabled(!text.isEmpty());}
使用了new就需要delete,但这并不需要,因为qt会在删除父对象时,自动删除其所有子对象。

3.修改main.cpp。

//#include "myclass.h"#include "finddialog.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);//MyClass w;//w.show();FindDialog *findDialog= new FindDialog;findDialog->show();return a.exec();}


4.编译时,会出现问题:‘GLES2/gl2.h’: No such file or directory。解决办法参见http://www.aiuxian.com/article/p-1533955.html。


5.运行界面。


6.使用qt+vs效率好低,几乎全靠手动输入,vs几乎没有联想功能,是用的方法不对么,为什么qt要用vs?

1 0
原创粉丝点击