PDA

View Full Version : Proper way to create 200 near identical QToolButtons.



Akiva
8th September 2013, 01:33
Someone told me there is no proper way to do what I want to do, so I guess just tell me what you would do.

Here is a sample of my code from my .cpp file


card_Estate = new QToolButton();
card_Estate->setCheckable(true);
card_Estate->setText("Estate");
card_Estate->setToolTip("<img src=':/Cards/Estate.jpg' width='250'>");
ui->verticalLayout_Disabled->addWidget(card_Estate);
connect(card_Estate, SIGNAL(clicked(bool)), this, SLOT(SLOTcard_Estate(bool)));


Now I need to duplicate this code 200 times. The only difference in each duplication is that the word "Estate" is replaced by another word, and that other word corresponds to the name of a file found in a folder or resource file: "<img src=':/Cards/Estate.jpg' width='250'>" , <img src=':/Cards/Copper.jpg' width='250'>, <img src=':/Cards/Dutchy.jpg' width='250'>,
etc~


Thanks for any ideas.

edit:
Oh, and I forgot to ask; is this same sort of thing possible with the header file as well, or is it best in that case to just manually declare 200 buttons?

Lesiok
8th September 2013, 08:00
Something like this ?

QStringList txts << "Estate" << "Cooper" << "Dutchy" << "Cabrio" << "Super car";
for( int i = 0; i < txts.size(); i++ )
{
QToolButton *m_button = new QToolButton();
m_button->setCheckable(true);
m_button->setText(txts.at(i));
QString img_src("<img src=':/Cards/%1.jpg' width='250'>");
img_src = img_src.arg(txts.at(i));
m_button->setToolTip(img_src);
ui->verticalLayout_Disabled->addWidget(m_button);
connect(m_button, SIGNAL(clicked(bool)), this, SLOT(SLOTcard_Estate(bool)));
}

What about signal clicked(). Should it be connected to one slot or every button to another slot ?

Akiva
8th September 2013, 08:23
Wow, that is fantastic, thanks.
Exactly the sort of thing I was looking for. The only thing better would be a way to populate that list with the names of files in one of my directories or resource files.

ChrisW67
8th September 2013, 08:32
QDir is your friend

anda_skoa
8th September 2013, 12:35
See also QButtonGroup or QSignalMapper for dealing with multiple signal sources and a single receiver slot.

Cheers,
_