Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSignalWrapper uses wrong Method?
Forum Updated to NodeBB v4.3 + New Features

QSignalWrapper uses wrong Method?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qohelet
    wrote on last edited by
    #1

    I wanted to connect the clicked()-Signal of a QPushButton with my public Slot *int addButton(QIcon icon = 0);
    As I've seen in the documentation this is not possible because the slot will be expecting QIcon that the signal will not send.
    But I don't get any runtime-errors with that code:

    @connect(abt,SIGNAL(clicked()),this,SLOT(addBtn(icon)));@

    The QSignalMapper should solve my problem but still I'm doing something wrong. How is it done correctly?

    Btnmk.h
    @class Btnmk : public QWidget {
    Q_OBJECT

    public:
    Btnmk();

    private:
    /*
    ...
    */

    QSignalMapper *signalMapper;    
    QPushButton* getAddButton();
    

    public slots:
    int addBtn(QIcon *icon = 0);
    };@

    Btnmk.cpp
    @Btnmk::Btnmk() {
    signalMapper = new QSignalMapper(this);

    createGridGroupBox(); //Create the window..
    

    }

    int Btnmk::addBtn(QIcon *icon) {
    //Creates a new Button in the window by using the icon
    }

    QPushButton* Btnmk::getAddButton() {
    QPushButton *abt = new QPushButton("Plus", this);

    QIcon *icon = getTestIconFromSomewhere();
    
    connect(abt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(abt,(QWidget *)icon);
    connect(signalMapper, SIGNAL(mapped(QIcon)), this, SLOT(clicked(QIcon)));
    
    return abt;
    

    }
    @

    Especially the line
    *signalMapper->setMapping(abt,(QWidget )icon);
    puzzles me a lot. If I remove the *(QWidget ) I get the following error:

    @Btnmk.cpp:88:38: Fehler: ungültige Umwandlung von »QIcon*« in »int« [-fpermissive]
    /usr/include/qt4/QtCore/qsignalmapper.h:64:10: Fehler: Argument 2 von »void QSignalMapper::setMapping(QObject*, int)« wird initialisiert [-fpermissive]@

    I don't know the exact terms for this message in English but it should be something like:
    @Btnmk.cpp:88:38: Error: invalid Cast from »QIcon*« to »int« [-fpermissive]
    /usr/include/qt4/QtCore/qsignalmapper.h:64:10: Error: Argument 2 of »void QSignalMapper::setMapping(QObject*, int)« is initialized [-fpermissive]@

    I followed my IDE (Netbeans) to the Prototype of the function and get to gesignalmapper.h:
    @void setMapping(QObject *sender, int id);@
    How is that even possible? There is another prototype decelerated:
    @ void setMapping(QObject *sender, QObject *object);@
    QIcon surely inherits from QObject. Why does the compiler take int instead??

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Several things are wrong in your code:

      • QIcon doesn't inherit from QObject
      • QIcon is not a QWidget
      • Casting QIcon to QWidget like that is wrong
      • Slots don't have return values

      What exactly do you want to achieve ?

      On a side note, your thread title is a bit wrong QSignalWrapper is not a Qt class

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qohelet
        wrote on last edited by
        #3

        I'm coming right from Java (I was doing C++ years ago the last time...), so I assumed the Q*-Classes would all inherit from QObject. Now as I check it I see it's not like that.

        As shown in the code I have the slot:

        @ int addBtn(QIcon *icon = 0);@

        I just want to trigger this event (Parameter included) as someone clicks another button.
        How do I achieve that?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Like I wrote before, a slot doesn't return anything.

          To have something clean, you should rather have a parameterless slot where you will implement the button creation logic.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qohelet
            wrote on last edited by
            #5

            I know that a slot doesn't return anything, that's the reason why I used the QSignalMapper.
            But I don't know how to use it with a QIcon

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You can't do it with QSignalMapper. If I understand correctly when you click on that button a new one should be created with an icon, then what should happen ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qohelet
                wrote on last edited by
                #7

                So how can I do it? (Preferred not to subclass anything)

                Nothing, that's all.

                I just want to call the method addBtn(QIcon *icon) with the parameter if someone clicks a button.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Like I said, do it indirectly. Have a parameterless slot where you will create the button retrieving the icon has you need it.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qohelet
                    wrote on last edited by
                    #9

                    That's the problem. I need to provide a Slot with a parameter. Otherwise I'd have done so already

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      What are the exact requirements ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        qohelet
                        wrote on last edited by
                        #11

                        A slot with the parameter QIcon which creates a new Button with that QIcon and is called if you click something.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Directly, you can't have that. Qt's signals must be connected to slots with matching signatures or less (but still matching) parameters.

                          QSignalMapper works with either QString, QObjects, or QWidgets. So you can't have a slot with a QIcon * parameter. However and since there's chances that you build your icon using a path to an image file, you could use that to build your new button. But as i've stated before, your slots should not have any return type other than void.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved