如创建QT 控件项目―> Qt-Gui应用―>文件名称命名为qt1,则
qt1.ui是绘图界面,有设计和编辑两种模式;
qt1.h定义信号与槽;
qt1.cpp是对qt1.h里信号与槽等成员函数的实现;
main.cpp是主程序文件。
关键:1、理清楚类名文件名,继承之间的关系;
2、控件的调用在qt1.cpp中connect()函数关联信号与槽中出现,控件名通qt1.ui中的objectName的值。
linux qt creator下的一个实例
http://book.51cto.com/art/201203/323264.htm
查找对话框
finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
class QCheckBox;//4-7行类的前向声明,通过该声明,编译器就知道类已经存在,而不用写出包含的头文件。
class QLabel;
class QLineEdit;
class QPushButton;
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_H
finddialog.cpp
#include <QtGui>
#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"));
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::CaseSensitive
:Qt::CaseInsensitive;
if(backwardCheckBox->isCheckable()) {
emit findPrevious(text,cs);
}else{
emit findNext(text,cs);
}
}
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
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();
}