我们现在的开发环境是VS2010+QT插件的方式,这个时候我想在某个工程里面添加一个dialog对话框,分三步走:
第一步:用QT Designer 新建一个.ui文件,命名为geostralayerNameChoice.ui,然后完成编辑。
第一:新增加一个dialog类,继承自 QDialog,比如class CGeoStraLayerNameChoiceDialog :public QDialog。
在类的头文件中添加如下代码:
/**
* @file GeoStraLayerNameChoiceDialog.h
* @brief
* @date 2015-5-22
* @author: hudongfang
*/
#ifndef PAI_FRAME_CGEOSTRALAYERNAMECHOICEDIALOG_H
#define PAI_FRAME_CGEOSTRALAYERNAMECHOICEDIALOG_H
#pragma warning( push ,0 )
#include <QDialog>
#include "OSGFramework.h"
#include "GeologicalStratificationTree.h"
#include "GeologicalStratificatonObject.h"
#include "DialogFactory.h"
#pragma warning( pop )
namespace Ui
{
class GeoStraLayerNameChoiceDialog;
}
using namespace pai::datamodel;
BEGIN_OSGGRAPHICS_NAMESPACE
class CGeoStraLayerNameChoiceDialog :public QDialog,public CDialogFactory
{
Q_OBJECT
public:
CGeoStraLayerNameChoiceDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
~CGeoStraLayerNameChoiceDialog(void);
private slots:
void slotOK();
void slotCancel();
//void slotEdit();
virtual QString GetGuid();
private:
Ui::GeoStraLayerNameChoiceDialog* m_pUI;
//层位级别树
CGeologicalStratificationTree* m_pDataTree_GeoStra;
};
END_OSGGRAPHICS_NAMESPACE
//using namespace pai::graphics;
#endif
在源文件的构造函数中添加如下代码:
#include <QHBoxLayout>
#include "GeoStraLayerNameChoiceDialog.h"
#include "ui_GeoStraLayerNameChoice.h"
#include "GeologicalStratificationRoot.h"
#include "OSGDataModel.h"
#include "GeologicalStratificationDialog.h"
#include "ObjProject.h"
#include "BaseObject.h"
using namespace pai::datamodel;
BEGIN_OSGGRAPHICS_NAMESPACE
CGeoStraLayerNameChoiceDialog::CGeoStraLayerNameChoiceDialog(QWidget *parent, Qt::WFlags flags)
:QDialog(parent,flags),
m_pDataTree_GeoStra(NULL)
{
m_pUI = new Ui::GeoStraLayerNameChoiceDialog();
m_pUI->setupUi(this);
//地质分层命名树
m_pDataTree_GeoStra = new CGeologicalStratificationTree(this);
m_pDataTree_GeoStra->InitTreeType(eTree_GeologicalStratification);
//SetGeoStraTree(m_pDataTree_Geological);
QHBoxLayout * hLayout = new QHBoxLayout();
hLayout->addWidget(m_pDataTree_GeoStra);
hLayout->setContentsMargins(10,10,10,10);
m_pUI->groupBox_layerName->setLayout(hLayout);
if( GetProject() )
{
m_pDataTree_GeoStra->SetRootObject(GetProject()->GetGeologicalStratificationRoot(true));
PaiObject *pObject = GetProject()->GetGeologicalStratificationRoot(true);
QList<PaiObject *> listChild;
pObject->GetChildren(listChild);
PaiObject::m_eventAgent.OnObjectAddChildren( pObject,listChild);
}
QObject::connect(m_pUI->pushButton_OK, SIGNAL(clicked()), this, SLOT(slotOK()) );
QObject::connect(m_pUI->pushButton_CANCEL, SIGNAL(clicked()), this, SLOT(slotCancel()) );
//QObject::connect(m_pUI->pushButton_Edit, SIGNAL(clicked()), this, SLOT(slotEdit()) );
}
CGeoStraLayerNameChoiceDialog::~CGeoStraLayerNameChoiceDialog(void)
{
delete m_pUI;
m_pUI = NULL;
delete m_pDataTree_GeoStra;
m_pDataTree_GeoStra = NULL;
}
void CGeoStraLayerNameChoiceDialog::slotOK()
{
QTreeWidgetItem * pTreeItem = m_pDataTree_GeoStra->currentItem();
QString strName = pTreeItem->text(0);
this->setWindowTitle(strName);
QDialog::accept();//注意此处如何调用基类的确定代码
}
void CGeoStraLayerNameChoiceDialog::slotCancel()
{
QDialog::reject();//注意此处如何调用积累的取消代码(关闭对话框)
}
附:
CSelectDlg dlg;
int res = dlg.exec();
if (res == QDialog::Accepted) isOilLayer = true;
if (res == QDialog::Rejected) isOilLayer = false;
//注意 QDialog::accept()、QDialog::reject()、 QDialog::Accepted、 QDialog::Rejected