在运行时在代码中调整其大小时,如何避免ockockwidget与主窗口中的其他对象重叠?

时间:2018-10-21 21:20:21

标签: c++ qt resize qdockwidget

我在qt GUI中创建了一个停靠小部件,并在其上添加了一个树表小部件。除了主菜单中的这些对象之外,我还有其他对象,例如QGraphicsView。在运行时,当我手动调整扩展坞的宽度大小时,它会用其对象推或拉主窗口,效果很好。当我在代码中调整停靠大小时,停靠及其对象与主窗口的对象重叠。我怎样才能解决这个问题?谢谢您的关注。

在头文件中:

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication.h"
#include <QtWidgets/QDockWidget>
#include <QtWidgets/QTreeWidget>
#include <QGraphicsView>
#include <QtWidgets/QPushButton>

class QtGuiApplication : public QMainWindow
{
    Q_OBJECT

public:
    QtGuiApplication(QWidget *parent = Q_NULLPTR);


private:
    Ui::QtGuiApplicationClass ui;
    QDockWidget *dockWidget;
    QWidget *dockWidgetContents;
    QTreeWidget *treeWidget;
    QGraphicsView* qGraph;
    QGraphicsScene* scene;
private slots:
    void worker_fn();
};

在源文件中:

#include "QtGuiApplication.h"

QtGuiApplication::QtGuiApplication(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    /// creat dock
    dockWidget = new QDockWidget(this);
    dockWidgetContents = new QWidget(); 
    dockWidget->setMinimumSize(QSize(50, 38));
    dockWidget->setWidget(dockWidgetContents);
    this->addDockWidget(static_cast<Qt::DockWidgetArea>(1), dockWidget);

    //// add tree table widget to dock
    treeWidget = new QTreeWidget(dockWidgetContents);

    //// creat an object in  main window
    qGraph = new QGraphicsView(ui.centralWidget);
    qGraph->setGeometry(QRect(70, 30, 300, 300));
    scene = new QGraphicsScene(qGraph);
    scene->setSceneRect(0, 0, 300, 300);
    qGraph->setScene(scene);
    qGraph->show();

    //////// creat push button to resize the dock
    QPushButton* btn_Ok = new QPushButton(ui.centralWidget);
    btn_Ok->setGeometry(QRect(340, 340, 75, 23));
    btn_Ok->setText("Ok");
    connect(btn_Ok, SIGNAL(clicked()), this, SLOT(worker_fn()));
}
void QtGuiApplication::worker_fn()
{
    QRect rect = dockWidget->geometry();
    dockWidget->resize(300, rect.height());
}

0 个答案:

没有答案