vs2005集成qt后产生的qt项目文件结构分析

来源:互联网 发布:lumia软件恢复工具 编辑:程序博客网 时间:2024/06/07 18:31
首先,假设我们使用集成了qt4的vc2005开发环境产生了一个ss的qt应用程序。然后双击左边的解决方案资源管理器的ss.ui,看到一个可视化的程序主窗口,可以通过控件窗口向该程序主窗口加入控件,编译。然后我们开始分析项目文件结构。
1、ss.h头文件中有一个私有成员变量 Ui::ssClass ui;这个成员变量ui就是在主窗口上进行的可视化控件操作产生的新主窗口所关联的变量(该主窗口对应于QWidget对象)。然后我们追踪ssClass类定义,在ui_ss.h中可以找到
namespace Ui {
    class ssClass: public Ui_ssClass {};

同样,在该文件中也有完整的Ui_ssClass的类定义,其成员变量包含该对话框包含的所有控件类型。
2、在ss.cpp实现文件中,调用ui.setupUi(this);来生成这个对话框对象,同样在ui_ss.h中我们可以找到该函数的完整定义,分析这段代码,很容易我们可以看出是将所有添加的控件和布局以子窗口的形式放置到一个widget中,然后再以该widget为子对象,而QDialog为父对象生成完整的窗口。
3、采取这种结构最大的好处在于可视化生成窗口的外观,然后将该外观整体作为一个类对象嵌入到对话框主窗口中,因为这样还可以对对话框类进行其他的设计(譬如添加一个成员变量来获得其控件个数等等)。这样就实现的可视化的窗口设计和对(对话框)窗口继承类的重新设计(调价成员,函数,槽等等)的分离。
4、注意Generated Files文件夹下的文件是由可视化窗口直接生成的,所以不能对其进行直接的代码修改。

附上相关代码:
ss.h
#ifndef SS_H
#define SS_H

#include <QtGui/QDialog>
#include "ui_ss.h"

class ss : public QDialog
{
    Q_OBJECT

public:
    ss(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~ss();
signals:
    void findNext(const QString &text, bool matchall);
    void findPrev(const QString &text, bool matchall);

private slots:
     void findClicked();
     void enableFindButton(const QString &text);
     void showNext(const QString &text, bool matchall);
     void showPrev(const QString &text, bool matchall);

private:
    Ui::ssClass ui;

private slots:
//    void on_pushButton_2_clicked();
//    void on_checkBox_2_stateChanged(int);
//    void on_lineEdit_textChanged(const QString &);
};

#endif // SS_H

ss.cpp
#include "ss.h"
#include "ui_ss.h"
#include <QMessageBox>

ss::ss(QWidget *parent, Qt::WFlags flags)
    : QDialog(parent, flags)
{
    ui.setupUi(this);
    QObject::connect(ui.lineEdit, SIGNAL(textChanged(const QString &)),
        this, SLOT(enableFindButton(const QString &)));
    QObject::connect(ui.findButton, SIGNAL(clicked()),
        this, SLOT(findClicked()));
    QObject::connect(this, SIGNAL(findPrev(const QString &, bool)),
        this, SLOT(showPrev(const QString &, bool)));
    QObject::connect(this, SIGNAL(findNext(const QString &, bool)),
        this, SLOT(showNext(const QString &, bool)));
}

void ss::enableFindButton(const QString &text)
{
    ui.findButton->setEnabled(!text.isEmpty());
}
void ss::findClicked()
{
    QString text = ui.lineEdit->text();
    bool matchall = ui.caseCheckBox->isChecked();
    if (ui.backwardCheckBox->isChecked())
    {
        emit findPrev(text, matchall);
    }
    else
    {
        emit findNext(text, matchall);
    }
}

void ss::showNext(const QString &text, bool matchall)
{
    QMessageBox(QMessageBox::NoIcon, tr("Next"), tr("Find Next"));
}
void ss::showPrev(const QString &text, bool matchall)
{
    QMessageBox(QMessageBox::NoIcon, tr("Prev"), tr("Find Prev"));
}

ss::~ss()
{

}


ui_ss.h
/********************************************************************************
** Form generated from reading ui file 'ss.ui'
**
** Created: Tue Sep 25 12:14:19 2007
**      by: Qt User Interface Compiler version 4.2.2
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/

#ifndef UI_SS_H
#define UI_SS_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QCheckBox>
#include <QtGui/QDialog>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

class Ui_ssClass
{
public:
    QWidget *widget;
    QHBoxLayout *hboxLayout;
    QVBoxLayout *vboxLayout;
    QHBoxLayout *hboxLayout1;
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QVBoxLayout *vboxLayout1;
    QPushButton *findButton;
    QPushButton *closeButton;

    void setupUi(QDialog *ssClass)
    {
    ssClass->setObjectName(QString::fromUtf8("ssClass"));
    widget = new QWidget(ssClass);
    widget->setObjectName(QString::fromUtf8("widget"));
    widget->setGeometry(QRect(50, 90, 231, 75));
    hboxLayout = new QHBoxLayout(widget);
    hboxLayout->setSpacing(6);
    hboxLayout->setMargin(0);
    hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
    vboxLayout = new QVBoxLayout();
    vboxLayout->setSpacing(6);
    vboxLayout->setMargin(0);
    vboxLayout->setObjectName(QString::fromUtf8("vboxLayout"));
    hboxLayout1 = new QHBoxLayout();
    hboxLayout1->setSpacing(6);
    hboxLayout1->setMargin(0);
    hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1"));
    label = new QLabel(widget);
    label->setObjectName(QString::fromUtf8("label"));

    hboxLayout1->addWidget(label);

    lineEdit = new QLineEdit(widget);
    lineEdit->setObjectName(QString::fromUtf8("lineEdit"));

    hboxLayout1->addWidget(lineEdit);


    vboxLayout->addLayout(hboxLayout1);

    caseCheckBox = new QCheckBox(widget);
    caseCheckBox->setObjectName(QString::fromUtf8("caseCheckBox"));

    vboxLayout->addWidget(caseCheckBox);

    backwardCheckBox = new QCheckBox(widget);
    backwardCheckBox->setObjectName(QString::fromUtf8("backwardCheckBox"));

    vboxLayout->addWidget(backwardCheckBox);


    hboxLayout->addLayout(vboxLayout);

    vboxLayout1 = new QVBoxLayout();
    vboxLayout1->setSpacing(6);
    vboxLayout1->setMargin(0);
    vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1"));
    findButton = new QPushButton(widget);
    findButton->setObjectName(QString::fromUtf8("findButton"));
    findButton->setEnabled(false);

    vboxLayout1->addWidget(findButton);

    closeButton = new QPushButton(widget);
    closeButton->setObjectName(QString::fromUtf8("closeButton"));

    vboxLayout1->addWidget(closeButton);


    hboxLayout->addLayout(vboxLayout1);


    retranslateUi(ssClass);

    QSize size(400, 300);
    size = size.expandedTo(ssClass->minimumSizeHint());
    ssClass->resize(size);

    QObject::connect(closeButton, SIGNAL(clicked()), ssClass, SLOT(close()));

    QMetaObject::connectSlotsByName(ssClass);
    } // setupUi

    void retranslateUi(QDialog *ssClass)
    {
    ssClass->setWindowTitle(QApplication::translate("ssClass", "ss", 0, QApplication::UnicodeUTF8));
    label->setText(QApplication::translate("ssClass", "Find", 0, QApplication::UnicodeUTF8));
    caseCheckBox->setText(QApplication::translate("ssClass", "Match &case", 0, QApplication::UnicodeUTF8));
    backwardCheckBox->setText(QApplication::translate("ssClass", "&Backward", 0, QApplication::UnicodeUTF8));
    findButton->setText(QApplication::translate("ssClass", "Find", 0, QApplication::UnicodeUTF8));
    closeButton->setText(QApplication::translate("ssClass", "Close", 0, QApplication::UnicodeUTF8));
    Q_UNUSED(ssClass);
    } // retranslateUi

};

namespace Ui {
    class ssClass: public Ui_ssClass {};
} // namespace Ui

#endif // UI_SS_H
原创粉丝点击