Solved Vector of QLabels
-
Hello everybody,
I am trying to create a QVector of QLabels to add dinamically labels such as:
QVector <QLabel*> labels;
but it does not run because theoretically it says use of undeclared identifier QLabel.
so, if I try to append a QLabel such as labels.append(new QLabel);
It does not work, why?
thanks!
-
#include <QLabel>
-
I did it, but It does not work...
-
@jss193
Hi
it must work or the #include is not in the same file where you have
QVector <QLabel*> labels;
or where you have
labels.append(new QLabel);
Also,
make sure you have
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
or
QT += widgets
in your .pro file. -
@jss193
What "does not work"?"use of undeclared identifier QLabel" sounds like a compile-time (or just possibly link-time) error, so I don't know what you have in mind by "it does not run"....
-
@mrjj Now it works, and it is creating QLabels dynamically, in my h file I have:
private: Ui::MainWindow *ui; QAbstractItemModel *model; int columns = 7; int rows = 0; QVector<QLabel*> labels; int countLabels = 0; private slots: void changeText(); };
And in .cpp:
void MainWindow::on_pushButton_clicked() { model->insertRows(0,1); QSpacerItem *item = new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Fixed); ui->horizontalLayout_2->addSpacerItem(item); for(int i =0;i<1;i++){ labels.append(new QLabel); ui->scrollArea->setWidgetResizable(true); ui->horizontalLayout_2->insertWidget(i,labels[countLabels]); countLabels =countLabels +1; } }
But it does not add labels to my layout, before I had the following code and it added Qlabels when pushing the button:
void MainWindow::on_pushButton_clicked() { model->insertRows(0,1); QSpacerItem *item = new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Fixed); ui->horizontalLayout_2->addSpacerItem(item); for(int i =0;i<1;i++){ QLabel *label1 = new QLabel("aaaa"); ui->scrollArea->setWidgetResizable(true); ui->horizontalLayout_2->insertWidget(i,label1); } }
Why now it is not creating the QLabels??
-
Fixed, it was creating empty QLabels I just added the text:
labels.append(new QLabel("TEST"));
-
-
@JonB said in Vector of QLabels:
Amazing how these things just happen!
Reminds me of:
- "My code doesnt work, and I dont know why" -> "My code works, and I dont know why"
:)