I don't want the QDockwidget to resize when I dynamically add a toolbutton to QMainWindow's mainToolBar

烈酒焚心 提交于 2019-12-25 00:18:37

问题


Each time I dynamically add a button to the mainToolBar of a QMainWindow, the QDockWidget resizes to its original size.

My question:

  • Can I prevent the QDockWidget from auto resizing and how do I do this?
  • If I cannot do that what other options are open to me? 'resizeDocks' perhaps?

This is an illustration of my dilemma. This is a sample app on startup:

Now I manually resize the QDockWindow.

Now I press 'F1' which creates a toolbutton on QMainWindow's mainToolBar and the QDockWindow auto resizes to its original size.

This is the code for this simple app:

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QKeyEvent>
#include <QInputDialog>
#include <QToolButton>
#include <QPlainTextEdit>
#include <QDockWidget>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;    
    QToolButton *Create_toolbutton(QString);

protected:
    void keyPressEvent(QKeyEvent*);

};

#endif // MAINWINDOW_H

mainwindow.cpp:

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

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

    this->setCentralWidget(new QPlainTextEdit());
    this->addDockWidget(Qt::RightDockWidgetArea, new QDockWidget());
}

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

void MainWindow::keyPressEvent(QKeyEvent * ke)
{

    if(ke->key() == Qt::Key_F1){
        ui->mainToolBar->addWidget(Create_toolbutton("new button"));
    }

    QMainWindow::keyPressEvent(ke);
}

QToolButton * MainWindow::Create_toolbutton(QString toolbutton_name)
{
    QToolButton * new_qtoolbutton = new QToolButton();
    new_qtoolbutton->setText(toolbutton_name);
    new_qtoolbutton->setArrowType(Qt::ArrowType::RightArrow);
    new_qtoolbutton->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextBesideIcon);
    return new_qtoolbutton;
}

回答1:


I am using resizeDocks and it is working sufficiently for me.

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

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

    this->setCentralWidget(new QPlainTextEdit());
    auto * dock = new QDockWidget();
    this->addDockWidget(Qt::RightDockWidgetArea, dock);
    resizeDocks({dock}, {100}, Qt::Horizontal);
}

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

void MainWindow::keyPressEvent(QKeyEvent * ke)
{
    if(ke->key() == Qt::Key_F1){
        ui->mainToolBar->addWidget(Create_toolbutton("new button"));
    }

    QMainWindow::keyPressEvent(ke);
}

QToolButton * MainWindow::Create_toolbutton(QString toolbutton_name)
{
    QToolButton * new_qtoolbutton = new QToolButton();
    new_qtoolbutton->setText(toolbutton_name);
    new_qtoolbutton->setArrowType(Qt::ArrowType::RightArrow);
    new_qtoolbutton->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextBesideIcon);
    return new_qtoolbutton;
}


来源:https://stackoverflow.com/questions/55117831/i-dont-want-the-qdockwidget-to-resize-when-i-dynamically-add-a-toolbutton-to-qm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!