How to add QGridLayout to QScrollArea?
-
Hi,
I have QGridLayout with 10 QLabels. After 5 seconds I will add to QGridLayout next QLabel, so after 5 minutes I will have many QLabels in QGridLayout. Each QLabel has FixedSize. I know that is impossible to have them in the screen at the same moment, so I would like to have Scroll Bar. I know that is QScrollArea class. I try something like that:
centralWidget()->setLayout(layout); layout->addWidget(scrollArea); scrollArea->setLayout(layoutWithQLabels);
But I don't get scroll bar and QLabels are overlap.
I would like something like that:
-
Hi
Would it not be easier just to use QListWidget?
Its a list of text lines that can scroll already. -
@mrjj I need QGridLayout
@JonB Thank you, but what with scrollArea?
Now I have:scrollWidget = new QWidget; area->setWidget(scrollWidget); scrollWidget->setLayout(layoutWithLabels); layout->addWidget(area); centralWidget()->setLayout(layout);
And I don't see any QLabels.
EDIT
I try setCentralWidget(area); but this doesn't work too... -
Hi,
Don't set a layout on a QScrollArea. Set it on the widget contained in the scroll area.
Then set the scroll area as central widget.
-
@SGaist Hi,
My code:layoutWithLabels = new QGridLayout; scrollWidget = new QWidget; area= new QScrollArea; area->setWidget(scrollWidget); scrollWidget->setLayout(layoutWithLabels); setCentralWidget(area);
and I add Widgets to layoutWithLabels. But I doesn't see any QLabels.
-
Well, the code you show here does not contain the part related to the labels. The issue might be in what you did not post.
-
@SGaist This is my whole code:
layoutWithLabels = new QGridLayout; scrollWidget = new QWidget; area= new QScrollArea; area->setWidget(scrollWidget); scrollWidget->setLayout(layoutWithLabels); setCentralWidget(area); for(int i=0;i<10;i++) { wektor.append(new QLabel("randomText")); wektor[i]->setStyleSheet("QLabel {background-color:red}"); layoutWithLabels->addWidget(wektor[i],i,0, Qt::AlignRight); wektor[i]->setFixedSize(50,20); }
wektor is QVector<QLabel*>
-
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.
-
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); });
-