Cant set Height or SizePolicy for QToolbar
-
i have added a QToolBar manually, because in Qt Designer i didnt found an Option to use Layouts in the QToolBar. I have 2 QToolbars in a vertical Layout at the left Side. One QToolBar has Top Alignment and the other Bottom. The Problem is that the Top Bar dont stretch to the bottom Bar. I have some empty Space. i tried to set QSizePolicy Expanding, Preferred and MinimumExpanding. I used setFixedHeight(), but nothing seems to change the Size of the Bar.
This is my actually Code. I nearly tried everything
QString stylesheet = "QToolBar { background-color: blue; }"; QToolBar *barTop = new QToolBar(this); barTop->setMovable(false); barTop->setFixedWidth(60); barTop->setFixedHeight(200); barTop->setOrientation(Qt::Vertical); barTop->setStyleSheet(stylesheet); barTop->setIconSize(QSize(45, 45)); barTop->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); QToolBar *barBottom = new QToolBar(this); barBottom->setMovable(false); barBottom->setFixedWidth(60); barBottom->setOrientation(Qt::Vertical); barBottom->setStyleSheet(stylesheet); barBottom->setIconSize(QSize(45, 45)); QAction *a_Home = new QAction("Home"); a_Home->setIcon(QIcon(":/icons/images/home.png")); a_Home->setToolTip("Übersicht"); barTop->addAction(a_Home); QAction *a_Diary = new QAction("Diary"); a_Diary->setIcon(QIcon(":/icons/images/diary.png")); a_Diary->setToolTip("Übersicht"); barTop->addAction(a_Diary); QWidget *spacer = new QWidget(this); spacer->setMinimumHeight(300); spacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); barTop->addWidget(spacer); QAction *a_Quit = new QAction("Quit"); a_Quit->setIcon(QIcon(":/icons/images/quit.png")); a_Quit->setToolTip("Beenden"); barBottom->addAction(a_Quit); QVBoxLayout *mainMenuLayout = new QVBoxLayout(); mainMenuLayout->addWidget(barTop); mainMenuLayout->addWidget(barBottom); mainMenuLayout->setAlignment(barTop, Qt::AlignTop); mainMenuLayout->setAlignment(barBottom, Qt::AlignBottom); mainMenuLayout->setContentsMargins(0, 0, 0, 0); mainMenuLayout->setSpacing(0); QWidget *central = new QWidget(this); central->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); central->setLayout(mainMenuLayout); setCentralWidget(central); this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-
I moved the Code in an explizit QWidget Class and changed that now
QWidget *spacer = new QWidget(this); spacer->setMinimumHeight(3000); spacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); barTop->addWidget(spacer);
It seems that it works. But the Solution to set the Height so big that the spacer goes out of screen isnt that clean. Any other Ideas?
-
Sorry for writing again, but i really dont like the Solution out of my last Post. Still have the Problem, that i cant expand my QToolBar. As i mentioned, i have added a QWidget as a Spacer to my QToolBar and added the QSizePolixy::Expanding, but that still dont work. Please help somebody.
-
Hi, so you just want your top bar to stretch all the way down? If so then you overcomplicated it. You don't need any spacers. From your original code remove the following lines:
barTop->setFixedHeight(200)
It's obviously not gonna stretch if it has fixed height.
mainMenuLayout->setAlignment(barTop, Qt::AlignTop); mainMenuLayout->setAlignment(barBottom, Qt::AlignBottom);
Alignment is for placing widgets inside their layout cell, not in the layout itself. Setting
Qt::AlignTop
to the top toolbar means that it will not expand, but have the preferred size and stick to the top border of its cell in the layout. This is what's causing your problem.QWidget *spacer = new QWidget(this); spacer->setMinimumHeight(300); spacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); barTop->addWidget(spacer);
As I mentioned - this is not needed.
central->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
These two are just unnecessary. There are no other widgets around so
central
will take any available space anyway andthis
is (I assume) the top level window so it's not even in a layout and this is pointless.Finally you need to set your policies correctly. You want your bottom bar to have preferred height and your top one to expand so set it up like this:
barTop->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); barBottom->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);