QLabel takes complete space - c++

I have a QWidget which contains a QVBoxLayout and that layout contains a QLabel and QToolButtons. My problem is, that the QLabel takes all the space. The only solution I found is to set maximumHeight to the QLabel, but if I do that, the Qt::AlignTop doesn't work anymore.
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window_main;
QWidget *widget_steps = new QWidget(&window_main);
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start);
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3);
window_main.showMaximized();
return a.exec();
}
Here a picture that shows how much space the QLable takes(the blue space):
So please help to minimize the space the QLable takes :)

Your problem is that the tool buttons have a fixed size, and therefore when resizing, the label is the only type that can grow: Therefore:
After adding the label, add stretch to the layout:
layout_steps->addWidget(label_steps_start);
layout_steps->addStretch();
Modified code - adds stretch at the bottom. Label size remains fixed, and buttons remain under it. I've removed the whole main window around the outside for the sake of testing.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget_steps = new QWidget;
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start);
//--- Removed.... layout_steps->addStretch();
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3);
layout_steps->addStretch(); //<----- Added!
widget_steps->show();
return a.exec();
}

One way you could do is to set the stretch factor for that particular widget inside the QVBoxLayout. You can find the documentation for that in here.
Basically, when you add a widget you can set that, for instance:
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window_main;
QWidget *widget_steps = new QWidget(&window_main);
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start, 3, Qt::AlignTop);
layout_steps->addStretch();
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1, 1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2, 1);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3, 1);
window_main.showMaximized();
return a.exec();
}

Related

Change vertical header title

How I can change title shown on the picture to "№". Thanks.
That widget is an object of the QTableCornerButton class that inherits from QAbstractButton but it is a class that is part of the private Qt API that does not use text, so you can not use setText() of QAbstractButton, so the other option is establish a QLabel with a layout above:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableView w;
QStandardItemModel model(10, 10);
w.setModel(&model);
QAbstractButton *button = w.findChild<QAbstractButton *>();
if(button){
QVBoxLayout *lay = new QVBoxLayout(button);
lay->setContentsMargins(0, 0, 0, 0);
QLabel *label = new QLabel("№");
label->setContentsMargins(0, 0, 0, 0);
lay->addWidget(label);
}
w.show();
return a.exec();
}

How to place QSizeGrip in the bottom right of a QDial?

On GraphicsView(QGraphicsView) i set scene(QGraphicsScene), i am adding qdial object throgh qgraphicsproxy widget, place sizegrip on bottom right position?
`
QDial *dial = new QDial(); // dial object
plot->setGeometry(event->pos().x(),event->pos().y(),80,80);
QSizeGrip * sizeGrip = new QSizeGrip(dial );// placing sizegrip
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(dial );
proxy->setFlag(QGraphicsItem::ItemIsMovable,true);
scene->addItem(proxy);`
image of output
You have to use a layout as I show below:
#include <QApplication>
#include <QDial>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QHBoxLayout>
#include <QSizeGrip>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView view;
QGraphicsScene *scene = new QGraphicsScene(&view);
view.setScene(scene);
QDial *dial = new QDial;
QSizeGrip * sizeGrip = new QSizeGrip(dial);
QHBoxLayout *layout = new QHBoxLayout(dial);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(sizeGrip, 0, Qt::AlignRight | Qt::AlignBottom);
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(dial);
proxy->setFlag(QGraphicsItem::ItemIsMovable,true);
scene->addItem(proxy);
view.show();
return a.exec();
}

QGraphicsScene many widgets

I have more than one widget in QGraphicsScene. The first created widget is on top of scene, other widgets is under first widget. I want such functionality - if i press one of widget it will be on top of the scene. How i can make it?
Also i don't know, how to make border to the widget in QGraphicsScene like the normal widget. This is the code:
QGraphicsScene *scene ;
QGraphicsProxyWidget *pw()
{
QWidget *w = new QWidget;
w->resize(580, 280);
w->setStyleSheet("background-color: rgb(110, 149, 255)");
QPushButton *p = new QPushButton;
p->setParent(w);
p->move(30, 50);
p->setText("hello");
QPushButton *p2 = new QPushButton;
p2->setParent(w);
p2->move(110, 50);
p2->setText("world");
QGraphicsWidget *parentWidget = new QGraphicsWidget();
parentWidget->setMinimumSize(QSizeF(100, 30));
parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
parentWidget->setAutoFillBackground(true);
parentWidget->resize(580, 280);
scene->addItem(parentWidget);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(w);
proxyWidget->setParentItem(parentWidget);
return proxyWidget;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 800, 480);
scene->addItem(pw());
scene->addItem(pw());
scene->addItem(pw());
scene->setBackgroundBrush(Qt::darkGreen);
QGraphicsView view(scene);
view.show();
return a.exec();
}

Widgets not shown in QT main window

I'm building a simple application, the main window have to shows two widgets (QTreeView on the right, QTabWidget on the left), to perform this i use a QHBoxLayout.
This is the code i've written(constructor of MainWindow):
MainWindow::MainWindow()
{
mainLayout = new QHBoxLayout(this);
tabber = new QTabWidget(this);
analysisTreeView = new QTreeView(this);
tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);
createActions();
createMenus();
createToolBars();
connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
setLayout(mainLayout);
}
But when i run the application the main window shows no widgets. Why?
On request, i add some code:
Once a button in the mainwindows's toolbar is clicked a new tab is added to tabber:
void MainWindow::newSheet()
{
GraphicsScene *newScene = new GraphicsScene(itemMenu,this);
QGraphicsView *newView = new QGraphicsView(this);
newScene->setSceneRect(-200, -200, 400, 400);
newView->scale(1.5,1.5);
newView->setCacheMode(QGraphicsView::CacheBackground);
newView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
newView->setRenderHint(QPainter::Antialiasing);
newView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
newView->setScene(newScene);
sheetList.append(newView);
tabber->addTab(newView,"PNC");
connect(newScene, SIGNAL(itemInserted(PItem*)), this, SLOT(itemInserted(PItem*)));
connect(newScene, SIGNAL(requestUpdateGUI(GraphicsScene*)), this, SLOT(updateGUI(GraphicsScene*)));
}
My main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(application);
QApplication a(argc, argv);
MainWindow window;
window.showMaximized();
return a.exec();
}
I suppose your class specializes QMainWindow. If so, it needs a centralWidget to be set:
MainWindow::MainWindow()
{
// added by jpo38
QWidget* mainWidget = new QWidget( this );
setCentralWidget( mainWidget );
// end added by jpo38
mainLayout = new QHBoxLayout(mainWidget);
tabber = new QTabWidget(mainWidget);
analysisTreeView = new QTreeView(mainWidget);
tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);
createActions();
createMenus();
createToolBars();
connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
setLayout(mainLayout);
}

How do I get a QLabel to expand to full width?

I want a QLabel to expand to full width of the container regardless of the contents. (I want this because I dynamically set the text and add widgets later which cause it to cut off part of the text)
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(100,100);
QHBoxLayout *layout = new QHBoxLayout;
this->setLayout(layout);
QLabel *label = new QLabel;
label->setStyleSheet("background-color:blue");
label->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
label->setText(tr("test"));
layout->addWidget(label, 0, Qt::AlignTop | Qt::AlignLeft);
}
This code shows that the blue box does not expand to the entire width, why?
You must set:
layout->setContentsMargins(0,0,0,0);
By default every QWidget or QFrame add 15 pixels of margin in every direction.
The main problem is with setting the alignment when you add the widget to the layout. Use label->setAlignment instead.
layout->addWidget(label);
I compiled your code, it works with those changes.
Here is the minimal example:
#include <QApplication>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget* w = new QWidget;
w->setFixedSize(100,100);
QHBoxLayout* layout = new QHBoxLayout;
layout->setContentsMargins(0,0,0,0);
w->setLayout(layout);
QLabel* label = new QLabel;
label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
label->setContentsMargins(0,0,0,0);
label->setStyleSheet("background-color:blue");
label->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
label->setText("test");
layout->addWidget(label);
w->show();
return a.exec();
}