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);
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...
To my custom widget, inherited from QWidget, I have added a QScrollArea like this:
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)//MainWindow is a QWidget
{
auto *scrollArea = new QScrollArea(this);
auto *widget = new QWidget(this);
widget->setStyleSheet("background-color:green");
scrollArea->setWidget(widget);
scrollArea->setWidgetResizable(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
QVBoxLayout *parentLayout = new QVBoxLayout(widget);
this->setStyleSheet("background-color:blue");
for(int i=0;i<12;i++){
QHBoxLayout* labelLineEdit = f1();
parentLayout->addStretch(1);
parentLayout->addLayout(labelLineEdit);
}
parentLayout->setContentsMargins(0,0,40,0);
}
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
echoLineEdit->setMaximumWidth(120);
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:white");
layout->addWidget(echoLineEdit);
layout->setSpacing(0);
return layout;
}
This produces a window which looks like this:
The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.
How could I fix this?
The problem is, that I want the scrollArea to occupy the entire
window, but it does not. It also doesn't get resized when I resize the window.
The reason is that you have not set any kind of layout to manage the positioning of your QScrollArea widget itself, so it is just being left to its own devices (and therefore it just chooses a default size-and-location for itself and stays at that size-and-location).
A simple fix would be to add these lines to the bottom of your MainWindow constructor:
QBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->addWidget(scrollArea);
I am trying to create an expandable Qt dialog application. The main layout is a QVBoxLayout. The top part has two views and a QPushButtonbutton. Clicking button will unfold the bottom widget which is initially hidden. In the bottom widget, there is another push button, which could fold (hide) the bottom widget. When the bottom widget fold/unfold, I expect the size of the dialog size to change as well.
But for some reason, the dialog size only increases when the bottom widget is unfolded. And never shrink back to (200, 100). Is there anything I missed?
Environment: Qt Creator 3.6.1; Based on Qt5.6.0 (MSVC2013 32bit); build on Mar 14 2016; revision d502727b2c
The code I am using :
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QTreeView *tree = new QTreeView;
QTableView *table = new QTableView;
QPushButton *button_show = new QPushButton;
button_show->setText(tr("Show hidden panel"));
QHBoxLayout *layout_top = new QHBoxLayout;
layout_top->addWidget(tree);
layout_top->addWidget(table);
layout_top->addWidget(button_show);
QHBoxLayout *layout_bottom = new QHBoxLayout;
QTextEdit *editor = new QTextEdit;
QPushButton *button_hide = new QPushButton;
button_hide->setText(tr("Hide the bottom panel"));
g_pEditor = editor;
layout_bottom->addWidget(editor);
layout_bottom->addWidget(button_hide);
QWidget *panel = new QWidget;
panel->setLayout(layout_bottom);
QVBoxLayout *layout_main = new QVBoxLayout;
layout_main->addLayout(layout_top);
layout_main->addWidget(panel);
setLayout(layout_main);
panel->hide();
connect(button_show, &QPushButton::clicked
, panel
, [=]()
{
panel->setVisible(true);
button_show->setEnabled(false);
resize(200, 200);// not really working, the dialog size is able to increase without calling resize()
});
connect(button_hide, &QPushButton::clicked, panel, [=]()
{
panel->hide();
button_show->setEnabled(true);
resize(200,100);// does not shrink the dialog size*
});
resize(200,100);
}
Thanks for your help :)
Your should try setFixedSize(w, h) instead. This sets both, the minimum and the maximum size to (w, h). "This will override the default size constraints set by QLayout."
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"
I have QWidget with button. When button is pressed, show new smaller window (Qwidget too). I want then new window is centered horizontal and veritcal on main window. Code which display new window is:
QWidget *wdg = new QWidget;
QPushButton *closeBtn = new QPushButton("Close");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(closeBtn);
wdg->setLayout(layout);
wdg->show();
wdg->resize(400,200);
Use the move slot. For example:
QPoint centerPoint = oldWidget->geometry()->center();
newWidget->adjustSize();
newWidget->move(centerPoint.x() - newWidget->width()/2, centerPoint.y() - newWidget->height()/2);
You may consider using frameGeometry() instead of geometry().
http://qt-project.org/doc/qt-5/application-windows.html#window-geometry
Hope that helps.