How do you place a groupbox in a QMenuBar? - qt

I need the groupbox that contains the ID picture and a button on the right side of the QMenuBar, and I don't know how to do that. Here's what I have so far:
QGridLayout *menulayout = new QGridLayout();
QWidget *menuwidget = new QWidget (dynamic_cast<QMainWindow*>(this->parent())->menuBar());
menulayout->setContentsMargins(18,0,18,0);
menulayout->addWidget(menuwidget);
menulayout->addWidget(_ui->groupBox_UserMenu, 0 ,0);
dynamic_cast<QMainWindow*>(this->parent())->menuBar()->setLayout(menulayout);

Fixed this by placing the groupbox on the desired position on the Form and then set it's parent to the QMenu Bar. The first lines of code I posted were unnecessary.
_ui->groupBox_UserMenu->setParent(_ui->menuBar);

Related

How to control widgets position insid QStackedLayout

I've created a QStackedLayout in which I added 2 buttons, but I want to shift one button to the right corner insted of the left corner as you can see in the picture below:
What I've done so far:
QStackedLayout *stackedLayout = new QStackedLayout;
stackedLayout->addWidget(ui->pushButton_2);
stackedLayout->addWidget(ui->pushButton);
stackedLayout->setStackingMode(QStackedLayout::StackAll);
Place the QPushbutton in a container (i.e. QWidget) that allows it to be centered with an additional layout.
If I understand properly, you want a QPushButton above an other. The one above must be smaller and at the top right corner?
QVBoxLayout * vlayout_PB_under = new QVBoxLayout();
QHBoxLayout * hlayout_PB_under = new QHBoxLayout();
QPushButton * PB_x = new QPushButton("X",this);
QPushButton * PB_test = new QPushButton("test",this);
PB_x ->setFixedSize(20,20);
PB_test ->setFixedSize(200,200);
PB_test ->setLayout(vlayout_PB_under );
vlayout_PB_under ->addLayout(hlayout_PB_under);
hlayout_PB_under->addWidget(PB_x );
vlayout_PB_under ->setAlignment(Qt::AlignRight);
hlayout_PB_under->setAlignment(Qt::AlignTop );
The purpose of a QStackLayout is to allow switching easily the visible widget at a same position, not to display at the same time multiple widget...
Read the documentation : "where only one widget is visible at a time"

How to set a center aligned QLabel to toolbar in Qt

I'm new to qt and exploring it .I want to have a text which is center aligned in my Mainwindow's toolbar.Below is my code inside my MainWindow constructor:
QLabel* label=new QLabel("Hello World");
label->setAlignment(Qt::AlignHCenter);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(label);
QWidget* wid = new QWidget;
wid->setLayout(layout);
ui->mainToolBar->addWidget(wid);
The above code displays the text , but not at the center.It displays at the left.What am I missing?Any help will be really helpful.
label->setAlignment(Qt::AlignHCenter);
This tells the label to (horizontally) center the text in itself.
layout->addWidget(label);
This is expanded by default argument to
layout->addWidget(label, 0);
Where the 0 is the stretch factor of the label within this layout. Zero means your label will be given as much space as it needs to display properly but nothing more. So your label is just as big as your text needs, has it's text centered, but since it's on a QHBoxLayout it's shown on the left side within your bar. If there are no other widgets in your bar's layout, you can set the stretch-factor to 1 to make the label fill the layout, your text will then show in the center.
layout->addWidget(label, 1);

Allignment of a QLabel and a QCheckBox in a BoxLayout gives unexpected result

when i add a QLabel and QCheckBoxs to either a QVBoxLayout or a QHBoxLayout i would expect them to be evenly distributed but the checkboxes will allign tight at the very bottom (in the above example) and the label will be centered on the resulting free space in the widget. How can i change this behaviour to distribute all 3 widgets evenly?
Many thanks.
This is the example code:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
QLabel* l = new QLabel("Hi");
QCheckBox* c = new QCheckBox("Label");
QCheckBox* c2 = new QCheckBox("Label");
l->setText("Hi");
QVBoxLayout* v = new QVBoxLayout;
v->addWidget(l);
v->addWidget(c);
v->addWidget(c2);
setLayout(v);
ui->setupUi(this);
}
And this is the result:
Take a look at QSizePolicy. You need to setSizePolicy for your QLabel and QCheckBoxes to be QSizePolicy::Preferred, from the docs:
The default policy is Preferred/Preferred, which means that the widget
can be freely resized, but prefers to be the size sizeHint() returns.
Button-like widgets set the size policy to specify that they may
stretch horizontally, but are fixed vertically. The same applies to
lineedit controls (such as QLineEdit, QSpinBox or an editable
QComboBox) and other horizontally orientated widgets (such as
QProgressBar).
Currently, your QLabel has preferred height, while both of the QCheckBoxes has fixed height. That means that the QLabel will be expanded automatically to take up any additional vertical space (that can't be taken by the QCheckBoxes.
So, In order to set all your widgets to Preferred height, you need to add the following to your constructor:
l->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
c->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
c2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
Another option, would be to add spacers around each one of the widgets, like this:
v->addStretch();
v->addWidget(l);
v->addStretch();
v->addWidget(c);
v->addStretch();
v->addWidget(c2);
v->addStretch();
setLayout(v);
This way QSpacerItems will take up all the additional space.

Reversing the layout of a QToolButton

I would like to make a QToolButton where the text is the left of its icon. I've tried to find related information, and tried things with:
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
but this sets the text to the right side of the button. I've looked into trying stylesheets, but text-alignment is only for push buttons. Is there any way to do this?
My highest-level goal is to make an interface bar which looks like this:
with a text label and image right beside it. Currently I'm using a toolbar with checkable toolbuttons because of the style (no border unless moused-over, still has the indent with checkable, contains text and an icon...). It's entirely possible that I'm using the wrong type widgets, so if I can't change the layout of this, is there any way to emulate this style?
This is what I currently have:
.
You can use QWidget::setLayoutDirection:
toolbar->setLayoutDirection(Qt::RightToLeft);
Your best bet is probably to create this functionality yourself in a QWidget. It should have a QLabel and a QToolButton (or QPushButton) arranged in a QHBoxLayout. Then you can add this custom widget to the toolbar using QToolBar::addWidget() or QToolBar::insertWidget().
Here's an example of a widget you could use:
#include <QtGui>
class CoolButton : public QWidget
{
public:
CoolButton(QWidget *parent = 0) : QWidget(parent)
{
QLabel *label = new QLabel("Hi!", this);
QToolButton *button = new QToolButton(this);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(button);
setLayout(layout);
}
};

How to add a scrollbar to parent QWidget

I understand how to add a scrollArea to a particular widget. However in my case Qwidget has multiple child widgets and these are all set using QVBoxLayout. Now how can I add a scroll bar in this case? Here QWidget is not the center widget, its one of the pages of the TabWidget. My code looks like:
QTabWIdget *center = new QTabWidget; setCentralWIdget(center);
xTab = new QWidget;
formLayout = new QFormLayout; formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...);
xTab->setLayout(formLayout);
Now how can I set the scrollBar to xTab? I tried using
scrollArea = new QScrollArea;
scrollArea->setWidget(xTab);
however, this isn't working.
Any idea/suggestions are helpful and appreciated.
Have you tried using QScrollArea as the tab page?
QTabWIdget *center = new QTabWidget; setCentralWIdget(center);
xTab = new QScrollArea;
formLayout = new QFormLayout; formLay....
xTab->setLayout(formLayout);
center->addTab(xTab, "XXX Tab");
I had success using the following:
scroll=new QScrollArea(mainWindow->centralWidget);
scroll->setGeometry(mainWindow->tabWidget->geometry());
scroll->setWidget(mainWindow->tabWidget);
scroll->show();
The QScrollArea defines where the scrollable widget will appear. If parent is 0, it's a non-modal window. setGeometry sets the QScrollArea instance to the desired dimensions (that of the tab). setWidget defines what widget the QScrollArea will actually be scrolling.

Resources