In this code, how can I center the button inside the gridlayout?
QPushButton* button_example = new QPushButton(this);
button_example->setText("Click");
QGridLayout *gl_example = new QGridLayout(this);
gl_example->addWidget(button_example);
instead of this use centralWidget and also add Qt::AlignHCenter | Qt::AlignVCenter) :
QPushButton *button_example = new QPushButton(centralWidget());
button_example->setText("Click");
QGridLayout *gl_example = new QGridLayout(centralWidget());
gl_example->addWidget(button_example, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignVCenter);
output:
Related
On a QGraphicsView I set a QGraphicsScene. I add a QDial object through a QGraphicsProxy widget. How to move the QDial object?
QDial *dial = new QDial;// dial object
dial->setGeometry(event->pos().x(),event->pos().y(),80,80);// placing on mouse position
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);
In this code QGraphicsWidget is GraphicItem by making parent of widget you can move widget on scene.setflags movable.
QDial *dial = new QDial;// dial object
dial->setGeometry(event->pos().x(),event->pos().y(),80,80);// placing on mouse position
QSizeGrip * sizeGrip = new QSizeGrip(dial);
QHBoxLayout *layout = new QHBoxLayout(dial);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(sizeGrip, 0, Qt::AlignRight | Qt::AlignBottom);
QGraphicsWidget* parentWidget = new QGraphicsWidget();//make parent of widget
parentWidget->setCursor(Qt::SizeAllCursor);
parentWidget->setGeometry(event->scenePos().x(),event->scenePos().y(),width.toInt(), height.toInt());
parentWidget->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable );
addItem(parentWidget);
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
proxy->setWidget(dial);
proxy->setParentItem(parentWidget);
Putting the QDial into a QGraphicsProxyWidget is only the first step.
Since the proxy does not support moving, you can put it into a QGraphicsItem (e.g. a rect) and use this to move the proxy with the QDial in it:
QDial *dial = new QDial();
QGraphicsRectItem* movableGraphicsItem = scene->addRect(event->pos().x(), event->pos().y(), 80, 80);
movableGraphicsItem->setFlag(QGraphicsItem::ItemIsMovable, true);
movableGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, true);
QGraphicsProxyWidget* proxy = scene->addWidget(dial);
proxy->setPos(event->pos().x(), event->pos().y() + movableGraphicsItem->rect().height());
proxy->setParentItem(movableGraphicsItem);
movableGraphicsItem->setRotation(180); // Test by rotating the graphics item
I have not tested this, you may have to play around with the sizing, the position and the layout and size grip you are using, but this is the base from where you can start.
I am working on a Qt Project and for this project I require to design something like this:
I have designed so far in Qt Creator and I have the component ready, but when I am trying to add widget in different layouts, I am not getting the shapes I want. What should I do to make my application resizable?
Catches:
Sidebar has fixed width, which means for horizontal increment of window size the sidebar's horizontal width won't increase. Sidebar itself is a widget.
upperbar's vertical width is fixed (if possible). Which means, during vertical window size increment the upperbar can't become vertically wider. And it itself is also a widget.
the widgets by the side of sidebar are in a qstackedwidget.
Nested layouts:
(green square = QStackedWidget)
Steps:
[Definition]
H(x, y, ...) = horizontal layouts on x, y, ...; where x, y, ... are widget(W#) or Layout(L#)
V(x, y, ...) = horizontal layouts on x, y, ...; where x, y, ... are widget(W#) or Layout(L#)
Step 1: V(W1, W2) = L1
Step 2: H(W3, L1) = L2
Step 3: V(W4, L2) = L3
Step 4: Set L3 as the layout of current page of the StackedWidget layout
Step 5: H(W5, StackedWidget) = L4
Step 6: H(W6, a spacer, W7) = L5
Step 7: V(L5, L4)
Notice that W6 and W7 are fixed in horizontal size (or set maximum on it), the spacer between them acts as the only resizable widget in the layout L5.
And here is the hierarchy:
Just for fun, the code version, with minimal optimized code...
#include "mainwindow.h"
#include <QBoxLayout>
#include <QLabel>
#include <QStackedWidget>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Ingredients (taken from the mockup from top-left to bottom-right)
QFrame *upperBar = new QFrame;
QLabel *upperIcon = new QLabel("icon");
QLabel *profilePicture = new QLabel("profile picture");
QFrame *sideBar = new QFrame;
QLabel *sideItemA = new QLabel("Item A");
QLabel *sideItemB = new QLabel("Item B");
QStackedWidget *contentStack = new QStackedWidget;
QFrame *contentPage1 = new QFrame;
QLabel *page1WidgetA = new QLabel("I am widget A");
QLabel *page1WidgetB = new QLabel("I am widget B");
QLabel *page1WidgetC = new QLabel("I am widget C");
QLabel *page1WidgetD = new QLabel("I am widget D");
QWidget *centralWidget = new QWidget;
// The needed layouts:
QHBoxLayout *upperBarLayout = new QHBoxLayout;
QVBoxLayout *sideBarLayout = new QVBoxLayout;
QGridLayout *page1GridLayout = new QGridLayout;
QGridLayout *centralLayout = new QGridLayout;
// Let's connect the pieces:
/* First we setup the upperbar: */
upperBarLayout->addWidget(upperIcon, 1, Qt::AlignLeft);
upperBarLayout->addWidget(profilePicture, 3, Qt::AlignRight);
upperBar->setLayout(upperBarLayout);
upperBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
/* Then we setup the sidebar: */
sideBarLayout->addWidget(sideItemA);
sideBarLayout->addWidget(sideItemB);
sideBarLayout->addStretch();
sideBar->setLayout(sideBarLayout);
sideBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
/* Then we setup the content stacked widget */
page1GridLayout->addWidget(page1WidgetA, 0, 0, 3, 1);
page1GridLayout->addWidget(page1WidgetB, 0, 1, 1, 1);
page1GridLayout->addWidget(page1WidgetC, 1, 1, 2, 1);
page1GridLayout->addWidget(page1WidgetD, 3, 0, 1, 2);
contentPage1->setLayout(page1GridLayout);
contentStack->addWidget(contentPage1);
contentStack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
/* Finally we setup the main elements into the central layout... */
centralLayout->addWidget(upperBar, 0, 0, 1, 2);
centralLayout->addWidget(sideBar, 1, 0, 1, 1);
centralLayout->addWidget(contentStack, 1, 1, 1, 1);
centralWidget->setLayout(centralLayout);
setCentralWidget(centralWidget);
/* Let's color it a little to better realize the positioning: */
setStyleSheet("QWidget {"
"border: 1px solid black;"
"color: red"
"}");
}
MainWindow::~MainWindow()
{
}
Here is the result:
I am creating a window dimanica to the downloads list.
But the scrollbar does not work and the "widgets children" are "cut".
Where can I be wrong? Thanks.
Source:
QWidget *central = new QWidget;
QScrollArea *scroll = new QScrollArea;
QVBoxLayout *layout = new QVBoxLayout(scroll);
scroll->setWidget(central);
scroll->setWidgetResizable(true);
int i=0;
while(i<10){
QWidget *p1 = new QWidget;
QHBoxLayout *hl = new QHBoxLayout(p1);
QLabel *label1 = new QLabel("test");
QLabel *label2 = new QLabel("0%");
hl->addWidget(label1);
hl->addWidget(label2);
layout->addWidget(p1);
i++;
}
QMainWindow *w = new QMainWindow;
w->setGeometry(50,50,480,320);
w->setCentralWidget(scroll);
w->show();
Found your mistake, you should set layout to widget central not to scroll:
QWidget *central = new QWidget;
QScrollArea *scroll = new QScrollArea;
QVBoxLayout *layout = new QVBoxLayout(central);
scroll->setWidget(central);
scroll->setWidgetResizable(true);
EDITED:
Your labels already take all available space, if you noticed, label1 begins at left border ends in the middle, where label2 starts and ends at the right border. If I understood you correctly, you want label1 to take all the space available, while label2 with percents to take only what space is needed, no more?
Read about QSizePolicy class and use setSizePolicy() on your labels. Try to insert this line right after label2 declaration:
QLabel *label2 = new QLabel("0%");
label2->setSizePolicy(QSizePolicy::QSizePolicy::Maximum,QSizePolicy::Maximum);
And add line layout->addStretch(); right before QMainWindow *w = new QMainWindow;
I want to reduce space between label and QLineEdit (QLabel is above QLineEdit). How can I achieve it? In the code I'm creating items, that I later put in some layouts.
QLabel* lgamma = new QLabel("Gamma");
gamma = new QLineEdit();
QLabel* lmin_linie = new QLabel(QString::fromUtf8("Min. il. zmian linii"));
min_lin = new QLineEdit();
// ...
QLabel* lmax_kursy = new QLabel(QString::fromUtf8("Max zm. il. kursów"));
max_kursy = new QLineEdit();
QGridLayout *lay = new QGridLayout(this);
QVBoxLayout *box1 = new QVBoxLayout();
QVBoxLayout *box2 = new QVBoxLayout();
// ...
QVBoxLayout *box12 = new QVBoxLayout();
box1->addWidget(lmin_linie);
box1->addWidget(min_lin);
box2->addWidget(lmax_lin);
box2->addWidget(max_lin);
// ...
box12->addWidget(literacje);
box12->addWidget(iteracje);
verticalColumn1->addLayout(box1);
verticalColumn1->addLayout(box2);
// ...
verticalColumn3->addLayout(box12);
start = new QPushButton("Start", this);
QHBoxLayout *corn = new QHBoxLayout();
corn->addLayout(verticalColumn1);
corn->addLayout(verticalColumn2);
corn->addLayout(verticalColumn3);
QVBoxLayout *rup = new QVBoxLayout();
rup->addLayout(corn);
rup->addWidget(start);
You can simply add a spacer to your layout.
QSpacerItem *spacer = new QSpacerItem(1, 50, QSizePolicy::Ignored, QSizePolicy::Expanding);
box1.addItem(spacer);
Adapt the args or QSpacerItem for your needs, for examle QSizePolicy::Preferred could be better than QSizePolicy::Expanding, and reduce the preferred height (second argument).
Why don't you use the easy way to do that GUI with Qt designer ?
You can try to set QLabel and QLineEdit border:
lmax_kursy->setStyleSheet("border-width:0px");
max_kursy->setStyleSheet("border-width:0px");
or set spacing in layout. First check what is a current value of spacing:
box1->spacing();
If it's 0, try to set negative value like -2:
box1->setSpacing(-2); // or 0 or something else
I have one form where i am applying ScrollArea but it is not getting applied properly ?
I want to place the four buttons in Scrollable Area and when buttons gets increased a scroll bar should appear there to scorll down vertically.
Currently when i am trying to add more buttons the buttons overlap with the combobox and does not grow downward.
Here is my code :
QPushButton *b1 = new QPushButton(strsearch);
QPushButton *b2 = new QPushButton(strsearch);
QPushButton *b3 = new QPushButton(strsearch);
QPushButton *b4 = new QPushButton(strsearch);
b1->setStyleSheet(
"background-image: url(:/user.gif);"
"background-repeat:no-repeat;"
"background-attachment:fixed;"
"background-position:left top;"
"border-style: outset;"
"background-color : black;"
"border-width: 2px;"
"border-radius: 10px;"
"border-color: black;"
"font: bold 16px;"
"color: black;"
"min-width: 10em;"
"min-height: 0.75em;"
"margin: 0 1px 0 1px;"
"color:rgb(255,255,255);"
"padding: 6px;"
);
b2->setIcon(QIcon(":/user.gif"));
b2->setIconSize(QSize(160, 26));
b3->setIcon(QIcon(":/user.gif"));
b3->setIconSize(QSize(160, 26));
b4->setIcon(QIcon(":/user.gif"));
b4->setIconSize(QSize(160, 26));
QGridLayout *layout = new QGridLayout;
layout->addWidget(b1, 1, 0);
layout->addWidget(b2, 2, 0);
layout->addWidget(b3, 3, 0);
layout->addWidget(b4, 4, 0);
layout->addWidget(scrollArea);
layout->setAlignment(Qt::AlignBottom);
setLayout(layout);
//Create the buttons
QPushButton *b1 = new QPushButton("Button 1");
QPushButton *b2 = new QPushButton("Button 2");
QPushButton *b3 = new QPushButton("Button 3");
QPushButton *b4 = new QPushButton("Button 4");
//Add the buttons to a vertical layout (faster than grid layout)
QVBoxLayout *scrollLayout = new QVBoxLayout;
scrollLayout->addWidget(b1);
scrollLayout->addWidget(b2);
scrollLayout->addWidget(b3);
scrollLayout->addWidget(b4);
//Create a viewport widget that contains the layout with buttons
QWidget *viewport = new QWidget;
viewport->setLayout(scrollLayout);
//Add the viewport to the scroll area
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(viewport);
//Add the scroll area to your main window's layout
mainLayout->addWidget(scrollArea);