当前位置:首页 >> 编程开发 >> Visual C++ >> 内容

Qt Project源码结构分析

时间:2013/11/4 作者:平凡之路 来源:xuhantao.com 浏览:

在网上阅读了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(); 
}

主源代码函数:

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

程序的运行结果是这样的:

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

相关文章
  • 没有相关文章
  • 徐汉涛(www.xuhantao.com) © 2024 版权所有 All Rights Reserved.
  • 部分内容来自网络,如有侵权请联系站长尽快处理 站长QQ:965898558(广告及站内业务受理) 网站备案号:蒙ICP备15000590号-1