Proper way to create 200 near identical QToolButtons.
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
Code:
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?
Re: Proper way to create 200 near identical QToolButtons.
Something like this ?
Code:
QStringList txts <<
"Estate" <<
"Cooper" <<
"Dutchy" <<
"Cabrio" <<
"Super car";
for( int i = 0; i < txts.size(); i++ )
{
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 ?
Re: Proper way to create 200 near identical QToolButtons.
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.
Re: Proper way to create 200 near identical QToolButtons.
Re: Proper way to create 200 near identical QToolButtons.
See also QButtonGroup or QSignalMapper for dealing with multiple signal sources and a single receiver slot.
Cheers,
_