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. Custom Signal/Slot Implementation for a QPushButton

Custom Signal/Slot Implementation for a QPushButton

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 15.4k Views
  • 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.
  • M Offline
    M Offline
    mchris357
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchris357
      wrote on last edited by
      #2

      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..

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #3

        [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_QPushButton

        Cheers,
        Leon

        http://anavi.org/

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CMGeorge
            wrote on last edited by
            #5

            @connect( ViewRawData, SIGNAL( output( StrainID ) ), this, SLOT( InvokeRawDataTable( StrainID ) ) );@

            is wrong. The corect form is:

            @connect( ViewRawData, SIGNAL( output( int ) ), this, SLOT( InvokeRawDataTable( int ) ) );@

            You send and receive a data type using Signal Slot, not the variable name.

            iOS & Qt Developer
            Happy Qt-ing

            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