QT中的namespace Ui的理解

来源:互联网 发布:linux 监听udp端口 编辑:程序博客网 时间:2024/05/18 02:07

接下来谈谈namespace Ui
                    { 
                        class Dialog: public Ui_Dialog {}; 
                    }

/********************************************/
                           dialog.h
/********************************************/
#ifndef DIALOG_H 
#define DIALOG_H 

#include <QtGui/QDialog> 

namespace Ui 

class Dialog; 


class Dialog : public QDialog 

Q_OBJECT 

public: 
Dialog(QWidget *parent = 0); 
~Dialog(); 

private: 
Ui::Dialog *ui; 
}; 

#endif // DIALOG_H 
/*********************************************/
                            ui_dialog.h
/*****************************************************************************/
/******************************************************************************** 
** Form generated from reading ui file 'dialog.ui' 
** 
** Created: Thu May 14 22:52:58 2009 
** by: Qt User Interface Compiler version 4.5.0 
** 
** WARNING! All changes made in this file will be lost when recompiling ui file! 
********************************************************************************/ 

#ifndef UI_DIALOG_H 
#define UI_DIALOG_H 

#include <QtCore/QVariant> 
#include <QtGui/QAction> 
#include <QtGui/QApplication> 
#include <QtGui/QButtonGroup> 
#include <QtGui/QDialog> 
#include <QtGui/QHeaderView> 
#include <QtGui/QListView> 
#include <QtGui/QPushButton> 

QT_BEGIN_NAMESPACE 

class Ui_Dialog 

public: 
QListView *listView; 
QPushButton *pushButton
 

void setupUi(QDialog *Dialog) 

if (Dialog->objectName().isEmpty()) 
Dialog->setObjectName(QString::fromUtf8("Dialog")); 
Dialog->resize(600, 400); 
listView = new QListView(Dialog); 
listView->setObjectName(QString::fromUtf8("listView")); 
listView->setGeometry(QRect(30, 10, 256, 192)); 
pushButton = new QPushButton(Dialog); 
pushButton->setObjectName(QString::fromUtf8("pushButton")); 
pushButton->setGeometry(QRect(140, 280, 75, 23)); 

retranslateUi(Dialog); 

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

void retranslateUi(QDialog *Dialog) 

Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8)); 
pushButton->setText(QApplication::translate("Dialog", "bye", 0, QApplication::UnicodeUTF8)); 
Q_UNUSED(Dialog); 
} // retranslateUi 

}; 

namespace Ui { 
class Dialog: public Ui_Dialog {}; 
} // namespace Ui 

QT_END_NAMESPACE 

#endif // UI_DIALOG_H 
/*******************************************************************************/
                        
                               dialog.cpp
/*******************************************************************************/
#include "dialog.h" 
#include "ui_dialog.h" 

Dialog::Dialog(QWidget *parent) 
: QDialog(parent), ui(new Ui::Dialog) 

ui->setupUi(this); 
//QObject::connect(ui->pushButton, SIGNAL(clicked()),this, SLOT(quit())); 


Dialog::~Dialog() 

delete ui; 
}
/********************************************************************************/

ui_dialog.h

代码中有很多被硬编码的地方:
listView->setGeometry(QRect(30, 10, 256, 192)); 
pushButton = new QPushButton(Dialog); 
pushButton->setObjectName(QString::fromUtf8("pushButton")); 
pushButton->setGeometry(QRect(140, 280, 75, 23)); 

designer生成的这个东西, 如何让程序的其他代码去使用呢?

最直接的, 它应该产生一个:
class Ui_Dialog {
QListView *listView; 
QPushButton *pushButton;
};

这样的类去让其他代码使用:

// My.h
#include "ui_dialog.h"
class My
        {
            Ui_Dialog dlg;
        };


// My.cpp
#include "My.h"
// 实现My

但是这样存在问题, 如果ui_dialog.h文件的内容被改变, 不但My.cpp会被重新编译,
所有包含My.h的文件也都会被重新编译。
而且这确实是一个问题: designer确实经常被拖来拖去。


如果产生ui_dialog.h的那个程序能将如下代码:
listView->setGeometry(QRect(30, 10, 256, 192)); 
pushButton = new QPushButton(Dialog); 
pushButton->setObjectName(QString::fromUtf8("pushButton")); 
pushButton->setGeometry(QRect(140, 280, 75, 23)); 

移动到一个ui_dialog.cpp 中, 至少在移动dlg上的那些界面元素时,  只会重新编译ui_dialog.cpp。
不会修改ui_dialog.h, 也就不会引发另一连串重编译。

但是, 除了将界面元素拖来拖去, designer还经常干的一些事就是添加,删除一些界面元素,如:
class Ui_Dialog 

public: 
QListView *listView; 
QPushButton *pushButton;
// ...
};

这样 ui_dialog.h 文件还是得改变。
如何让designer改变GUI外观后, 不会引发工程大范围的重新编译?

所以designer使用了pimpl手法 ……

前置声明一个 Ui:: Dialog类
namespace Ui  


      class Dialog; 


class Dialog : public QDialog  


      Ui:: Dialog *ui;  // 使用该类的一个指针
}; 

然后用户使用 dialog.h 头文件以及 Dialog类。
该文件被修改的频率就会低很多很多。
无论是将designer上的界面元素拖来拖去, 还是添加删除, dialog.h文件的内容——Dialog类的定义——都不会改变。

然后用户可以使用这个Dialog了:

#include "dialog.h"
class My 
{
    Dialog dlg;
};

 

 

Ui 创建两种不同的方式:
1. 在qt4中使用了继承的方式来使用designer创建的窗体,也就是同时继承QDialog和UI_Dialog

 

2. 而在Qt Creator自动创建的项目中,使用了组合的方式来使用Designer创建的窗体,就是集成QDialog,

而将UI_Dialog作为一个成员变量来使用,也就是
private:
Ui::Dialog *ui;

 


区别:

在前一种方式中,你可以在继承类中直接使用UI_Dialog上的组件。

在后一种方式中,你要使用ui->XXX的方式使用UI_Dialog上的组件。

两种方式都可以,但个人感觉第二种好一些,毕竟组合比集成的耦合度来的弱一些,就是稍有点麻烦,要加ui->,但同时也带来了更清晰的代码结构

原创粉丝点击