ScrollArea in QT Issue? - qt

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);

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();

Stop QWidgets from stacking on top of each other dynamically

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);

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

Resources