打开QT creater创建取名去findDialog的项目,这个项目要基于QDialog。直接上FindDialog.h的头文件。

 #ifndef FINDDIALOG_H
#define FINDDIALOG_H #include<QDialog>
#include<QtGui>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton; class FindDialog : public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget *parent = );
~FindDialog(); 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_H

头文件里,前向声明了几个类,这个类没有定义,在.cpp文件中,前向声明必须要 #include。这个未被定义的。不能直接定义一个对象,程序直接是错误的,而定义一个指针指向这个类是可以的,但是在析构的时候,因为这个指针指向的东西不明确,所以在析构的时候可能会导致内存泄露。

下面直接上.cpp的文件,

 #include <QtGui>
#include "finddialog.h"
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)//调用父类的构造函数
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit); caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backford")); findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false); closeButton = new QPushButton(tr("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(tr("Find"));
setFixedHeight(sizeHint().height());
} FindDialog::~FindDialog()
{ } void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
if(backwardCheckBox->isChecked()) {
emit findPrevious(text, cs);
} else {
emit findNext(text, cs);
}
} void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}

Q_OBJECT这个宏在 定义信号的时候,都是要写的。

最后直接上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();
}

最后运行结果。

用QT写一个对话框的更多相关文章

  1. Qt写一个截屏工具(窗口透明)

    最近发现好多次打开QQ仅仅想用它来截屏 ⊙﹏⊙b汗 不如自己来写一个截屏工具,集成到自己的小工具箱里面 动手之前考虑一下要怎么实现,我考虑过的方案大概有下面两种  : 1. 监控全局鼠标事件 (真是“ ...

  2. 如何用Qt写一个同一时间只能运行一个实例的应用程序

    http://blog.sina.com.cn/s/blog_6343941a0100nk2x.html 可以达到的目的: 1.应用只启动一个实例,依赖于QtNetwork模块 2.启动时向另一个实例 ...

  3. 使用QT实现一个简单的登陆对话框(纯代码实现C++)

    使用QT实现一个简单的登陆对话框(纯代码实现C++) 效果展示 使用的QT控件 控件 描述 QLable 标签 QLineEdit 行文本框 QPushButton 按扭 QHBoxLayout 水平 ...

  4. QT学习之路--创建一个对话框

    Q_OBJECT:这是一个宏,凡是定义信号槽的类都必须声明这个宏. 函数tr()全名是QObject::tr(),被他处理过的字符串可以使用工具提取出来翻译成其他语言,也就是做国际化使用. 对于QT学 ...

  5. 用Qt写软件系列三:一个简单的系统工具(上)

    导言 继上篇<用Qt写软件系列二:QIECookieViewer>之后,有一段时间没有更新博客了.这次要写的是一个简单的系统工具,需求来自一个内部项目.功能其实很简单,就是查看当前当前系统 ...

  6. QT学习日记篇-03-仿写一个智能家居界面

    课程大纲: <1>让界面漂亮起来,仿写一个智能家居界面 ->第一:给QT工程添加图片 进入下一步: <注意路径和名称一定不能有中文>                   ...

  7. 第八章 Qt GUI之对话框使用

    第八章 Qt GUI之对话框使用 对话框可以是模态(modal)的或非模态(modeless)两种.当我们在一个用户界面程序里面对一个对话框(比如选择文件对话框)的操作没有结束前,界面的其他窗口无法操 ...

  8. C/C++ Qt 自定义Dialog对话框组件应用

    在上一篇博文 <C/C++ Qt 标准Dialog对话框组件应用> 中我给大家演示了如何使用Qt中内置的标准对话框组件实现基本的数据输入功能. 但有时候我们需要一次性修改多个数据,使用默认 ...

  9. 直接用Qt写soap

    直接用Qt写soap 最近的项目里用到了webservice, 同事用的是`gSoap`来搞的. 用这个本身没什么问题, 但这货生成的代码实非人类可读, 到处都是`__`和`_`, 看得我眼晕.... ...

随机推荐

  1. lucene原理

    lucene查找原理: https://yq.aliyun.com/articles/581877

  2. 【温故知新】C#基于事件的异步模式(EAP)

    在开发winform和调用asp.net的web service引用的时候,会出现许多命名为 MethodNameAsync 的方法. 例如: winform的按钮点击 this.button1.Cl ...

  3. execution(* *..BookManager.save(..))的解读

    execution(* *..BookManager.save(..))的解读: 第一颗* 代表ret-type-pattern 返回值可任意, *..BookManager 代表任意Pacakge里 ...

  4. 【wordpress】wordpress自定义主题

    wordpress每个主题至少要有这两个文件 – style.css 和 index.php. index.php 告诉主题中所有的元素如何布局; style.css 则告诉主题中所有的元素该如何展示 ...

  5. 1、Shell命令行书写规则

    学习目标Shell命令行书写规则 正文对Shell命令行基本功能的理解有助于编写更好的Shell程序,在执行Shell命令时多个命令可以在一个命令行上运行,但此时要使用分号(;)分隔命令,例如: ro ...

  6. zato集群部署

    注: SQL ODB和Cluster’s config需要首先依次创建,其他三个次序随意 对不熟悉的命令,使用server create *** -h 查看帮助文档 修改完后配置文件,要重启(zato ...

  7. Hbuilder编辑App时,ajax跨域访问失败问题

    今天试着用Hbuilder写app的前段显示页面,在第一步时就被打住了,ajax异步调用服务器的登录接口时,报错, 显示这样的错误 XMLHttpRequest cannot loadhttp://w ...

  8. The Internet Communications Engine (Ice) 跨平台异构通讯方案 第二弹-Hello world!

    如果不知道ICE是什么的同学,请看上一篇的ICE简介:http://www.cnblogs.com/winds/p/3864677.html 好了,HelloWorld,从中间语言讲起. 首先,我们新 ...

  9. JS实现中英文混合文字溢出友好截取功能

    在显示字符串的时候,避免字符串过长往往会对字符串进行截取操作,通常会用到js的 substr 或者 substring方法, 以及 字符串的length属性 substr() 方法可在字符串中抽取从 ...

  10. 将一个数据库中表的数据导入另一个数据库(DB2)

    将一个数据库中的数据导入另一个数据库(DB2) 我这里举得例子是使用的DB2数据库,其他数据库思路也是这样啦! 1.从db2 数据库中将表中的数据导入本地的excel中 export to d:\my ...