Qt下完全手写创建对话框

2014-11-24 10:51:29 ? 作者: ? 浏览: 0

在这里将完全使用C++编写一个查找对话框。首先来看它的头文件


/////finddialog.h///////


#ifndef FINDDIALOG_H
#define FINDDIALOG_H


#include


///前置申明,避免包含大的头文件使得编译能过快速通过


class QCheckBox;


class QLabel;
class QLineEdit;
class QPushButton;


///定义自己的类,继承QDialog从而可以用其成员函数


////并且具备相应的属性


class FindDialog : public QDialog
{


/////对于所有定义了信号和槽的类,定义下面这个宏是必须的
Q_OBJECT


public:
FindDialog(QWidget *parent = 0);


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.cpp,在这个文件中主要实现了三个成员函数:构造函数,两个槽函数:


在构造函数中实现了对话框的布局与信号的连接:


#include


#include "finddialog.h"


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 &backward"));


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


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


//////将信号和槽连接起来并且在运行起来后系统会自动监视是否有指定的信号产生,如:文本框里是否有输入,


/////button是否被点击,如果信号发生那么就调用相应的槽函数去处理。


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());
}




////槽函数的实现,当点击时


void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs =
caseCheckBox->isChecked() Qt::CaseSensitiv:Qt::CaseInsensitive;
if (backwardCheckBox->isChecked())


{
emit findPrevious(text, cs);////发射信号
}


else


{
emit findNext(text, cs);//// 发射信号
}



}


/////槽函数的实现,当编辑框有输入时调用激活按钮


void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}


最后来看看main.cpp,在main函数里主要是实例化一个finddialog然后等待信号以执行相应的操作


#include


#include "finddialog.h"


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();


////将应用和系统事件传递给fingdialog对话框,并且等待程序结束进而退出
return app.exec();
}


-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: