Stop QWidgets from stacking on top of each other dynamically - qt

I create all my QWidgets with code and put them in a tab. However all of the widgets are stacked on top of each other. Is there a way to move widgets dynamic?. It would be possible to move the widgets dynamic to how many there already is in the widget?.As it is know I have to move them with move() which could be hard to keep track on when more objects are added.
QTabWidget* MainWindow::CreateTabWidget(){
QTabWidget* tabWidget = new QTabWidget(ui->centralWidget);
tabWidget->setFixedSize(this->size().width(),this->size().height()- 40);
QWidget* tab = new QWidget();
QLabel* label = new QLabel("Sektionnamn",tab);
QLineEdit* line = new QLineEdit(tab);
line->move(0,20);
tabWidget->addTab(tab,"Tab 1");
return tabWidget;
}

Use QLayout:
QLabel* label = new QLabel("Sektionnamn");
QLineEdit* line = new QLineEdit();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(line);
tab->setLayout(layout);

Related

How to create a scrollable widget using QHBoxLayout and QScrollArea

I am trying to create a widget with scrollable using the QHBoxLayout and QScrollArea but i am unable to succeed to create a GUI,here is my code can any one please suggest me where i need to change.
Any help would be appreciated
mainLayout = new QHBoxLayout(this);
mainLayout->setSizeConstraint(QLayout::SetMaximumSize);
QCheckBox *sam_box = new QCheckBox(this);
sam_box->setText("HAIIIIIIIIIII");
sam_box->setFixedSize(10000,10000);
QCheckBox *so_box = new QCheckBox(this);
so_box->setText("HOWWWWWWWWWWWW");
so_box->setFixedSize(150000,15000);
mainLayout->addWidget(sam_box);
mainLayout->addWidget(so_box);
QScrollArea *scrollarea = new QScrollArea(this);
scrollarea->setBackgroundRole(QPalette::Shadow);
scrollarea->setFixedSize(700,500);
scrollarea->setLayout(mainLayout);`
Here is my output screen
Thanks in advance,
Rohith.G
One way to achieve this is by below logic
centralwidget() -> QHBoxLayout-> QScrollArea -> QWidget-> add the check box
The code flow & comments will explain the logic in detail.
QWidget *wgt = new QWidget();//new code
wgt->setGeometry(0,0,500,500); //new code
//QHBoxLayout mainLayout = new QHBoxLayout(this);
QHBoxLayout *mainLayout = new QHBoxLayout(this->centralWidget());
mainLayout->setSizeConstraint(QLayout::SetMaximumSize);
//QCheckBox *sam_box = new QCheckBox(this);
QCheckBox *sam_box = new QCheckBox(wgt);
sam_box->setText("HAIIIIIIIIIII");
//sam_box->setFixedSize(10000,10000);
sam_box->setFixedSize(200,200); //make it small for quick testing
//QCheckBox *so_box = new QCheckBox(this);
QCheckBox *so_box = new QCheckBox(wgt);
so_box->setText("HOWWWWWWWWWWWW");
//so_box->setFixedSize(150000,15000);
so_box->setFixedSize(150,150); //make it small for quick testing
//mainLayout->addWidget(sam_box);
//mainLayout->addWidget(so_box);
QScrollArea *scrollarea = new QScrollArea();
scrollarea->setBackgroundRole(QPalette::Shadow);
//scrollarea->setFixedSize(700,500); //scroll area cant be resized
mainLayout->addWidget(scrollarea); //new code
//scrollarea->setLayout(mainLayout);
scrollarea->setWidget(wgt);
Below code will generate 30 QCheckBoxes added each ten in a vertical box layout and all the vertical layout in a horizontal box layout
QScrollArea *scrl = new QScrollArea();
scrl->setGeometry(0,0,300,300);
QWidget *wgtMain = new QWidget();
QHBoxLayout *hboxMain = new QHBoxLayout(wgtMain);
for(int iCount=0;iCount<3;iCount++){
QWidget *wgtSub = new QWidget();
QVBoxLayout *vboxSub = new QVBoxLayout(wgtSub);
for(int jCount=0;jCount<10;jCount++){
QCheckBox *chk = new QCheckBox("Check Box " + QString::number(jCount+1) + " of " + QString::number(iCount+1));
vboxSub->addWidget(chk);
}
hboxMain->addWidget(wgtSub);
}
scrl->setWidget(wgtMain);
scrl->show();

QTabWidget does not respect spacing from layout

I have this test case:
// Scroll
QScrollArea *sa = new QScrollArea(ui->centralWidget);
sa->setWidgetResizable( true );
// Layout for widgets
QVBoxLayout *vl_2 = new QVBoxLayout();
vl_2->setSpacing(0);
// Widget to attach the scroll to and the layout
QWidget *widget = new QWidget()
widget->setLayout(vl_2);
sa->setWidget(widget);
// Test widgets
QComboBox *cb_1 = new QComboBox();
QComboBox *cb_2 = new QComboBox();
vl_2->addWidget( cb_1 );
vl_2->addWidget( cb_2 );
And the widgets have 0 space between them.
But if I add them to a QTabWdiget, it all breaks as if QTabWidget does not respect the set setSpacing(0);.
// TabWidget
QTabWidget *run_results = new QTabWidget(ui->centralWidget);
run_results->resize( this->size().width() -20, this->size().height() -80 );
run_results->show();
// Scroll
QScrollArea *sa = new QScrollArea(ui->centralWidget);
sa->setWidgetResizable( true );
// Layout for widgets
QVBoxLayout *vl_2 = new QVBoxLayout();
vl_2->setSpacing(0);
// Widget to attach the scroll to and the layout
QWidget *widget = new QWidget()
widget->setLayout(vl_2);
sa->setWidget(widget);
// Add the scroll to as the TabWidget tab.
run_results->addTab(sa, "test");
// Test widgets
QComboBox *cb_1 = new QComboBox();
QComboBox *cb_2 = new QComboBox();
vl_2->addWidget( cb_1 );
vl_2->addWidget( cb_2 );
Anyone know what I need to do to force QTabWidget to not resize and move my widgets so that they take all the space?
I tried to add Qt::AlignTop to the addWdiget but it did nothing other than place the first widget at the top and the next in the middle of the screen.
I understand where I went wrong.
In the first case I add my scrollarea as a sub-widget to the centralwidget. In the second example I add the scrollarea as the centralwidget which expands it to the whole tabwidget.
I resolved the second case by first adding a holder QWidget as the tabwidget and then adding the scrollarea as a sub-widget to it.

Add widgets to a ScrollArea

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;

How to delete a space between QLabel and QLineEdit

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

How to allow a QWidget to expand to an entire tab within a QTabWidget?

I've never been too great when it comes to layouts and size policies, so this is likely a simple answer, but here goes.
When I add a new tab to a QTabWIdget, and set the widget to a QSplitter, the splitter will use the entire amount of space given. However, when I put a QSplitter within a QWidget, and add a tab using the QWidget, it will not use the give space - only what the splitter size is.
I would like to stay away from a QLayout, as I have all necessary widgets within splitters. Just getting my QWidget to use all space in the tab (which should allow the QSplitter to use all space in the QWIdget) is what I need.
I've built a sample below showing the comparison, as well as the result. Long question short - how can I get scenario one (within "Tab One") to appear like scenario two ("Tab Two") and still use a parent QWidget for the Spliiter?
ui->setupUi(this);
QTabWidget* pTab = new QTabWidget(this);
this->setCentralWidget(pTab);
//Splitter belongs to a QWidget, which belongs to the Tab. Does not use all space
QWidget* pWidget = new QWidget(this);
QSplitter* pSplitter = new QSplitter(pWidget);
QListWidget* pList = new QListWidget(this);
QListWidget* pList2 = new QListWidget(this);
pSplitter->addWidget(pList);
pSplitter->addWidget(pList2);
pTab->addTab(pWidget, "Tab One");
//Splitter belongs to the Tab. Uses all space
QListWidget* pList3 = new QListWidget(this);
QListWidget* pList4 = new QListWidget(this);
QSplitter* pSplitter2 = new QSplitter(this);
pSplitter2->addWidget(pList3);
pSplitter2->addWidget(pList4);
pTab->addTab(pSplitter2, "Tab Two");
Change this:
QWidget* pWidget = new QWidget(this);
QSplitter* pSplitter = new QSplitter(pWidget);
To this:
QWidget* pWidget = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(pWidget);
QSplitter* pSplitter = new QSplitter(pWidget);
layout->addWidget(pSplitter);

Resources