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);
Related
I want to make a QLable and a QComboBox display in one line (horizontal, let's name it as h_combination_1), and there are some similar layouts like this (e.g. h_combination_2, h_combination_3, and etc). Then I want to manage this combinations (h_combination_1..3) display vertially. But at last these combinations display horizontally, how strange it is! Could someone help me with this?
Here is the code:
void ThemeWidget::initLayout()
{
[..]
QHBoxLayout* themeLayout = new QHBoxLayout;
QLabel* themeLabel = new QLabel("Theme");
themeLayout->addWidget(themeLabel);
themeLayout->addWidget(cbox_theme);
QHBoxLayout* legendLayout = new QHBoxLayout;
QLabel* legendLabel = new QLabel("Legend");
themeLayout->addWidget(legendLabel);
themeLayout->addWidget(cbox_legend);
QHBoxLayout* animationLayout = new QHBoxLayout;
QLabel* animationLabel = new QLabel("Animation");
themeLayout->addWidget(animationLabel);
themeLayout->addWidget(cbox_animation);
antialiasCheckBox = new QCheckBox("Antialiasing");
[..]
QVBoxLayout* parametersLayout = new QVBoxLayout;
parametersLayout->addLayout(themeLayout);
parametersLayout->addLayout(legendLayout);
parametersLayout->addLayout(animationLayout);
parametersLayout->addWidget(antialiasCheckBox);
QWidget* paramsBox = new QWidget();
paramsBox->setLayout(parametersLayout);
gridLayout = new QGridLayout;
QHBoxLayout* layout = new QHBoxLayout;
layout->addLayout(gridLayout);
layout->addWidget(paramsBox);
this->setLayout(layout);
}
Here is a screenshot:
In the begining, I've just add the layout parametersLayout to layout, without binding it to a QWidget.
For example:
void ThemeWidget::initLayout()
{
[..]
QVBoxLayout* parametersLayout = new QVBoxLayout;
parametersLayout->addLayout(themeLayout);
parametersLayout->addLayout(legendLayout);
parametersLayout->addLayout(animationLayout);
parametersLayout->addWidget(antialiasCheckBox);
gridLayout = new QGridLayout;
QHBoxLayout* layout = new QHBoxLayout;
layout->addLayout(gridLayout);
layout->addWidget(parametersLayout);
this->setLayout(layout);
}
But it turned out just as same as binding parametersLayout to a QWidget.
The layoutItems in parametersLayout should be vertical, but there were not!
So I changed into the method I discribed in the details above, and the results was still the same.
This is my code :
void maquette::on_btn_edit_clicked()
{
QWidget* wdg = new QWidget;
wdg->resize(320, 340);
wdg->setWindowTitle("Modiffier");
QLabel label1("matricule", wdg);
label1.setGeometry(100, 100, 100, 100);
wdg->show();
}
the window shows up but the label didn't show
void maquette::on_btn_edit_clicked()
{
QWidget *wdg = new QWidget;
wdg->resize(320,340);
wdg->setWindowTitle("Modiffier");
QLabel *label1 = new QLabel("matricule",wdg);
label1->setGeometry(100, 100, 100, 100);
wdg->show();
}
You can either add the QLabel using parenting. as mentioned before.
QLabel *label1 = new QLabel("matricule",wdg);
or
QLabel *label1 = new QLabel("matricule");
label1->setParent(wdg);
This will make the widget float inside its parent.
You can also add the QLabel to a layout that has been assigned to the QWidget.
QVBoxLayout* layout = new QVBoxLayout();
wdg->setLayout(layout);
QLabel *label1 = new QLabel("matricule");
layout->addWidget(label1);
This will add the widget to the layout.
The layout will control how the child widgets are laid out.
What I currently have:
What I want:
For those unable to view the images; the widgets are spread out by some sort of margin between them. I would like to keep them as close as possible. How can I squeeze the widgets closer together?
I have already tried:
setFixedSize(sizeHint()); and setSizeConstraint(QLayout::SetFixedSize); on the main window, layouts, and widget object. Nothing seems to work.
As an extra, I would also appreciate this:
(having the label get even closer to the lineEdit)
I am using Windows and Qt 5.11.1, 64-bits.
The window constructor code:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
widget = new QWidget();
label = new QLabel(tr("Enter your name:"));
nameLine = new QLineEdit;
nameLine->setMinimumWidth(250);
label->setBuddy(nameLine);
okButton = new QPushButton (tr("Ok"));
clearButton = new QPushButton (tr("Clear"));
connect(okButton, SIGNAL(clicked()), this, SLOT(message()));
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
QGridLayout *grid = new QGridLayout;
grid->addWidget(label,0,0);
grid->addWidget(nameLine,1,0);
grid->addWidget(okButton,0,1);
grid->addWidget(clearButton,1,1);
widget->setLayout(grid);
setWindowTitle(tr("Leo v0.0"));
setCentralWidget(widget);
}
A possible solution is to establish a QVBoxLayout with addStretch():
QVBoxLayout *vlay = new QVBoxLayout;
QGridLayout *grid = new QGridLayout;
grid->addWidget(label, 0, 0);
grid->addWidget(nameLine, 1, 0);
grid->addWidget(okButton, 0, 1);
grid->addWidget(clearButton, 1, 1);
vlay->addLayout(grid);
vlay->addStretch();
widget->setLayout(vlay);
setCentralWidget(widget);
Can I add some widgets like QLabel and QPushButton into a QTabWidget?
Actually, I want to do something like this:
I'm using C++ and Qt.
Thanks
It's possible, just use QTabWidget::setCornerWidget.
Quick example:
QWidget* pTabCornerWidget = new QWidget(this);
QLabel* pLabelTime = new QLabel(pTabCornerWidget);
pLabelTime->setText("10:22:20");
QPushButton* pButton = new QPushButton(pTabCornerWidget);
pButton->setText("?");
pButton->setMaximumSize(QSize(25, 25));
QHBoxLayout* pHLayout = new QHBoxLayout(pTabCornerWidget);
pHLayout->addWidget(pLabelTime);
pHLayout->addWidget(pButton);
mUI.tabWidget->setCornerWidget(pTabCornerWidget, Qt::TopRightCorner);
If you want do this stuff, I recommend you use QTabBar instead of QTabWidget. For example, your code can be (remember, that it's just a very simple example):
// Here some first widget
QWidget *wid1 = new QWidget(this);
QHBoxLayout *wid1Lay = new QHBoxLayout(wid1);
wid1Lay->addWidget(new QLabel(tr("Widget1")));
// Here some second widget
QWidget *wid2 = new QWidget(this);
QHBoxLayout *wid2Lay = new QHBoxLayout(wid2);
wid2Lay->addWidget(new QLabel(tr("Widget2")));
// Here some third widget
QWidget *wid3 = new QWidget(this);
QHBoxLayout *wid3Lay = new QHBoxLayout(wid3);
wid3Lay->addWidget(new QLabel(tr("Widget3")));
// Here your Tab bar with only bars
QTabBar *bar = new QTabBar(this);
bar->addTab("One");
bar->addTab("Two");
bar->addTab("Three");
// Here some label (for example, current time) and button
QLabel *lab = new QLabel(tr("Some text"), this);
QPushButton *but = new QPushButton(tr("Push"), this);
// Main layouts
QVBoxLayout *vLay = new QVBoxLayout(ui->centralWidget);
QHBoxLayout *hLay = new QHBoxLayout();
vLay->addLayout(hLay);
hLay->addWidget(bar);
// Spacer for expanding left and right sides
hLay->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
hLay->addWidget(lab);
hLay->addWidget(but);
vLay->addWidget(wid1);
vLay->addWidget(wid2);
vLay->addWidget(wid3);
// Some simple connect with lambda for navigation
connect(bar, &QTabBar::currentChanged, [=] (int index) {
wid1->setVisible(false);
wid2->setVisible(false);
wid3->setVisible(false);
switch(index) {
case 0: wid1->setVisible(true);
break;
case 1: wid2->setVisible(true);
break;
case 2: wid3->setVisible(true);
break;
default:{}
}
});
emit bar->currentChanged(0);
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."