QSignalWrapper uses wrong Method?
-
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_OBJECTpublic:
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?? -
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
-
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? -
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.
-
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 -
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 ?
-
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.
-
Like I said, do it indirectly. Have a parameterless slot where you will create the button retrieving the icon has you need it.
-
That's the problem. I need to provide a Slot with a parameter. Otherwise I'd have done so already
-
What are the exact requirements ?
-
A slot with the parameter QIcon which creates a new Button with that QIcon and is called if you click something.
-
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.