Results 1 to 5 of 5

Thread: Proper way to create 200 near identical QToolButtons.

  1. #1
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question 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
    Qt Code:
    1. card_Estate = new QToolButton();
    2. card_Estate->setCheckable(true);
    3. card_Estate->setText("Estate");
    4. card_Estate->setToolTip("<img src=':/Cards/Estate.jpg' width='250'>");
    5. ui->verticalLayout_Disabled->addWidget(card_Estate);
    6. connect(card_Estate, SIGNAL(clicked(bool)), this, SLOT(SLOTcard_Estate(bool)));
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Proper way to create 200 near identical QToolButtons.

    Something like this ?
    Qt Code:
    1. QStringList txts << "Estate" << "Cooper" << "Dutchy" << "Cabrio" << "Super car";
    2. for( int i = 0; i < txts.size(); i++ )
    3. {
    4. QToolButton *m_button = new QToolButton();
    5. m_button->setCheckable(true);
    6. m_button->setText(txts.at(i));
    7. QString img_src("<img src=':/Cards/%1.jpg' width='250'>");
    8. img_src = img_src.arg(txts.at(i));
    9. m_button->setToolTip(img_src);
    10. ui->verticalLayout_Disabled->addWidget(m_button);
    11. connect(m_button, SIGNAL(clicked(bool)), this, SLOT(SLOTcard_Estate(bool)));
    12. }
    To copy to clipboard, switch view to plain text mode 
    What about signal clicked(). Should it be connected to one slot or every button to another slot ?

  3. The following user says thank you to Lesiok for this useful post:

    Akiva (8th September 2013)

  4. #3
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,689
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Proper way to create 200 near identical QToolButtons.

    QDir is your friend

  6. The following user says thank you to ChrisW67 for this useful post:

    Akiva (8th September 2013)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    Akiva (9th September 2013)

Similar Threads

  1. Replies: 9
    Last Post: 17th May 2015, 17:46
  2. Tab focus on QToolBar QToolButtons
    By bmn in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2011, 03:38
  3. Qt Designer Setting "defaultAction" of QToolButtons in Qt designer.
    By LonelyStar in forum Qt Tools
    Replies: 1
    Last Post: 7th October 2011, 21:46
  4. QWidget containing two QToolButtons
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2007, 00:25
  5. Various icon sizes for QToolButtons on QToolbar
    By Erlendhg in forum Qt Programming
    Replies: 4
    Last Post: 30th June 2007, 19:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.