Put QMenuBar at QMainWindow Bottom in QT - qt

Is there any way to put a QMenuBar at screen bottom (I mean, at QMainWindow bottom)?
I'm working on my thesis project, and my director asked me to put a QMenuBar at screen bottom. Is this possible?, I have been trying adjusting the menubar geometry. In Qt Designer I can move the bar position, but when I run my project, the menu bar is always up.
Thanks in advance.

Don't use the default QMenuBar provided with the QMainWindow. Instead create your own. This proof of concept example creates a new QMenuBar which is added to a QVBoxLayout which was added to the mainwindow:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMenuBar* bar = new QMenuBar(this);
ui->verticalLayout->addWidget(bar);
QMenu* menu1 = new QMenu("First menu", bar);
menu1->addMenu("Foo");
menu1->addMenu("Bar");
QMenu* menu2 = new QMenu("Second menu", bar);
menu2->addMenu("Foo");
menu2->addMenu("Bar");
bar->addMenu(menu1);
bar->addMenu(menu2);
}
This works at least in Windows.

I've placed menus in the QDockWidget so I assume it is also possible to place menu bar at the bottom.
But you must do it programmaticaly. QMenuBar inherits QWidget, so just add a QWidget at the bottom of QMainWindow, then create a QMenuBar specifying this QWidget as a parent widget.

Related

How to expand layout in scrollarea with added content

I have a ScrollArea which contains a frame to hold a layout. By pressing a button (Plusbutton below) I add new Widgets to the layout in the ScrollArea.
After adding several Items the Widgets get smaller and cut off. I want the layout to get bigger with added Widgets and want be able to scroll further down to see the added content.
[Edit]
I have constructed those widgets in Qt designer so i cant provide that much code. But i hope this will help you understand my problem.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->setWidgetResizable(true);
ui->verticalLayout_6->insertWidget(0,ui->blueframe,Qt::AlignTop);
}
void MainWindow::on_addwidget_pressed()
{
ui->verticalLayout_6->insertWidget(2,new Toolbar);
}

Menubar is not showing on the second window

I am trying to build an application that after getting some user input on the first window, pops up another window and displays some results. However, even though the menubar is visible on the first window, the menubar does not appear on the second window. The two windows are objects of different classes, but both classes are inherited from QMainWindow.
I have tried using the menuBar() function which returns a pointer for the menubar to add menus (this works for the first window). I also tried creating a new menubar object which didn't help either.
//MapWindow.h
class MapWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MapWindow(QWidget *parent = nullptr);
~MapWindow();
private:
QAction *vehicleAct;
QAction *missionAct;
QAction *backAct;
QMenu *toolMenu;
};
//MapWindow.cpp
MapWindow::MapWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MapWindow)
{
ui->setupUi(this);
setWindowState(Qt::WindowMaximized);
vehicleAct = new QAction("Vehicle Selection");
vehicleAct->setShortcut(Qt::CTRL + Qt::Key_V);
missionAct = new QAction("Mission Selection");
missionAct->setShortcut(Qt::CTRL + Qt::Key_M);
backAct = new QAction("Back");
backAct->setShortcut(Qt::CTRL + Qt::Key_B);
toolMenu = menuBar()->addMenu("Tools");
toolMenu->addAction(vehicleAct);
toolMenu->addAction(missionAct);
toolMenu->addAction(backAct);
}
MapWindow::~MapWindow() {
delete ui;
}
When I use the same code in the WelcomeWindow class which is also inherited from QMainWindow it works perfectly. However it doesn't even show a menubar in this second window.
I managed to find the problem. One of my widgets (QScrollArea) was located in the top left corner of the screen which was stopping the whole menubar from displaying for some reason. Moving the QScrollArea down a little bit has solved the problem.

Making a layout in Qt

I just begin to work on making a layout with Grid. I tried to make it using the HBoxlayout and VBoxlayout. But How to set the position of the layout. I searched out and I found the setAlignment option but it isn't working on it.
So how to do its postioning of layouts like the image?
this is the layout I want to make
check this out
#include "mainscreen.h"
#include "ui_mainscreen.h"
#include<QLayout>
#include<QPushButton>
MainScreen::MainScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainScreen)
{
ui->setupUi(this);
QGridLayout *layout=new QGridLayout;
QHBoxLayout *hlayout=new QHBoxLayout;
QPushButton *x=new QPushButton;
hlayout->setAlignment(Qt::AlignTop);
hlayout->addWidget(x);
layout->addChildLayout(hlayout);
this->setLayout(layout);
this->show();
}
MainScreen::~MainScreen()
{
delete ui;
}
You can find good information about Layout in this Tutorial from Qt: http://doc.qt.io/qt-5/qtwidgets-layouts-basiclayouts-example.html
Two things that are important for your Layout:
You need to nest several layout. Themain layout will be a VBoxlayout that contains the top and bottom Widget and one HBoxLayout in the middle. The HBoxLayout contain the other two widgets.
Two get different size for the two middle widgtes you need to give either a Stretch factor to the Layout or define sizes for the widgets.
You may consider doing the following
QHBoxLayout *TopLayout = new QHBoxLayout;
QHBoxLayout *BottomLayout = new QHBoxLayout;
QHBoxLayout *MiddleLayout = new QHBoxLayout;
QVBoxLayout *mainLayout = new QVBoxLayout;
QPushButton *topBtn = new QPushButton;
QPushButton *bottomBtn = new QPushButton;
QPushButton *LeftMiddleBtn = new QPushButton;
QPushButton *RightMiddleBtn = new QPushButton;
TopLayout->addWidget(topBtn);
BottomLayout->addWidget(bottomBtn);
// order matters here (i.e. the first addWidget will be placed in the left)
MiddleLayout->addWidget(LeftMiddleBtn);
MiddleLayout->addWidget(RightMiddleBtn);
// order matters here
mainLayout->addLayout(TopLayout);
mainLayout->addLayout(MiddleLayout);
mainLayout->addLayout(BottomLayout);
setLayout(mainLayout);
More convenient and right approach is to use Qt Designer or Qt Creator for this matter.
QMainWindow is a spatial case. It already has a layout set and it is impossible to change layout in widget! Reason is that QMainWindow has lots of other functionalities like menu, docking, status bar.
So how to make it work?
You have to set central widget:
MainScreen::MainScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainScreen)
{
ui->setupUi(this);
auto widget = new QWidget();
setCentalWidget(widget);
auto vLayout = new QVBoxLayout(widget);
// add your stuff here
// some example for testing:
vLayout->addWidget(new QButton("Test button"));
vLayout->addWidget(new QLabel("Nice label"));
vLayout->addWidget(new QTextEdit);
}
Also you are doing something wrong since this line ui->setupUi(this); indicates that you are using Qt Designer (design ui by mouse) and this means that layout should not be setup directly by code!

Aligning QMenuBar items (add some on left and some on right side)

Currently I have QMenuBar with three QActions and it looks like this:
but I would like to get this (get some QActions right-aligned):
Is there a way to do this?
Probably the most simple solution to this particular problem is to use the corner widget. It can be used to place almost anything at the rightmost position, of course also a new menu bar:
QMenuBar *bar = new QMenuBar(ui->menuBar);
QMenu *menu = new QMenu("Test menu", bar);
bar->addMenu(menu);
QAction *action = new QAction("Test action", bar);
bar->addAction(action);
ui->menuBar->setCornerWidget(bar);
Result:
This is esp. helpful when the main menu is still to be edited in QDesigner...
Well one possible solution is here. But it involves implementing your own style (QStyle as I recall). However here is a snippet that I have just tried on mainwindow class:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
QMenuBar *barLeft = new QMenuBar;
QMenuBar *barRight = new QMenuBar;
barLeft->addAction("Foo Left 1");
barLeft->addAction("Foo Left 2");
barRight->addAction("Foo Left 1");
barRight->addAction("Foo Left 2");
QHBoxLayout *mainMenuLayout = new QHBoxLayout;
mainMenuLayout->addWidget(barLeft);
mainMenuLayout->addWidget(barRight);
mainMenuLayout->setAlignment(barLeft, Qt::AlignLeft);
mainMenuLayout->setAlignment(barRight, Qt::AlignRight);
QWidget *central = new QWidget;
central->setLayout(mainMenuLayout);
setCentralWidget(central);
}
This should be suitable.

QT4 using QMdiArea and QScrollArea strange usage trouble

Here is what I am doing: mainwindow with MdiArea, and I add a scrollarea widget (which contains a image label) to MdiArea as a subwindow. It doesn't work (the picture doesn't show).
Here is my code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QScrollArea sa;
QPixmap *image = new QPixmap("2.jpg");
QLabel* imageLabel = new QLabel();
imageLabel->setPixmap(*image);
sa.setWidget(imageLabel);
sa.show();
ui->mdiArea->addSubWindow(&sa);
}
But when I use a QLabel as subwindow directly, i.e. replace the last line with:
ui->mdiArea->addSubWindow(imageLabel);
it works perfectly.
Anyone know why this is happening?
QScrollArea sa;
This declares a QScrollArea on the stack. It gets destroyed immediately after the constructor finishes. Allocate it with new like you do for the other widgets and it should start working.
QScollArea *sa = new QScrollArea;
...
ui->mdiArea->addSubWindow(sa);
(And change the sa. to sa->.)

Resources