PDA

View Full Version : How To Create Array of Widgets in Qt4?



redhat
12th January 2009, 11:35
Hi
How I Can Create Array of Widgets (For Example: QLineEdit) on Qt4?

Please Help Me!
Thanks.

Lykurg
12th January 2009, 13:16
Maybe
QList<QLineEdit*> myList;
mylist.append(new QLineEdit());

depends on what you want to do.

Lykurg

vonCZ
26th April 2009, 09:56
Is this bad? I want to create an array of widgets to put on a QStackedWidget. Can I do this:




// ...in myfile.cpp...

QPushButton *pb[2];

for(int a=0; a<2; a++)
{
pb[a] = new QPushButton;
myStack->addWidget(pb[a]);
}

It compiles and seems to work. Previously when I've needed an array of Widgets, I would declare

QPushButton *pb[2];


in the header file. But is there any reason I cannot declare it in the .cpp file like above?

vonCZ
26th April 2009, 13:24
follow-up: if I need to replace the "2" with "getNumber()" like so:




// ...in myfile.cpp...

// QPushButton *pb[2];

QPushButton *pb[ getNumber() ];

// or
// const int tempNum = getNumber();
// QPushButton *pb[tempNum];

for(int a=0; a<2; a++)
{
pb[a] = new QPushButton;
myStack->addWidget(pb[a]);
}

I get an error: expected constant expression. Is there a way around this?

ComaWhite
27th April 2009, 10:37
follow-up: if I need to replace the "2" with "getNumber()" like so:




// ...in myfile.cpp...

// QPushButton *pb[2];

QPushButton *pb[ getNumber() ];

// or
// const int tempNum = getNumber();
// QPushButton *pb[tempNum];

for(int a=0; a<2; a++)
{
pb[a] = new QPushButton;
myStack->addWidget(pb[a]);
}I get an error: expected constant expression. Is there a way around this?

You will have to wait for constexpr in C++0x to be able to do it.