Results 1 to 17 of 17

Thread: A list for storing member functions/slots?

Hybrid View

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

    Default Re: A list for storing member functions/slots?

    Thank you for your honest reply.

    You are right to point out that my terminology could be more concise, and I greatly appreciate it, despite any apparent frustration on my part. It forces me to elevate my venacular, and become a better programmer.

    By "Basic Properties" I merely meant to do the same thing as what QtDesigner's "Property Editor" does. For example, the property editor will allow you to set the maximum and minimum sizes of most QWidgets, as well as some other things. I am not sure what other term to use besides "Properties".
    By instructions, I merely meant to reference any various signals or slots pertaining to the QToolButton, and in my case, the task of connecting my signals to my slots. My use of the term "Instructions" must clearly be wrong, so I apologize.

    I'll try to be even more concise with my example. All I want to accomplish is this:
    Qt Code:
    1. ListOfSlots->append(NameOfSomeSlot());
    2. ListOfButtons->append(NameOfSomeButton)
    3.  
    4. for( int i = 0; i < ListOfSlots.size(); i++ )
    5. {
    6. connect(ListOfButtons->at(i),SIGNAL(clicked()), this, SLOT(ListOfSlots->at(i)()));
    7. }
    To copy to clipboard, switch view to plain text mode 

    I really appreciate your patience in all of this.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A list for storing member functions/slots?

    To me what you want to accomplish will not make your code shorter as you still have to add all those slot names to the list, but here you go:

    Qt Code:
    1. QList<const char*> slotList;
    2. slotList << SLOT(slot1());
    3. slotList << SLOT(slot2());
    4. // ...
    5.  
    6. for(int i=0; i< listOfButtons.size();++i) {
    7. connect(listOfButtons->at(i), SIGNAL(clicked()), this, slotList.at(i));
    8. }
    To copy to clipboard, switch view to plain text mode 

    Having said that I have to admit it all makes little sense to me. I'm assuming you are programming some kind of game (possibly a board game?) and you are using all those buttons for some kind of fields or pieces. In my opinion a much better approach would be to use Graphics View or at least to use a QSignalMapper and connect all your buttons to a single slot keyed by the button id.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Akiva (15th September 2013)

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

    Default Re: A list for storing member functions/slots?

    Ha, you are right about a few things there, especially about having to add all all these to a list partially defeating the purpose of such a loop. Despite this, we have our reasons to do this. One such reason is to easily change the signal and perhaps add some more later. For what its worth, here is our: project https://launchpad.net/dominion.linux

    So you were quite on target with the board game suspicion.

    Anyway, I will look at the Signal Mapper for sure; There is always so much to learn!

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

    Default Re: A list for storing member functions/slots?

    Since each QToolButton is a QObject you already have a generic way to get/set any declared property of the button without knowing the specific class of the button: QObject::property() and QObject::setProperty(). You can access information on available properties through the QMetaObject of the QToolButton.

    If all the tool buttons belong to a single parent object then you can access them using the child list of the parent using QObject::findChildren() without necessarily maintanng a separate list.

    Have a think about how these functions might be useful to you.

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

    Akiva (15th September 2013)

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

    Default Re: A list for storing member functions/slots?

    Good suggestion Chris.

    Additional to that all buttons could connect to the same slot, QObject::sender() could be used to retrieve the button and then QObject:roperty() could be used to extract the respective parameters.

    Of, if all buttons have the same types of parameters but just different values, create a QToolButton subclass that has methods for those and cast QObject::sender() to that class and call the getters directly.

    Another option would be to have separate receiver objects, one for each button. Each such instance would get its individual parameters upon construction, thus not needing any in the slot.

    e.g.
    Qt Code:
    1. class ButtonReceiver : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. ButtonReceiver( /* parameter */, QObject *parent = 0);
    6.  
    7. public slots:
    8. void onButtonClicked();
    9. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QList<ButtonReceiver*> receiverList;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. for (int i = 0; i < buttonList; ++i) {
    2. connect(buttonList[i], SIGNAL(clicked()), receiverList[i], SLOT(onButtonClicked()));
    3. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A list for storing member functions/slots?

    I have taken a look at the code in the repository mentioned by OP. The code there contains a couple hundred of practically identical slots generated by some external python tool.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Pointer to non static member functions within the class
    By Lykurg in forum General Programming
    Replies: 3
    Last Post: 24th April 2011, 07:37
  2. how to document classes and member functions?
    By jackal in forum Qt Programming
    Replies: 15
    Last Post: 9th April 2011, 22:02
  3. Replies: 1
    Last Post: 16th March 2011, 08:10
  4. Pointers to Member Functions
    By Doug Broadwell in forum General Programming
    Replies: 8
    Last Post: 15th May 2007, 23:08
  5. emiting signals from const member functions !?
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2006, 11:29

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.