标签:des style io ar color os sp 文件 on
Qt QMainWindow上添加QDockWidget窗口
<一>、建立一个标准的参数Dialog :StdDialog(QDockWidget)
(窗口(QWidget) -> LeftParamDlg/ RightParamDlg(QFrame)->StdDialog(QDockWidget))
窗口(QWidget) :designer设计
LeftParamDlg/ RightParamDlg(QFrame):刚才的窗口能够显示
StdDialog(QDockWidget):可以用来调度不同的LeftParamDlg/ LeftParamDlg。
1、设计窗口,创建一个QWidget窗口
2、显示每个窗口的类,继承QFrame
eg://LeftParamDlg.h
#ifndef LEFTPARAMDLG_H
#define LEFTPARAMDLG_H
#include "GeneratedFiles/ui_parameterleft.h"
#include <QFrame>
#include <QWidget>
class LeftParamDlg : public QFrame
{
Q_OBJECT
public:
LeftParamDlg(QWidget *parent = 0);
~LeftParamDlg();
private:
Ui::LeftParamWidget *ui;
};
#endif
//LeftParamDlg.cpp
#include "leftparamdlg.h"
LeftParamDlg::LeftParamDlg(QWidget *parent)
:QFrame(parent)
{
ui = new Ui::LeftParamWidget;
LeftParamDlg::ui->setupUi(this);
}
LeftParamDlg::~LeftParamDlg()
{
delete ui;
ui = NULL;
}
同理,如果有RightPramDlg也这样写一个类来进行显示
3、标准对话框调用:StdDialog(QDockWidget),继承自QDockWidget
eg://StdParamDialog.h
#ifndef CPARAMETERDIALOG_H
#define CPARAMETERDIALOG_H
#include <QDockWidget>
class QFrame;
class LeftParamDlg;
class RightParamDlg;
class CParameterDialog : public QDockWidget
{
Q_OBJECT
public:
CParameterDialog(QWidget *parent = 0);
~CParameterDialog();
bool showLeftPrmDlg();
bool showRightPrmDlg();
private:
void init();
void createFrame();
void loadLeftPrmDlg();
void loadRightPrmDlg();
private:
QFrame *mainFrame_;
LeftParamDlg *leftPrmDlg_;
RightParamDlg *rightPrmDlg_;
};
#endif
//StdParamDialog.cpp
#include "parameterdialog.h"
#include "leftparamdlg.h"
#include "rightparamdlg.h"
#include <QFrame>
#include <QGridLayout>
CParameterDialog::CParameterDialog(QWidget *parent)
: QDockWidget(parent)
{
init();
}
CParameterDialog::~CParameterDialog()
{
if (leftPrmDlg_)
delete leftPrmDlg_;
leftPrmDlg_ = NULL;
if (rightPrmDlg_)
delete rightPrmDlg_;
rightPrmDlg_ = NULL;
if (mainFrame_)
delete mainFrame_;
mainFrame_ = NULL;
}
bool CParameterDialog::showLeftPrmDlg()
{
createFrame();
loadLeftPrmDlg();
return true;
}
bool CParameterDialog::showRightPrmDlg()
{
createFrame();
loadRightPrmDlg();
return true;
}
void CParameterDialog::loadLeftPrmDlg()
{
//Grid和VBox都可以
//assert(mainFrame_);
mainFrame_->hide();
//QGridLayout *gridLayout = new QGridLayout(mainFrame_);
//mainFrame_->setLayout(gridLayout);
//setWindowTitle("Left Parameters");
//leftPrmDlg_ = new LeftParamDlg(this);
//gridLayout->setRowMinimumHeight(1,400);
//gridLayout->setColumnMinimumWidth(0,200);
//gridLayout->addWidget(leftPrmDlg_,1,0,7,10);
QVBoxLayout *gridLayout = new QVBoxLayout(mainFrame_);
mainFrame_->setLayout(gridLayout);
setWindowTitle("Left Parameters");
leftPrmDlg_ = new LeftParamDlg(this);
gridLayout->addWidget(leftPrmDlg_);
mainFrame_->showNormal();
mainFrame_->adjustSize();
//set the minimum size so it will shrink down to the right size
setMinimumSize(mainFrame_->sizeHint());
showNormal();
adjustSize();
}
void CParameterDialog::loadRightPrmDlg()
{
//assert(mainFrame_);
mainFrame_->hide();
QGridLayout *gridLayout = new QGridLayout(mainFrame_);
mainFrame_->setLayout(gridLayout);
setWindowTitle("Right Parameters");
rightPrmDlg_ = new RightParamDlg(this);
//rightPrmDlg_->setFrameConent();
gridLayout->setRowMinimumHeight(2,100);
gridLayout->setColumnMinimumWidth(1,200);
gridLayout->addWidget(rightPrmDlg_,1,0,7,10);
mainFrame_->showNormal();
mainFrame_->adjustSize();
//set the minimum size so it will shrink down to the right size
setMinimumSize(mainFrame_->sizeHint());
showNormal();
adjustSize();
}
void CParameterDialog::init()
{
mainFrame_ = NULL;
leftPrmDlg_ = NULL;
rightPrmDlg_ = NULL;
}
void CParameterDialog::createFrame()
{
if (mainFrame_)
delete mainFrame_;
QFrame *newFrame = new QFrame;
setWidget(newFrame);
setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
mainFrame_ = newFrame;
}
<二>、action按钮与Dialog关联
在QMainWindow头文件中,声明不同的STDParaDialog类名,调用不同方法显示窗口
QMainWindow的头文件中
private:
CParameterDialog *leftParamDlg_;
CParameterDialog *rightParamDlg_;
private slots:
void showLeftParamDlg();
void showRightParamDlg();
QMainWindow的cpp文件中
leftParamDlg_ = NULL;
rightParamDlg_ = NULL;
connect(ui.actionParam_Left, SIGNAL(triggered()), this, SLOT(showLeftParamDlg()));
connect(ui.actionParam_Right, SIGNAL(triggered()), this, SLOT(showRightParamDlg()));
void CMainWindow::showLeftParamDlg()
{
if (leftParamDlg_ != NULL)
{
leftParamDlg_->close();
delete leftParamDlg_;
leftParamDlg_ = NULL;
}
leftParamDlg_ = new CParameterDialog(this);
leftParamDlg_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, leftParamDlg_, Qt::Horizontal);
leftParamDlg_->setFloating(false);
leftParamDlg_->hide();
leftParamDlg_->showLeftPrmDlg();
}
void CMainWindow::showRightParamDlg()
{
if (rightParamDlg_ != NULL)
{
rightParamDlg_->close();
delete rightParamDlg_;
rightParamDlg_ = NULL;
}
rightParamDlg_ = new CParameterDialog(this);
rightParamDlg_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, rightParamDlg_, Qt::Horizontal);
rightParamDlg_->setFloating(false);
rightParamDlg_->hide();
rightParamDlg_->showRightPrmDlg();
}
标签:des style io ar color os sp 文件 on
原文地址:http://www.cnblogs.com/hedaihong/p/4133888.html