PDA

View Full Version : How can I create button from arraylist.



electronicboy
5th October 2009, 15:21
Hi all,

I have an arraylist. This arraylist hold QPushButton's name.



List<QString> buttunNameList = new List<QString>;
buttonNameList<<"buttonname1"<<"buttonname2"<<"buttonname3";


I create button manuel.



QPushButton *buttonname1 = new QPushButton();
QPushButton *buttonname2 = new QPushButton();
QPushButton *buttonname3 = new QPushButton();


But I want to create button from arraylist. How can I create button from arraylist?

spirit
5th October 2009, 15:54
QStringList buttonsNames;
buttonsNames << "button1" << "button2" << "button3";
foreach (const QString &name, buttonsNames) {
QPushButton *pb = new QPushButton(name, this);
...
}

Lykurg
5th October 2009, 16:59
... and if you need to access the pointers later on, use a QList<QPushButton*>.

QList<QPushButton*> buttons;
// loop
buttons.append(pb);
// end loop
buttons.at(0)->... // == buttonname1
buttons.at(1)->... // == buttonname2
//...

electronicboy
5th October 2009, 21:08
@Spirit thanks for help. It's right.

@Lykurg sory. I don't understand ur algorithm. Can u explain ur algoritm?

Performans is very important for me. What do u recommended for high performans?

spirit
5th October 2009, 21:20
he means


QList<QPushButton *> buttons;//global variable or class-member
...
QStringList buttonsNames;
buttonsNames << "button1" << "button2" << "button3";
foreach (const QString &name, buttonsNames) {
QPushButton *pb = new QPushButton(name, this);
buttons << pb;
...
}
...
for (int i = 0; i < buttons.count(); ++i) {
QPushButton *pb = buttons.at(i);
pb->setText(QString("new text %1").arg(i));
}
...

electronicboy
5th October 2009, 21:25
Hımm. Thanks. I try this code;



buttonsNames << "button1" << "button2" << "button3"<<"button4"<<"button5";
foreach (const QString &name, buttonsNames) {
QPushButton *pb = new QPushButton(name,this);
pb->setObjectName(name);
connect(pb,SIGNAL(clicked()),this,SLOT(clickedButt on()));
}