打开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. Rabbitmq的五种模式和案例

    消息生产者p将消息放入队列 消费者监听队列,如果队列中有消息,就消费掉,消息被拿走后,自动从队列删除 (缺点:消息可能没有被消费者正确处理,已经消失了,无法恢复) 应用场景:聊天室 1.引入依赖 &l ...

  2. MySQL插入记录 insert

    一.insert insert tb_name [(col_name,......)] {values | value} ({expr | default } ....... ) , ( ... ) ...

  3. Robot Framework(用户关键字)

    在 Robot Framework 中关键字的创建分两种:系统关键字和用户关键字.系统关键字需要通过脚本开发相应的类和方法,这个我们将在后面的章节介绍.用户关键字的创建就要简单得多,它主要利用现有的系 ...

  4. 《JavaScript语言精粹》读书笔记

    第三章:对象 //1.定义一个方法 method Function .prototype.method=function(name, func){ this.prototype[name]=func; ...

  5. PHP的parse_ini_file()函数,解释结构类型php.ini格式的文件

    1.直接读取,返回一维数组 如: "test.ini" 的内容: [names] me = Robert you = Peter [urls] first = "http ...

  6. spring 3.0 整合redis

    参考文章:https://blog.csdn.net/weixin_42184707/article/details/80361464 其中遇到了问题,第一,redis的xml配置文件中的,头部地址资 ...

  7. <数据挖掘导论>读书笔记6关联分析的高级概念

    处理联系属性: 基于离散化的方法 基于统计学的方法 非离散化方法 处理概念分层 定义在一个特定领域的各种实体或者概念的多层组织.概念分层可以用有向无环图DAG来标示. 序列模式 可选计数方案 COBJ ...

  8. Array中对象的排序

    1.子母排序 NSArray *kArrSort = [_dic allKeys]; //这里是字母数组:,g,a,b.y,m…… NSArray *resultkArrSort = [kArrSor ...

  9. 个人多年经典收藏集合(SQL) 推荐大家收藏

    1.SQL经典问题 查找连续日期 2.sqlserver 中charindex/patindex/like 的比较 3.SQL Server 跨服务器查询 4.SQLserver中字符串查找功能pat ...

  10. 8、导航:Nav

    1.导航视图   angular2 中的是视图是显示在<router-outlet></router-outlet>里的同时他要依赖于 directives:[ ROUTER_ ...