PDA

View Full Version : dynamically create QLabels



jeffmetal
17th June 2011, 22:17
I am trying to create QLabels's depending on the amount of fields in a give file which will change. The NumFieldsH(hRfmfile) is a c function that that returns the number of fields as an int.



/* Create the data layout */
dataGroupBox = new QGroupBox;
gridGroupBox->setTitle(tr("Buttons"));
datalayout = new QGridLayout;

QLabel field[NumFieldsH(hRfmfile)];
for(i = 0; i < NumFieldsH(hRfmfile); i++){

field[i] = new QLabel("test");
datalayout->addWidget(field[i],i,1);
}

dataGroupBox->setLayout(datalayout);


the header


public:
QGroupBox *dataGroupBox;
QGridLayout *datalayout;

Im getting errors when i compile this for the 2 lines in the loop, what am I doing wrong?

Santosh Reddy
17th June 2011, 23:14
QLabel field[NumFieldsH(hRfmfile)];
You are declaring a array of QLabel on stack, when doing so, you need to give a constant value as size.

If you want a variable size array, or don't know the size at compile time, then this is the very reason to use dynamic memory from heap.

Something like this


QList<QLabel *> field;
int size = NumFieldsH(hRfmfile); // this way NumFieldsH(hRfmfile) is called only once
for(i = 0; i < size ; i++){ // if NumFieldsH(hRfmfile) is use in for() then it is called for every iteration,which is not good is NumFieldsH(hRfmfile) if trying to read a IO device (file etc)

field.append(new QLabel("test"));
datalayout->addWidget(field[i], i, 1);
}

wysota
17th June 2011, 23:15
You forgot to tell us what the errors are.

stampede
17th June 2011, 23:29
I'm guessing:

field[i] = new QLabel("test");
: cannot convert 'QLabel *' to 'QLabel' in assignment

datalayout->addWidget(field[i],i,1);
: no matching function to call to QGridLayout::addWidget(QLabel,int,int), candidates are QGridLayout::addWidget(QWidget*,int,int)
or something like that :p
btw. I'm not commenting on solution, because Santosh code should work :)

----
edit2:

You are declaring a array of (...) on stack, when doing so, you need to give a constant value as size.
Depends on the compiler. For example in g++ 4.5.x following code is perfectly valid:


#include <iostream>
#include <stdlib.h>

int main( int argc, char ** argv ){
if( argc > 1 ){
int x[ atoi(argv[1]) ];
std::cout << sizeof(x)/sizeof(int); // should print a value given in program argument
}
return 0;
}

This feature is called variable-length arrays (VLA), and is part of C99 standard supported by gcc (http://gcc.gnu.org/c99status.html).

7ymekk
18th June 2011, 12:57
QLabel field[NumFieldsH(hRfmfile)];
for(i = 0; i < NumFieldsH(hRfmfile); i++){

field[i] = new QLabel("test");
datalayout->addWidget(field[i],i,1);

I suggest to write it like this:


QLabel* field[NumFieldsH(hRfmfile)];
for(i = 0; i < NumFieldsH(hRfmfile); i++){

field[i] = new QLabel("test");
datalayout->addWidget(field[i],i,1);

and I'm guessing You're getting non-scalar exception

better way is the Santosh solution, with QList...

jeffmetal
19th June 2011, 19:24
thanks you santosh for the help I did it the way you recommended and it worked I also added another row of QlineEdits that i have called data. The problem I am now having is how do I go about setting a new value for the QLineEdits in another method.

I cant seem to access them using data[i].setValue("test"); to work i get the error



/usr/lib64/qt4/include/QtGui/qwidget.h: In member function ‘void Dialog::update()’:
/usr/lib64/qt4/include/QtGui/qwidget.h:672: error: ‘QWidgetData* QWidget::data’ is private
dialog.cpp:234: error: within this context
dialog.cpp:234: error: ‘class QWidgetData’ has no member named ‘setValue’


im assuming this is because the data Lineedits are being declared in the constructor and are out of the scope of my new method.