1

嗨,我创建了一个 dockwidget,我将它作为隐藏启动。问题是之后我无法显示它,而我可以从 isHidden() 函数中正确获取状态。奇怪的是,如果我启动不隐藏的dockwidget,它就完美了。我包括一个重现这种奇怪行为的例子。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:

    void showDock();
private:
    Ui::MainWindow *ui;

    QDockWidget *dock;
    QPushButton *button;


};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    QMainWindow mainWindow;
//    QDockWidget *dock = new QDockWidget(&mainWindow);
    QDialog *dockDialog = new QDialog(this); // <---------edit: you need to create a parent widget for the dock
    dock = new QDockWidget(dockDialog);

    dock->setStyleSheet("QDockWidget { font: bold }");
    dock->setFloating(true);
    dock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
    QAbstractButton *floatButton = dock->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");
    if(floatButton)
        floatButton->hide();

    dock->setAllowedAreas( Qt::NoDockWidgetArea );
    dock->setWindowTitle("Tools");

    this->addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);

    QMainWindow *window = new QMainWindow(dock); // <------edit: set the dock to be the parent for the window
    window->setWindowFlags(Qt::Widget);
    QToolBar *bar = new QToolBar(window);

    bar->setMovable(false);
    bar->addAction("Select");
    bar->addAction("Polygon");
    bar->addAction("Brush");
    bar->addAction("Erazer");
    bar->addSeparator();
    bar->addAction("Mark");
    bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    bar->setOrientation(Qt::Vertical);

    window->addToolBar(Qt::LeftToolBarArea, bar);
    window->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    window->setParent(dock);
    dock->setWidget(window);
    dock->hide();            // <------------ comment this line and it will work, edit: you do not need to do that anymore, it is working nicely now

    button = new QPushButton("show", this);
    button->setCheckable(true);

    QObject::connect(button, SIGNAL(clicked()), this, SLOT(showDock()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::showDock()
{
//    qDebug() << "hello";
    if(button->isChecked()){
        if(dock->isHidden()){
            qDebug() << "hidden";
        dock->setFloating(true); // <-----edit: you need to add these lines in order to be able to see the dialog that contains the dock widget, i do not know why i need to that again since in the initialization i already specifying that my dock is floatable
        QAbstractButton *floatButton = dock->findChild<QAbstractButton*>("qt_dockwidget_floatbutton"); // <---------edit: add this lines in order to get rid off the floating button
        if(floatButton)
            floatButton->hide(); // <----edit: the same as previous
        dock->show();
        }
    }

    if(!button->isChecked()){
        if(!dock->isHidden()){
            qDebug() << "not hidden";
            dock->hide();
        }
    }
}

因为现在是上面的代码,所以 dockwidget 没有出现在屏幕上。如果您尝试评论我在 mainwindow.cpp 中指定的行,它可以工作,但关键是我想启动隐藏的 dockwidget。有人知道发生了什么吗?

谢谢。

4

2 回答 2

0

控件的父级QDock应该是您的window小部件,而不是MainWindow类的对象。

所以,你需要替换这个:

 dock = new QDockWidget(this);

有了这个:

QMainWindow *window = new QMainWindow(0); // I smell a potential leak here (set the parent!)
dock = new QDockWidget(window);

在第一种情况下,当您按下“显示”按钮时,控件也是可见的,但它位于窗口外的某个位置,因此您可能看不到它。此外,无需创建QMainWindow已经继承的内部类的单独实例QMainWindow

于 2013-02-06T00:31:40.417 回答
0

我看到你显示()/隐藏()你的码头小部件。我自己没有尝试过,但也许您应该使用QDockWidget::toggleViewAction方法隐藏和显示dockwdiget。

于 2013-02-14T14:50:55.783 回答