Results 1 to 11 of 11

Thread: array of slots?

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default array of slots?

    this is a simplified version of my problem: I have 2 sliders that send information to another class/object:

    Qt Code:
    1. // .h file
    2. QSlider *zXag[2];
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // .cpp file
    2.  
    3. zXag[0] = new QSlider(Qt::Vertical);
    4. zXag[1] = new QSlider(Qt::Vertical);
    To copy to clipboard, switch view to plain text mode 

    My problem is with the connect. I want to do something like this:

    Qt Code:
    1. connect(zXag[0], SIGNAL(valueChanged(int)), coin3dWidget, SLOT(setVertExag_0(int)));
    2. connect(zXag[1], SIGNAL(valueChanged(int)), coin3dWidget, SLOT(setVertExag_1(int)));
    To copy to clipboard, switch view to plain text mode 

    which works fine. But instead of 2 SLOT functions defined like above, I want something like:

    setVertExag[0](int)
    setVertExag[1](int)

    In other words, just as I've an array of 2 slider objects, I'd like an array of 2 slot functions. I know you cannot make arrays of functions in C++, but hopefully you can at least see what I'm trying to do here and can suggest a solution. Below is the actual function that generates my sliders (and connects), in case this helps explain:

    Qt Code:
    1. //function call
    2. for(int a=0; a<Total_Num_Sliders; a++)
    3. {
    4. mkZXag(a);
    5. }
    6.  
    7. //function
    8. void Window::mkZXag(int compNUM)
    9. {
    10.  
    11. zXag[compNUM] = new QSlider(Qt::Vertical);
    12. zXag[compNUM]->setSingleStep(1);
    13. zXag[compNUM]->setTickInterval(10);
    14. zXag[compNUM]->setTickPosition(QSlider::TicksRight);
    15.  
    16. connect(zXag[compNUM], SIGNAL(valueChanged(int)), coinWidget, SLOT(setVExag/*???*/(int)));
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: array of slots?

    Well, I think you can use QMetaObject to accomplish this. But you will need a very strict slot naming convention.
    You will have to iterate through all the methods of the receiving object( using QMetaObject::methodCount and QMetaObject::method(int) ) and for each method that has the type QMetaMethod::Slot compare it's signature with a QString = "setVertExag_" + sliderIndex.
    If they are equal(which means you have defined that slot), then just construct the slot name( which has to be const char*) and pass it to connect.

    Another solution is to make an array of slot names, but you will have to update it each time you add a new slot.


    Regards

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

    vonCZ (15th July 2007)

  4. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: array of slots?

    Quote Originally Posted by marcel View Post
    Another solution is to make an array of slot names, but you will have to update it each time you add a new slot.
    thanks Marcel. I've already got a class designed to manage this sort of thing, so it might be the easiest solution. I know this should be straight-forward, but I tried the following and it didn't work:

    Qt Code:
    1. char *zXagSlotName[] =
    2. {
    3. "na",
    4. "na",
    5. "set_zXag02(int value)",
    6. "set_zXag03(int value)",
    7. "set_zXag04(int value)",
    8. "set_zXag05(int value)",
    9. "set_zXag06(int value)",
    10. };
    11.  
    12. char *get_zXagSlotNames(int value) { return zXagSlotName[value]; }
    To copy to clipboard, switch view to plain text mode 

    but when I do this:

    Qt Code:
    1. connect(zXag[indexNUM], SIGNAL(valueChanged(int)), coinWidget, SLOT(getzXagSlotName(indexNUM)));
    To copy to clipboard, switch view to plain text mode 

    I get the following (not surprising) message:
    Qbject::connect: No such slot CoinWidget::getzXagSlotName(compNUM)
    what should I put here *

    SLOT(*)

    or Is this what you had in mind?

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: array of slots?

    It is because you use the SLOT macro. It is used to build a function signature(const char*).
    Since the array you already have is of const char*, you can pass directly an element of that array.
    Like:
    1. connect(zXag[indexNUM], SIGNAL(valueChanged(int)), coinWidget, zXagSlotName[indexNUM]);
    To copy to clipboard, switch view to plain text mode 


    Also be careful for index out of bounds errors on that array.

    Regards

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

    vonCZ (15th July 2007)

  7. #5
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: array of slots?

    thanx, but it still doesn't work; compiles, but I get the following message run-time:

    Object::connect: Use the SLOT or SIGNAL macro to connect CoinWidget::set_zXag02(int value)
    at least now it sees what I want to connect to, CoinWidget::set_zXag02(int value)... but still instructing me to use the SLOT macro.

  8. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: array of slots?

    Yeah, sorry about that .
    Change the array from char* to const char* and prepend to every slot name "1";
    Like this:
    Qt Code:
    1. const char* zXagSlotName[] =
    2. {
    3. "na",
    4. "na",
    5. "1set_zXag02(int value)",
    6. "1set_zXag03(int value)",
    7. "1set_zXag04(int value)",
    8. "1set_zXag05(int value)",
    9. "1set_zXag06(int value)",
    10. };
    To copy to clipboard, switch view to plain text mode 


    It should work now with the connect statement I have presented you earlier( just tested it here).


    Regards

  9. The following user says thank you to marcel for this useful post:

    vonCZ (15th July 2007)

  10. #7
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: array of slots?

    excellent, Marcel, thanks. One last correction though: I had to drop "value" from the parameter list like so:
    Qt Code:
    1. const char *zXagSlotName[] =
    2. {
    3. "na",
    4. "na",
    5. "1set_zXag02(int)",
    6. "1set_zXag03(int)",
    7. "1set_zXag04(int)",
    8. "1set_zXag05(int)",
    9. "1set_zXag06(int)",
    10. };
    To copy to clipboard, switch view to plain text mode 

    you probably copy/pasted my earlier mistake. I must say: Qt Centre Forum rocks.

  11. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: array of slots?

    Quote Originally Posted by vonCZ View Post
    excellent, Marcel, thanks. One last correction though: I had to drop "value" from the parameter list like so:
    Qt Code:
    1. const char *zXagSlotName[] =
    2. {
    3. "na",
    4. "na",
    5. "1set_zXag02(int)",
    6. "1set_zXag03(int)",
    7. "1set_zXag04(int)",
    8. "1set_zXag05(int)",
    9. "1set_zXag06(int)",
    10. };
    To copy to clipboard, switch view to plain text mode 
    you probably copy/pasted my earlier mistake. I must say: Qt Centre Forum rocks.
    Yes, I copied it from your post.
    I did a simpler test, with only one slot.
    Nevertheless, it is good it works.

    Note: if you want to do the same thing with signals, then you must prepend "2" to the signal name. But you can find out more by just looking at the SIGNAL/SLOT macro implementations in the Qt sources

    Regards.

  12. #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: array of slots?

    Why not just use a signal mapper? Do you really need an array of methods? How about just having a single method that takes two arguments instead of one? That would be a much cleaner design and you wouldn't have to dive into low-level metaobject info (and think about how it improves code maintenance!).

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

    vonCZ (19th July 2007)

  14. #10
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: array of slots?

    emit a user define signal(say sliderChange(int)) on slider's value change, then
    write in parent class:
    connect(Window, SIGNAL(sliderChange(int)), coin3dWidget, SLOT(setVertExag(int)));

    in setVertExag(int i)
    {
    QSlider *slider = qobject_cast<QSlider *>(sender());
    // here you will get the address of slider who emited the signal. and you can access that object data.

    }
    the above code is incomplete code. this is just hint.
    or you use QSignalMapper

  15. The following user says thank you to rajesh for this useful post:

    vonCZ (19th July 2007)

  16. #11
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: array of slots?

    Quote Originally Posted by wysota View Post
    Why not just use a signal mapper? Do you really need an array of methods?
    good point. I *think* i'll need multiple functions (that take input from the Sliders) in the Coin3d part of the app, but I should be able to call those specific functions using SigMap or Rajesh's suggestion....

Similar Threads

  1. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38
  2. Signals and Slots question
    By Thoosle in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2006, 00:24
  3. Adding slots in Designer
    By jamos in forum Qt Tools
    Replies: 5
    Last Post: 18th May 2006, 23:28
  4. How to create QPixmap from unsigned character array?
    By rashidbutt in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 18:25
  5. Missing slots
    By Mariane in forum Newbie
    Replies: 1
    Last Post: 5th February 2006, 01:50

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.