How to add QGridLayout to QScrollArea?
-
Just a silly idea: build the complete widget first so it has all its content in place and as last step create the scroll area and put the widget in.
-
@SGaist hmmm, it works, but there is a new problem.
Now my code looks like:
layoutWithLabels = new QGridLayout; for(int i=0;i<30;i++) { wektor.append(new QLabel(QString::number(i))); wektor[i]->setStyleSheet("QLabel {background-color:red}"); layoutWithLabels->addWidget(wektor[i],i,0); wektor[i]->setFixedSize(50,20); } scrollWidget = new QWidget; scrollWidget->setLayout(layoutWithLabels); area= new QScrollArea; area->setWidget(scrollWidget); setCentralWidget(area);But if you read my first post I would like to add a new QLabel to QGridLayout after 5 sec. I have QTimer and slot:
void MainWindow::timeoutSlot() { static int i=30; wektor.append(new QLabel(QString::number(i))); wektor[i]->setStyleSheet("QLabel {background-color:red}"); layoutWithLabels->addWidget(wektor[i],i,0); wektor[i]->setFixedSize(50,20); i++; }and it add new QLabel, but look:
This is my app after start:

and this is my app after add new QLabels:

QLabels are overlap
-
There's no need for that static i variable. You can use the vector size for your computation.
-
@SGaist hmmm, it works, but there is a new problem.
Now my code looks like:
layoutWithLabels = new QGridLayout; for(int i=0;i<30;i++) { wektor.append(new QLabel(QString::number(i))); wektor[i]->setStyleSheet("QLabel {background-color:red}"); layoutWithLabels->addWidget(wektor[i],i,0); wektor[i]->setFixedSize(50,20); } scrollWidget = new QWidget; scrollWidget->setLayout(layoutWithLabels); area= new QScrollArea; area->setWidget(scrollWidget); setCentralWidget(area);But if you read my first post I would like to add a new QLabel to QGridLayout after 5 sec. I have QTimer and slot:
void MainWindow::timeoutSlot() { static int i=30; wektor.append(new QLabel(QString::number(i))); wektor[i]->setStyleSheet("QLabel {background-color:red}"); layoutWithLabels->addWidget(wektor[i],i,0); wektor[i]->setFixedSize(50,20); i++; }and it add new QLabel, but look:
This is my app after start:

and this is my app after add new QLabels:

QLabels are overlap
-
Since you have your labels in on column, why not use a QVBoxLayout ?
-
Hi
Tried your code and it worked?
(not including the static int )
QTimer::singleShot(5000, [this, layoutWithLabels]() { wektor.append(new QLabel(QString::number( wektor.size())) ); wektor.last()->setStyleSheet("QLabel {background-color:blue}"); layoutWithLabels->addWidget(wektor.last(), wektor.size(), 0); wektor.last()->setFixedSize(50, 20); }); -
