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;
}