Results 1 to 20 of 29

Thread: dynamcly allocated buttons

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default dynamcly allocated buttons

    Hi to all!

    I've dynamically created buttons (the number of them is NOT known at compile time, but at runtime). It works ok, buttons are shown as I wanted. But now, I need to connect every button's signal clicked() to some slot function. How do I provide slot functions at runtime??
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: dynamcly allocated buttons

    It look like I did it on my own. But it does not work. Is it allowed to setup slot from one object and then call slot from different object?

    I got to gdb and i get following message:
    Qt Code:
    1. warning: Object::connect: No such slot COperationWIndow::m_pMerchandizeBrowser->fillMerchandize(iIndex)
    To copy to clipboard, switch view to plain text mode 

    Why this does not work?
    Last edited by MarkoSan; 10th December 2007 at 17:18.
    Qt 5.3 Opensource & Creator 3.1.2

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

    Default Re: dynamcly allocated buttons

    Quote Originally Posted by MarkoSan View Post
    How do I provide slot functions at runtime??
    I don't think you can add methods to classes at runtime in C++

    Quote Originally Posted by MarkoSan View Post
    Is it allowed to setup slot from one object and then call slot from different object?
    If you think about something like this:
    Qt Code:
    1. connect(object1, SIGNAL(sig()), object2, SLOT(sl())); // "this" is neither object1 nor object2
    To copy to clipboard, switch view to plain text mode 
    then yes.

  4. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: dynamcly allocated buttons

    Well, if I got it right, then:
    Qt Code:
    1. m_MerchandizeSelectorButtons.at(iIndex)->connect(m_pButtonMerchandizeConfirmer,
    2. SIGNAL(clicked()),
    3. this. SLOT(m_pMerchandizeBrowser->fillMerchandize(iIndex)));
    To copy to clipboard, switch view to plain text mode 

    then second "this" is the name of "target" object?
    Qt 5.3 Opensource & Creator 3.1.2

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: dynamcly allocated buttons

    You can't put a statement into SLOT() macro like that. Take a look at QButtonGroup or QSignalMapper.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: dynamcly allocated buttons

    QSIgnalMapper? I hate it, I've never understood it, I've read the docs about QSignalMapper but it is my humble opinion these docs are not written at the best ...
    Qt 5.3 Opensource & Creator 3.1.2

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: dynamcly allocated buttons

    Quote Originally Posted by MarkoSan View Post
    QSIgnalMapper? I hate it, I've never understood it, I've read the docs about QSignalMapper but it is my humble opinion these docs are not written at the best ...
    To be direct and honest, you should start with reading the article about signals and slots. It's the very basic principle of signals and slots that the SLOT() macro takes a slot name plus optional parameter types, without parameter names, and nothing more. Once you understand how signals and slots work, it should be straightforward to understand QSignalMapper docs. Besides, QButtonGroup should be even easier to use...
    J-P Nurmi

  8. #8
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: dynamcly allocated buttons

    Ok, but I simply must overcome this QSignalMapper since it is very useful. I understand signal/slots feature, but this QSignalMapper is giving me headache. Now let me dig into it ...
    Qt 5.3 Opensource & Creator 3.1.2

  9. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: dynamcly allocated buttons

    Does every button in a QList <QPushButton*> need its own QSIgnalMapper?
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamcly allocated buttons

    Quote Originally Posted by MarkoSan View Post
    Does every button in a QList <QPushButton*> need its own QSIgnalMapper?
    No, you need only one signal mapper for the whole list.

    In Qt docs you can find such example:
    Qt Code:
    1. ...
    2. for (int i = 0; i < texts.size(); ++i) {
    3. QPushButton *button = new QPushButton(texts[i]);
    4. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    5. signalMapper->setMapping(button, texts[i]);
    6. ...
    7. }
    8.  
    9. connect(signalMapper, SIGNAL(mapped(const QString &)),
    10. this, SIGNAL(clicked(const QString &)));
    11. ...
    To copy to clipboard, switch view to plain text mode 

    The setMapping() line tells the signal mapper that whenever the particular button invokes map(), the signal mapper should emit mapped(const QString &) signal with given parameter. So in this case you can replace all clicked() signals coming from different buttons with a single mapped( const QString & ) signal that comes from the signal mapper (of course parameter value depends on which button sent the original signal).

    You can have a mapped() signal also with integer, QObject * or QWidget * parameter --- you just have to use the other setMapping() method variants instead.

    In your case you can use QButtonGroup as jpn has suggested. Basically it's like a QSignalMapper but crafted especially for buttons.

  11. The following user says thank you to jacek for this useful post:

    MarkoSan (11th December 2007)

  12. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: dynamcly allocated buttons

    I would stick to QSignalMapper because I simply must overcome this topic. So, let me dig into it and try to reprogramm the example...

    Well, I've done this:
    Qt Code:
    1. m_MerchandizeSelectorButtons.at(iIndex)->connect(m_MerchandizeSelectorButtons.at(iIndex),
    2. SIGNAL(clicked()),
    3. m_pMerchandizeSelectorButtonSignalMapper,
    4. SLOT(map()));
    To copy to clipboard, switch view to plain text mode 

    What should I now put into map() slot? Call to other's object slot?
    Last edited by MarkoSan; 11th December 2007 at 00:19.
    Qt 5.3 Opensource & Creator 3.1.2

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.