I'm trying to set a QGridLayout with four widget as in the image below:
however what I've managed with QGridLayout as of now is:
I don't see how I can set the size of the row different for column 0 and 1. Maybe QGridLayout is not the right way of doing it but I don't know of any other widget that would do the trick.
Does anyone have any idea how to achieve this?
I would use vertical and horizontal layouts instead of the grid layout. So you need two vertical layouts and horizontal one:
// Left side
QLabel *lbl1 = new QLabel(this);
QTableWidget *t = new QTableWidget(this);
QVBoxLayout *vl1 = new QVBoxLayout;
vl1->addWidget(lbl1);
vl1->addWidget(t);
// Right side
// QImage is not a widget, so it should be a label with image
QLabel *lbl2 = new QLabel(this);
QCustomPlot *pl = new QCustomPlot(this);
QVBoxLayout *vl2 = new QVBoxLayout;
vl2->addWidget(lbl2);
vl2->addWidget(pl);
// Create and set the main layout
QHBoxLayout mainLayout = new QHBoxLayout(this);
mainLayout->addLayout(vl1);
mainLayout->addLayout(vl2);
I don't think grids are the way to go here indeed...
You could try making a horizontal layout of 2 QFrames, in which you set a vertical layout each with the two widgets of that "column"
Related
To be more clear explaining my problem, I've done a screenshot with some notes on it, hope it helps:
QGroupBox Layout format problem
As you can see from it, I have one big QVBoxLayout for the main layout of the app, inside it I've put a Qwidget, then a QGridLayout and then a QGridLayout again.
Inside this last one QGridLayout, I've put two QGroupBoxes, one in position 0,0 and one in position 0,1. Each QGroupBox has its own inner Layout, both of QGridLayout type again.
The screenshot shows that the firs QGroupBox works good, while the second one, that's quite smaller than the first, has two problems:
1) The label shoul be "Specific Operations" but it is trunked, and the only way to show it completely seems to be to put the buttons one next to the other horizontally... but I don't want it!
2) I managed to align the QGroupbox on the left of its "grid" but I need it to be on the upper-left corner of it, while it is centered for the moment... How can I achieve this?
Here is part of the code that should help you understand. Here is the kalk.h file:
class Kalk : public QWidget
{
Q_OBJECT
public:
Kalk(QWidget *parent = 0);
private slots:
void kalkChange(QString);
//....
private:
QComboBox *chooser;
QVBoxLayout *mainLayout;
QGridLayout *subLayout;
QGridLayout *operationsLayout;
QGroupBox *baseOperators;
QGridLayout *baseOperatorsLayout;
QGroupBox *specificOperators;
QGridLayout *specificOperatorsLayout;
};
Then the corresponding kalk.cpp file:
Kalk::Kalk(QWidget *parent) : QWidget(parent){
chooser = new QComboBox();
//...
connect(chooser,SIGNAL(currentIndexChanged(QString)),this,SLOT(kalkChange(QString)));
mainLayout = new QVBoxLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
subLayout = new QGridLayout;
subLayout->setEnabled(false);
subLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addWidget(chooser);
mainLayout->addLayout(subLayout);
//operationsLayout = new QHBoxLayout;
operationsLayout = new QGridLayout;
operationsLayout->setSizeConstraint(QLayout::SetFixedSize);
baseOperators = new QGroupBox(tr("Base Operations"));
baseOperatorsLayout = new QGridLayout(baseOperators);
baseOperatorsLayout->setSizeConstraint(QLayout::SetFixedSize);
specificOperators = new QGroupBox(tr("Specific Operations"));
specificOperatorsLayout = new QGridLayout(specificOperators);
specificOperatorsLayout->setSizeConstraint(QLayout::SetFixedSize);
operationsLayout->addWidget(baseOperators,0,0);
operationsLayout->setAlignment(baseOperators,Qt::AlignLeft);
operationsLayout->addWidget(specificOperators,0,1);
operationsLayout->setAlignment(specificOperators,Qt::AlignLeft);
mainLayout->addLayout(operationsLayout);
setLayout(mainLayout);
//...
}
In another function I load the buttons inside the Layout of the QGroupBox, but I don't think the problem is here...
ui->setupUi(this);
QWidget *win = new QWidget();
QVBoxLayout* lay = new QVBoxLayout(win);
lay->setAlignment(Qt::AlignTop | Qt::AlignLeft);
QPushButton *botton = new QPushButton(cmd);
lay->addWidget(botton);
ui->scrollAreaWidgetContents->setLayout(lay);
the data in the scroll area is center aligned and want to change this to left alignment
from past few days i am trying to make the data to be left aligned but its not changing
can anyone help me in this regard
Add spacer item to the right side into horizontal layout.
QHBoxLayout * poHLayout = new QHBoxLayout;
QPushButton * button = new QPushButton(cmd);
// Spacer item
QWidget * poSpacerItem = new QWidget(this);
poSpacerItem->setSizePolicy(
QSizePolicy::Minimum,QSizePolicy::Maximum);
// Add your button to the left side. (will align button to left)
poHLayout->addWidget(button);
// Add spacer item
poHLayout->addWidget(poSpacerItem);
ui->scrollAreaWidgetContents->setLayout(poHLayout);
I have created a simple QHBoxLayout (horizontal) that is pushed to the bottom of the QVBoxLayout (Vertical) and it contains two buttons. See code:
QWidget* create_ver_and_horizontal_box() {
QWidget* temp = new QWidget();
// Add buttons to the horizontal box
QHBoxLayout* hbox = new QHBoxLayout();
QPushButton *ok = new QPushButton("OK");
QPushButton *cancel = new QPushButton("Cancel");
hbox->addWidget(ok);
hbox->addWidget(cancel);
// Create a vertical box and add the horizontal box to
// the end of it
QVBoxLayout* vbox = new QVBoxLayout();
vbox->addStretch(1);
vbox->addLayout(hbox);
// set the layout and return
temp->setLayout(vbox);
return temp;
}
and the resulting UI is the following.
But when I add the QWidget temp to be the parent of the QHBoxLayout, like so:
// Add buttons to the horizontal box
QHBoxLayout* hbox = new QHBoxLayout(temp);
This is what I get:
I want to understand what is going on here. And in which cases I want the QWidget to be the parent of a layout or any other QWidget(s) and in which cases I don't the containing QWidget to be the parent of the containing QWidgets. For example, I could've added temp to be the parent of the two Push buttons but I didn't. What is the implication of not adding vs adding.
Thanks,
QHBoxLayout* hbox = new QHBoxLayout(temp);
is equivalent to
QHBoxLayout* hbox = new QHBoxLayout();
temp->setLayout(hbox);
I.e. you are making the horizontal layout responsible for temp.
The call to setLayout(vbox) should have generated a runtime warning message, that temp already has a layout, hinting at that.
Since you want the vertical layout to be responsible for that widget, either keep the temp->setLayout(vbox) or pass temp to the constructor of QVBoxLayout.
i have a layout problem in my Qt Widget
What i have:
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(widget1);
layout->addWidget(widget2);
layout->addWidget(widget3);
this->setLayout(layout);
http://i.stack.imgur.com/p7SvE.png
What i want:
http://i.stack.imgur.com/ANRel.png
Sorry for posting the images so sucky, but i need 10 reputation for image postings
I need the Widget 1 behind the other 2 widgets, and full sized.
It would be perfect when widget 2 and 3 could get some opacity by the stylesheet.
Thanks for every help!
You should create 2 Layout,
First VLayout for MainWindow and second VLayout for Widget 1
For example :
//If your widget1 size is null set it with setGeometry();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(widget1);
QVBoxLayout *layoutWdg1 = new QVBoxLayout(widget1);
layoutWdg1->addWidget(widget2);
layoutWdg1->addWidget(widget3);
this->setLayout(layout);
I currently have something like this
QLabel* l = new QLabel(this);
l->setTextFormat(Qt::RichText);
l->set_IsSelf(IsSelf);
l->setWordWrap(true);
l->setText("Thissssssssssssssssssssssssssssssss");
l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
l->setMaximumWidth(40);
Now I realize that width is very small and thats ok. What I want is to display all the content and make it expand vertically.
You should insert your label in a layout which it's sizeconstraint is set to QLayout::SetMinimumSize and set the vertical sizepolicy of your label to QSizePolicy::MinimumExpanding :
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetMinimumSize);
QLabel* l = new QLabel;
l->setWordWrap(true);
l->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
layout->addWidget(l);