PDA

View Full Version : Dynamic number of QLabel



jd
4th April 2008, 17:38
Hello,

I have a problem to create a dynamic number of QLabel and QLineEdit.

I have build a QList:



QList<QLineEdit*> dongleDataLineEdit;
QList<QLabel*> dongleDataLabel;


Now, the code to generate my QLabel an QLineEdit.



void CMainWindow::setDongleDataField() {
for (int i=0; i<plainDongle->DNG_Memmory/4; i++) {
dongleDataLabel.append(new QLabel);
dongleDataLineEdit.append(new QLineEdit);
}

displayDongleData();
}

void CMainWindow::displayDongleData() {
QGridLayout *dongleLayout = new QGridLayout;
int x = 0;

for (int i; i<plainDongle->DNG_Memmory/4; i++) {
dongleDataLabel[i]->setText(tr("Value "+i));
dongleLayout->addWidget(dongleDataLabel[i], x, 0);
dongleLayout->addWidget(dongleDataLineEdit[i], x, 1);
x++;
}
}


The problem is, my program does not start, it terminated without any error.

Can someone help me to fix the problem?

Lykurg
4th April 2008, 17:54
void CMainWindow::displayDongleData() {
QGridLayout *dongleLayout = new QGridLayout;


QGridLayout *dongleLayout = new QGridLayout(this);
and is CMainWindow shown? What is CMainWindow? have you called "app.exec()" in main?



int x = 0;

for (int i; i<plainDongle->DNG_Memmory/4; i++) {
dongleDataLabel[i]->setText(tr("Value "+i));
dongleLayout->addWidget(dongleDataLabel[i], x, 0);
dongleLayout->addWidget(dongleDataLineEdit[i], x, 1);
x++;
}
}

You don't need "int x" just use "i". And better use "i<dongleDataLabel.size()"

EDIT: once you have "dongleDataLabel." and once "dongleDataLabel->"? but that must result in an compiler error...

jd
4th April 2008, 18:09
Argh!

The mistake was her:


for (int i; i<plainDongle->DNG_Memmory/4; i++) {


i has no value...

so my program starts, but i have another problem with my layout.

If i call


setLayout(dongleLayout);


I become an error, wicht a layout is already set. How can I add my dongleLayout to CMainWindow?

CMainWindow is my QMainWindow I've created this with QtDesigner.
I can't found a function addLayout.

The error:
QWidget::setLayout: Attempting to add QLayout "" to QWidget "layoutWidget", which already has a layout