linux QT

来源:互联网 发布:大唐陌刀 知乎 编辑:程序博客网 时间:2024/06/07 22:41
linux qt creator工程中几个文件的含义及关系

如创建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-&gt;setBuddy(lineEdit);
    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));
    findButton = new QPushButton(tr("&Find"));
    findButton-&gt;setDefault(true);
    findButton-&gt;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-&gt;addWidget(label);
    topLeftLayout-&gt;addWidget(lineEdit);
    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout-&gt;addLayout(topLeftLayout);
    leftLayout-&gt;addWidget(caseCheckBox);
    leftLayout-&gt;addWidget(backwardCheckBox);
    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout-&gt;addWidget(findButton);
    rightLayout-&gt;addWidget(closeButton);
    rightLayout-&gt;addStretch();
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout-&gt;addLayout(leftLayout);
    mainLayout-&gt;addLayout(rightLayout);
    setLayout(mainLayout);
    setWindowTitle(tr("Find"));
            setFixedHeight(sizeHint().height());
}
void FindDialog::findClicked()
{
    QString text = lineEdit-&gt;text();
    Qt::CaseSensitivity cs =
            caseCheckBox-&gt;isChecked()?Qt::CaseSensitive
                                    :Qt::CaseInsensitive;
    if(backwardCheckBox-&gt;isCheckable()) {
        emit findPrevious(text,cs);
    }else{
        emit findNext(text,cs);
    }
}
void FindDialog::enableFindButton(const QString &text)
{
    findButton-&gt;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-&gt;show();
    return app.exec();
}

原创粉丝点击