Results 1 to 17 of 17

Thread: A list for storing member functions/slots?

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

    Question A list for storing member functions/slots?

    I apologize before hand for asking what I am sure is a very noobish question, but after looking around google for hours, I just can't figure out how to find an answer to my question, nor where to start looking in c++ documentation. My suspicion is that it has to do with my ignorance towards function pointers.

    Now, something like this makes sense to me:

    .h snippet
    Qt Code:
    1. QList<QToolButton *> *list_Button;
    2. QToolButton *card_AbandonedMine;
    To copy to clipboard, switch view to plain text mode 

    .cpp snippet
    Qt Code:
    1. card_AbandonedMine=new QToolButton;
    2. list_Button->append(card_AbandonedMine);
    To copy to clipboard, switch view to plain text mode 

    which I might make a loop like this:
    Qt Code:
    1. void DominionLinux::createCards()
    2. {
    3. for( int i = 0; i < list.size(); i++ )
    4. {
    5. list_Button->at(i)->setCheckable(true);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    That all works perfectly, and my hope is that I can also add function slots into this. The reason I can't use just one type of function, is because each slot has unique arguments.

    Now as far as creating some sort of list, I have tried applying the same principle to a member function/slot, but the closest I have come is for my program to run, but then unexpectantly crash. I went in with basically the same mindset:

    .h snippet
    Qt Code:
    1. QList<void *> *list_Slots;
    2. private slot:
    3. void *SLOTcard_AbandonedMine(bool Checked=false, bool matchOpponent=false);;
    To copy to clipboard, switch view to plain text mode 

    .cpp snippet
    Qt Code:
    1. void* DominionLinux::SLOTcard_AbandonedMine(bool Checked, bool TryMatchOpponent)
    2. {
    3. if (Checked == true)
    4. {
    5. ui->verticalLayout_Enabled->addWidget(card_AbandonedMine);
    6. }
    7. else if (Checked == false)
    8. {
    9. ui->verticalLayout_Disabled->addWidget(card_AbandonedMine);
    10. }
    11.  
    12. }
    13.  
    14. void DominionLinux::createList()
    15. {
    16. connect(card_AbandonedMine,SIGNAL(toggled(bool)), this, SLOT(SLOTcard_AbandonedMine(bool)));
    17. list_Slots->append(SLOTcard_AbandonedMine(false,false)); // If I disable this line, my program will work fine
    18. }
    To copy to clipboard, switch view to plain text mode 

    And it stops with this message:
    The inferior stopped because it received a signal from the Operating System.

    Signal name :
    SIGSEGV
    Signal meaning :
    Segmentation fault
    and this is the output
    g++ -o DominionLinux main.o DominionLinux.o qrc_cards.o moc_DominionLinux.o -lQt5Widgets -L/usr/lib/i386-linux-gnu -lQt5Network -lQt5Gui -lQt5Core -lGL -lpthread
    { test -n "" && DESTDIR="" || DESTDIR=.; } && test $(gdb --version | sed -e 's,[^0-9]\+\([0-9]\)\.\([0-9]\).*,\1\2,;q') -gt 72 && gdb --nx --batch --quiet -ex 'set confirm off' -ex "save gdb-index $DESTDIR" -ex quit 'DominionLinux' && test -f DominionLinux.gdb-index && objcopy --add-section '.gdb_index=DominionLinux.gdb-index' --set-section-flags '.gdb_index=readonly' 'DominionLinux' 'DominionLinux' && rm -f DominionLinux.gdb-index || true
    08:08:12: The process "/usr/bin/make" exited normally.
    08:08:13: Elapsed time: 00:07.
    All I really want is just a very base and simple example of how to make some sort of list for a function/ member function/ slot (if there is a difference), so I can call it in an iterator loop.

    Hope this made sense~ Thanks in advance.
    Last edited by Akiva; 14th September 2013 at 18:55. Reason: Fixed Typo

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

    What is the type of list_Slots?
    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. #3
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

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

    Ah sorry, that was a typo of mine when I was pasting the example code. The first line should read,
    QList<void *> *list_Slots;

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

    Why a pointer to a list? Did you initialize the pointer with an object? And why void*?
    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.


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

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

    From who I asked, and from what I read, I got the idea that <void*> was a way to accomplish this. Someone on IRC said that I could do it with old school function pointers, and typed out "void *". I did a google search for "QList<Void*> ...." and found some results, so I sheepishly tried to emulate it. Mind you I didnt find much code, and I really did not know where to search and find an answer. All the code relating to my attempt was posted above; I didnt initialize it with any object.

    Believe me; I am happy to scrap my existing code if some one can just give me a simple example of the way to do this.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

    Using void* is practically an error in C++. I don't know what you need those pointers for but most probably you should rather have some common base class for the objects you return and store that in the list. And I still don't know if you initialized the list pointer or not. It's very uncommon to have a pointer to a QList.
    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.


  7. #7
    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?

    Ignoring for a moment how you want to achieve it could you please explain what you are trying to achieve? For what purpose do you want this list of actions? To use a Perl idiom "there's more than one way to do it" but not all options are elegant or the first thing you think of.

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

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

    So I should make a new class, fill it with my 200 some odd slots, create 200 some odd objects, and append them to QList<myclass*>?

    Edit:
    Sorry, didnt see ninja post

    What I am trying to accomplish is simple:

    I have 215 QToolbuttons, and each one of them has a unique function. To make things simple, I would like to define the basic properties and instructions of the QToolbuttons by using an iterating "for" loop.
    Last edited by Akiva; 14th September 2013 at 23:06.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

    Quote Originally Posted by Akiva View Post
    I have 215 QToolbuttons, and each one of them has a unique function. To make things simple, I would like to define the basic properties and instructions of the QToolbuttons by using an iterating "for" loop.
    And how is storing a void* result of some slot call going to help with that?
    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.


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

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

    I already gave you an answer to that question; it was based on sheer ignorance, and a suggestion from #Qt. As said, I went ahead and failed, and tried to search for an answer to my question, but was not able to pinpoint an answer. That is why I have come to the beginners section of this QT forum to ask for some basic guidance. I should reiterate that I have no investment in maintaining my current code if it is simply incorrect, and I can not tell if you honestly are curious as to whether there is a way to use QList<void*> in order to accomplish my goal, or whether it is a rhetorical question aimed to berate me for my ignorance towards function pointers. Either way, what is important to me is to first have a working example, to which I can then use as an educational tool.

    Therefore what would be most helpful to me in my opinion is for a kind individual to volunteer a very basic example, including how to prototype the slot and the list into a header, how to declare/define/write it in the source, and then how to append the slot into the list. There is obviously no obligation for anyone to do that, but I hold out hope that someone does not mind to do so.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

    The problem is we still don't know what you are trying to do. Vague descriptions about "basic properties and instructions" are of little help. So far I only know that you have a slot declared to return void* that doesn't return anything that you call and try to store the unreturned return value in a list that's supposedly uninitialized. I can't see any attempt to use any for loop in your code thus I have no idea what you are trying to do, what you already tried, etc. It would be really helpful if you restated your problem in a clear and comprehensive way, possibly backed by an example.

    Just to be clear -- I don't see any "function pointer" in your code. The only pointers I see are a pointer to a list that itself holds pointers to "something". If by the following code:
    Qt Code:
    1. list.append(someFunc(param1, param2));
    To copy to clipboard, switch view to plain text mode 
    you thought you were appending a function pointer to a list then you were wrong. someFunc(param1, param2) is a function invocation, and not a function pointer. The latter is someFunc which I already told you a couple of posts ago.

    Still no idea what it has to do with your problem, whatever it is.
    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.


  12. #12
    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    Akiva (15th September 2013)

  15. #14
    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!

  16. #15
    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.

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

    Akiva (15th September 2013)

  18. #16
    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,
    _

  19. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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, 08:37
  2. how to document classes and member functions?
    By jackal in forum Qt Programming
    Replies: 15
    Last Post: 9th April 2011, 23:02
  3. Replies: 1
    Last Post: 16th March 2011, 09:10
  4. Pointers to Member Functions
    By Doug Broadwell in forum General Programming
    Replies: 8
    Last Post: 16th May 2007, 00:08
  5. emiting signals from const member functions !?
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2006, 12: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.