Custom Signal/Slot Implementation for a QPushButton
-
Hello again,
I am attempting to implement a signal/slot connection that must pass a parameter to the slot. From everything I've read, I need to "overload" the signal with the parameter so it can be passed to the slot.
In an effort to accomplish this, I created this class (derived from QPushButton):
@
class StrainButton : public QPushButton{ /BEGIN CLASS STRAINBUTTON DECLARATION/
Q_OBJECT
public:
StrainButton( QString, int, QWidget* parent = 0 );
protected:
int StrainID;
public slots:
void triggerOutput( );
signals:
void output( int );
}; /CLOSE CLASS STRAINBUTTON DECLARATION/
@Here's the implementation file:
@
StrainButton::StrainButton( QString ButtonText, int ButtonID, QWidget* Parent ) : QPushButton( Parent ){ /BEGIN STRAINBUTTON DEFAULT CLASS CONSTRUCTOR DEFINITION/
StrainID = ButtonID;
connect( this, SIGNAL( clicked() ), this, SLOT( triggerOutput( ) ) );
} /BEGIN STRAINBUTTON() DEFAULT CLASS CONSTRUCTOR DEFINITION/
void StrainButton::triggerOutput( )
{
emit output( StrainID );
}
@Basically, my intention was to invoke triggerOutput() when clicked() is triggered, which would in turn emit output( int ). I could then use the output( int ) signal to call the slot that required the int parameter.
Here's the signal/slot call to accomplish the above:
@
StrainButton* ViewRawData = new StrainButton( tr( "Data Summary" ), StrainID, this );connect( ViewRawData, SIGNAL( output( StrainID ) ), this, SLOT( InvokeRawDataTable( StrainID ) ) );
@Can anybody give me an idea of where my error is? The only place I can think of is the connect call in the constructor, but I don't see how that would be a problem.
This is the message I receive from QT Creator when the buttons appear on the screen:
Object::connect: No such signal StrainButton::output( StrainID ) in ..\VCCalculator\source_files\vctablayouts.cpp:481
If I use the "autocomplete" in QT Creator, it does show output( int ) as a signal when I type SIGNAL(
Thanks in advance for your help!
-
Please disregard, I figured it out.
I needed to make the call like this:
@
connect( ViewRawData, SIGNAL( output( int ) ), this, SLOT( InvokeRawDataTable( int ) ) );
@Question: How do I define what the value of the int parameter is?
Disregard again...I assume it is the emit statement that decides it:
@
void StrainButton::triggerOutput( )
{
emit output( StrainID );}
@Mods, feel free to move this if you need to. Even better, is there a link you can attach that explains how to do this? I'm sure I'm not the only person who didn't quite catch how this works..
-
[quote author="mchris357" date="1305252627"]Even better, is there a link you can attach that explains how to do this?
[/quote]Hi,
You may find these links useful:
"Signals & Slots":http://doc.qt.nokia.com/latest/signalsandslots.html
"How to Use QPushButton":http://developer.qt.nokia.com/wiki/How_to_Use_QPushButtonCheers,
Leon -
Refer to QSignalMapper for one solution.
Another solution is the way you show in your last code snippet. Just declare the signal in the subclass, and emit it with the value you need. Which value that is, only you know. Perhaps you have added a property to your subclass that contains that value?
-