Results 1 to 13 of 13

Thread: get function called by QPushbutton?

  1. #1
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default get function called by QPushbutton?

    Hi,
    Is there a way to get the function name that was executed by a button?


    thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    Is there a way to get the function name that was executed by a button?
    I assume you mean, "When my slot is invoked, which signal from which object was the one that invoked it?"

    Inside your slot:

    1. You get the QObject pointer of the object that emitted the signal using QObject::sender()
    2. You get the index (signal number) of the signal using QObject::senderSignalIndex()
    3. Using the sender's QObject pointer, you get its QMetaObject pointer using QObject::metaObject()
    4. Using the QMetaObject pointer and the index, you get the QMetaMethod using QMetaObject::method()
    5. Using QMetaMethod, you get the function (signal) name (as a QByteArray) using QMetaMethod::name()
    6. You can turn the QByteArray into a QString using the QString constructor that takes a QByteArray reference as argument



    Easy. But if that's not what you wanted, your question doesn't make a lot of sense.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    Sorry, was in a rush.
    Say I have a window with a button. When I clicked the button it changes the text on the button to "Clicked" via a function.

    Qt Code:
    1. myBtn = QPushButton("Click Me")
    2. myBtn.clicked.connect(changeLabel)
    3.  
    4. def changeLabel()
    5. myBtn.setText("Clicked")
    To copy to clipboard, switch view to plain text mode 

    I have the button object, what I'd like to get is the "changeLabel" function, so I can call it via another button/function.
    Here is my issue... I have a software, Nuke, and it's written in PyQt/Pyside. The developers documented everything in Nuke and how to access it, except, they left one out. The PAUSE button. And... guess what...my company wants me to build a tool that requires me to mess with the PAUSE. It is a QPushButton, and I want to know the name of the function that gets called when I click it.

    I'll take a look at your suggestions tomorrow at work.. QMetaMethod has peaked my interest.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: get function called by QPushbutton?

    The function that changes the label's text is setText, no?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    It is a QPushButton, and I want to know the name of the function that gets called when I click it.
    Well, the code I posted will get you the name of the button's method that is called when it is clicked, but you have to be in a slot connected to that button in order to find that out. I think you are asking the reverse question: "I have a button instance, what slots is its clicked() signal connected to?"

    That's the (unfortunately) protected method QObject::receivers(), which you can't call from the outside even if you do have a pointer to the button instance. Your Nuke QPushButton is a concrete class and you can't derive from it.

    So basically, from my original code you can find out "Who's calling me?", but unless you can derive from QPushButton you can't find out "Who is this button calling?"
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Nfrancisj (26th May 2017)

  7. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: get function called by QPushbutton?

    Rather than try to figure out the name of the slot, couldn't/shouldn't they just use QPushButton::setText to change the button's text? I'm not seeing the value in finding and executing the slot as opposed to just setting the text directly.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  8. The following user says thank you to jefftee for this useful post:

    Nfrancisj (26th May 2017)

  9. #7
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    @JeffTee : The code I posted is just a quick mock-up that describes the actual issue. I'm not trying to setText, instead, I'm trying to get the function called by the Slot. What I'm trying to get is LINE 4 in my code example.

    @d_stranz :
    So basically, from my original code you can find out "Who's calling me?", but unless you can derive from QPushButton you can't find out "Who is this button calling?"
    BUMMER!! Well..that blows.

    OKAY...Plan B.
    I haven't researched this yet, but I'll ask while I have your ear. Is there a way, given I have the button object, to execute/push the button via code? Say I add another button to my simple code example where the clicked SIGNAL of the second button executes the clicked SLOT of the first button.

    Thanks

  10. #8
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    @JeffTee.
    Ahh...I apologize. I now understand what your comment means. Sometimes the members in the Nuke framework are modified/masked and what is .setText() might be something else.

    Although I will continue to search, for now D_Stranz suggestion is what will do what I want.
    I'm using QMetaObject and the invoke method to trigger the button.

  11. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    Is there a way, given I have the button object, to execute/push the button via code?
    Sure. QAbstractButton::click() is a public (slot) method. You can call it just like any other public method of a class if you have a pointer to the instance of the class. So if you implement your own Pause button, you can add a connection between your button's clicked() signal and the Nuke button's click() slot. When you click your button, it -should- do the same things as if you had pushed Nuke's button instead because the Nuke button will in turn issue its own clicked() signal to all of the slots connected to it. (This assumes that these other slots don't check who the caller is before doing their thing).

    If you think about it, this kind of functionality is required in order to do automated GUI testing. The test code has to be able to emulate what a real user with a mouse would do - like push buttons, for one.
    Last edited by d_stranz; 26th May 2017 at 16:52.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  12. #10
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    Oh nice! Didn't know about .click(). Good to know... I was using .invoke(). Seems to do the same thing. Know of any benefit for choosing one over the other?

    I'm not sure if this is okay or not, but I'm going to ask another question. í ½í¸„ If it's not, I'll start a new thread.

    Other than the .objectName(), is there any other way to ID a QWidget? I'm noticing a lot of Qwidgets in Nuke have no name.

  13. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    Other than the .objectName(), is there any other way to ID a QWidget? I'm noticing a lot of Qwidgets in Nuke have no name.
    Names are optional. In C++, I usually just keep pointers to widgets I want to remember.

    Oh nice! Didn't know about .click()
    If you don't see something you are looking for in a Qt class, -always- check the base classes. The methods that provide convenient things that apply to all derived types are often found there.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #12
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    Quote Originally Posted by d_stranz View Post
    Names are optional. In C++, I usually just keep pointers to widgets I want to remember.
    right, but I didn't build the software. I'm getting the widget using .parent() and . children(), then storing them in a list and iterating through to find what I want. Alot of the widgets dont have names like you said.
    The problem is I am assuming the array get populated the same everytime and I'm using the index to access the child.

    Is there any other way to id a widget?

  15. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: get function called by QPushbutton?

    QObject::objectName() and the pointer to the instance are the only ways I know of.

    If you are looking for a specific widget of a specific type (like a QPushButton) you can always iterate through all the children, do a qobject_cast< QPushButton * > on each, and then call text() for those with non-null pointers until you find the one you want. If the widget text is internationalized, this is harder to do reliably, but if you are targeting a single language version then it won't be so bad.

    If the parent widget is built the same way each time, then I think the index would be stable. If the widget is dynamic (eg. different versions for different players, levels of the game, etc.) then there's no guarantee of anything.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  16. The following user says thank you to d_stranz for this useful post:

    Nfrancisj (31st May 2017)

Similar Threads

  1. Returned value of a function called from another class
    By Stanfillirenfro in forum Qt Programming
    Replies: 5
    Last Post: 6th February 2013, 13:02
  2. Database program exit when function called
    By micamica in forum Newbie
    Replies: 8
    Last Post: 13th July 2011, 17:07
  3. Custom QStyledItemDelegate paint function never called
    By mattsnowboard in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2011, 01:57
  4. error message appears when function is called
    By babygal in forum Qt Programming
    Replies: 11
    Last Post: 14th June 2010, 23:49
  5. QString - no member function called 'find'
    By pitterb in forum Newbie
    Replies: 1
    Last Post: 13th January 2009, 11:31

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.