当前位置: 代码迷 >> QT开发 >> QMainWindow中自定义槽函数控制QDockWidget的显示有关问题
  详细解决方案

QMainWindow中自定义槽函数控制QDockWidget的显示有关问题

热度:210   发布时间:2016-04-25 05:08:58.0
QMainWindow中自定义槽函数控制QDockWidget的显示问题
下面是定义的一个测试QMainWindow的类,在这个类里我们来测试一下QObject::connect(scrollBar, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));

其实这个测试demo也是延续我着我上一个帖子尚未解决的问题的,设计的测试思路是这样的,在QMainWindow的UI文件中画上菜单,同时在QMainWindow类文件定义QDockWiget,目的就是通过菜单的响应来测试connect函数,具体类定义如下:

//deviceinfoview.h
class DeviceInfoView : public QMainWindow {

Q_OBJECT

public:

DeviceInfoView(QWidget *parent = 0);
~DeviceInfoView(); 

private:

void initDockWiget();

private:

Ui::DeviceInfoView *ui;
QDockWidget *dockWidget;
QDirModel *model;
QTreeView * treeView;

signals:

void setShowDockingSignals(bool shown); 

private slots:

void setShowDockingSlot(bool shown);

};
类定义就不在详细解释,主要注意一下,我们自定义了信号void setShowDockingSignals(bool shown)和自定义了槽函数void setShowDockingSlot(bool shown)。 

下面看源文件: ////deviceinfoview.cpp
DeviceInfoView::DeviceInfoView(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::DeviceInfoView)
{

ui->setupUi(this);
model = new QDirModel;
model->setReadOnly(false);
model->setSorting(QDir::DirsFirst|QDir::IgnoreCase|QDir::Name);
ui->deviceTreeView->setModel(model);
ui->deviceTreeView->header()->setStretchLastSection(true);
ui->deviceTreeView->header()->setSortIndicator(0,Qt::AscendingOrder);
ui->deviceTreeView->header()->setSortIndicatorShown(true);
ui->deviceTreeView->header()->setClickable(true); 

QModelIndex index = model->index(QDir::rootPath());
ui->deviceTreeView->expand(index);
ui->deviceTreeView->scrollTo(index);
ui->deviceTreeView->resizeColumnToContents(0);
QHBoxLayout *btnLayout = new QHBoxLayout;
QVBoxLayout *mainLayout = new QVBoxLayout(this); 

mainLayout->addWidget(ui->deviceTreeView);
mainLayout->addLayout(btnLayout);
this->setLayout(mainLayout);
///this->setCentralWidget(ui->deviceTreeView);
ui->actionDocking->setCheckable(true); //将设备器工具栏菜单设置为可复选
ui->actionDocking->setChecked(true); //默认为选中
ui->actionDocking->setVisible(true); //默认为显示设备器工具栏 

initDockWiget(); 

connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(setShowDocking()));
connect(ui->actionDocking, SIGNAL(triggered(bool)),dockWidget, SLOT(setShown(bool)));

}//end of construction DeviceInfoView 

DeviceInfoView::~DeviceInfoView()
{

delete ui;

}//end of distruciton DeviceInfoView 

void DeviceInfoView::initDockWiget(){ 

QWidget *dv = new QLabel("Docking");
dockWidget = new QDockWidget("Test",this);
dockWidget->setAllowedAreas(Qt::RightDockWidgetArea);
dockWidget->setFeatures(QDockWidget::AllDockWidgetFeatures);
dockWidget->setFloating(false);
dockWidget->setWidget(dv);
dockWidget->setVisible(true);
addDockWidget(Qt::RightDockWidgetArea,dockWidget); 

}//end of method initDockWiget 

void DeviceInfoView::setShowDockingSlot(bool shown){

dockWidget->setShown(shown);

}//end of slots setShowDockingSlot 

以上是完整的源文件,我测试的时候,点击菜单按钮,能够正常的隐藏和显示dockWidget,但是当我将connect(ui->actionDocking, SIGNAL(triggered(bool)),dockWidget, SLOT(setShown(bool)));这一句的SLOT换成我自定义的槽函数setShowDockingSlot(bool shown)的时候,dockWidget就无法正常隐藏和显示了,点击菜单毫无反应。 

请问各位达人,在自定义的槽函数中同样是通过QDockWidget对象dockWidget来调用了setShown(bool),为何无法正常控制dockWidget的隐藏和显示呢? 

QT Creator 在ubuntu下编写的源代码已上传我的资源 http://download.csdn.net/source/2101907。 

期待与各位热心达人交流! 

QQ453489997

------解决方案--------------------
啊,还是那个问题吗?
我觉得是你用指针传给下一个对象时,指的不是你做要的那个对象