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

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.

Related

Can someone give me a simple qt example?

I want to implement a simple QT example: click the QPushButton to display a paragraph of text.
Like this:
I know there are many ways to implement it, but I don't know what's wrong with my code.
QPushButton *btn = new QPushButton;
//btn->show();
btn->setParent(this);
btn->setText("button 1");
QLabel *la = new QLabel(this);
connect(btn,&QPushButton::clicked,la,&QLabel::setText("show me"));
Anyone who can help me?
The idea is to create the QLabel with a predefined text and hide it. On clicking the button, show it. So, QLabel::hide and QLabel::show could be used here. Just take care of the coordinates where you show the label because it would overlap the button itself without layout or proper coordinates.
Example (mainwindow.cpp, constructor):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("My App"); // title bar text
QPushButton *btn = new QPushButton(this);
btn->setText("Click Me!");
QLabel *la = new QLabel(this);
la->setText("Show Me!"); // set label text in advance
la->move(100, 100); // set label position
la->hide(); // hide label on load
connect(btn, &QPushButton::clicked, la, &QLabel::show);
}
MainWindow::~MainWindow()
{
delete ui;
}
Output (after clicking the image):

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!

Qt - QStackedLayout setCurrentIndex displays blank window instead of widget

I just started learning Qt a couple days ago to make a game, and I'm trying to figure out how to make layouts work.
What I want is a Window using QStackedLayout with 2 widgets inside: StartScreen and GameScreen. StartScreen is shown on top on program run. A button in StartScren is connected to a function inside Window, and Window will call setCurrentIndex to change the widget on top to GameScreen.
What happens right now is that when I click on the button, the view changes to a blank window. I've tried hardcoding to setCurrentIndex(0), which does nothing, setCurrentIndex(1), which is what GameScreen should be and displays the same blank window, and setCurrentIndex(2), which is out of index bounds but still does nothing. So the connection is going through, but I don't understand why a blank window will show up instead of the button I have on GameScreen.
If someone can explain to me what concept I missed and how to fix it, I'd greatly appreciate it. Thanks!
Here is window.cpp:
Window::Window(QWidget *parent) : QWidget(parent)
{
resize(640, 480);
layout = new QStackedLayout;
createStartScreen();
createGameScreen();
setLayout(layout);
show();
};
void Window::createStartScreen(){
start = new StartScreen();
layout->addWidget(start);
start->setWindow(this);
}
void Window::playGame(){
layout->setCurrentIndex(layout->indexOf(game));
}
void Window::createGameScreen(){
game = new GameScreen();
layout->addWidget(game);
}
startscreen.cpp:
StartScreen::StartScreen(QWidget *parent) : QWidget(parent)
{
newGameButton = new QPushButton("New Game", this);
newGameButton->setGeometry(QRect(QPoint(260, 300), QSize(120,40)));
quitButton = new QPushButton("Quit", this);
quitButton->setGeometry(QRect(QPoint(260, 360), QSize(120,40)));
connect(quitButton, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));
};
void StartScreen::setWindow(Window *w){
connect(newGameButton, SIGNAL(clicked()), w, SLOT(playGame()));
}
gamescreen.cpp:
GameScreen::GameScreen(QWidget *parent) : QWidget(parent)
{
button = new QPushButton("Hi");
button->setGeometry(QRect(QPoint(260, 260), QSize(120,40)));
};
That's because you don't call show on button. But you should rather use a layout to handle it.
e.g.:
GameScreen::GameScreen(QWidget *parent)
: QWidget(parent)
{
button = new QPushButton(tr("Hi"));
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(button);
};
button will have a size adapted to its text and will be cleaned positioned in GameScreen.
On a side note, you should rather add a signal to StartScreen to request a new game and do the connection in Window. That way you won't have a tight coupling between StartScreen and Window.

Put QMenuBar at QMainWindow Bottom in 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.

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