Qt Project源码布局阐明
当前位置:以往代写 > C/C++ 教程 >Qt Project源码布局阐明
2019-06-13

Qt Project源码布局阐明

Qt Project源码布局阐明

副标题#e#

在网上阅读了QT入门教程系列文章,感激豆豆博客的版主,把这么好的教程和各人分享,本文是对入门教程的条记,以期抛砖引玉,听到各人的好看法。 但愿各人更好更快的进修QT,到达本身的方针,实现本身的抱负。

本文阐明QT项目标布局,如头文件中代码的布局与功能,主源代码文件的布局与功能。也就是说头文件中应该放些什么,源代码文件中放些什么。

先看一个经典的例子,头文件:

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

主函数:

#include <QApplication> 
     
#include "finddialog.h" 
     
int main(int argc, char *argv[]) 
{ 
        QApplication app(argc, argv); 
        FindDialog *dialog = new FindDialog; 
        dialog->show(); 
        return app.exec(); 
}


#p#副标题#e#

主源代码函数:

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

措施的运行功效是这样的:

Qt Project源码机关阐发

本文出自 “LinuxQt济南高新区” 博客,请务必保存此出处http://qtlinux.blog.51cto.com/3052744/571692

    关键字:

在线提交作业