How to add one parameter to the clicked SIGNAL?
-
@ChristianMontero Hey sorry, was in the middle of editing my previous post, check above for a code example. :)
-
@ambershark Thanks a lot, let me try it!
-
@ambershark Thanks a lot! it works!
have a great day/nigth I don't know where are you writing me!
you are so helpful! -
@ChristianMontero Night for me, and you're welcome, happy to help. :)
-
@ambershark hey one last thing :/ where could I read to understand the second option that you gave me, I mean to emit a different Clicked signal, because for the purpose of the application the next step it's to pass the text of each button to a string and I have to acces to every button as an element of an array, I hope not to bother you anymore, just need some reference, you think that the QT Documentation can help me on this, or would you recommend me somewhere else to read?
-
@ChristianMontero It's no bother ... give me a sec and I'll type up an example for you. I'm not sure of a good place to point you for docs on it though.
It's basically just deriving a class and setting your own signals.
-
Here ya go... there may be errors I didn't compile or anything.
MyButton.h file:
#pragma once #include <QPushButton> class MyButton : public QPushButton { Q_OBJECT public: MyButton(QWidget *parent = 0) : QWidget(parent), someBool_(false), someInt_(0) { connect(this, SIGNAL(clicked()), this, SLOT(handleClick())); } signals: void buttonClicked(bool, int); private slots: void handleClick() { emit buttonClicked(someBool_, someInt_); } private: bool someBool_; int someInt_; };
Some window.cpp somewhere:
// somewhere else ... i.e. Window // ... { connect(instanceOfMyButton, SIGNAL(buttonClicked(bool, int)), this, SLOT(myHandler(b } void Window::myHandler(bool b, int i) { // do whatever you want with b/i }
-
@ChristianMontero said in How to add one parameter to the clicked SIGNAL?:
@ambershark hey one last thing :/ where could I read to understand the second option that you gave me, I mean to emit a different Clicked signal, because for the purpose of the application the next step it's to pass the text of each button to a string and I have to acces to every button as an element of an array, I hope not to bother you anymore, just need some reference, you think that the QT Documentation can help me on this, or would you recommend me somewhere else to read?
If you explain this a bit more I can help you design it better perhaps.. I don't quite understand what you're trying to do. I wrote the example above to show you what I was talking about making a custom derived button, but it may not be a good solution for what you want. Or at least there may be a more elegant or easier one.
-
@ambershark Thank you very much, I just stared to use QT and I was having problems, but I hope i get use to it quickly and start helping people just like you, thanks you again! take care!
I just read your last comment, well I have to establish a connection between 2 devices, via UDP could be two computers, cellphones or microcontrollers, then in my GUI I need two sections, the first one is an array of labels, where i will show the data that i will recieve from de UDP connection, and the second section is an array of buttons, where the user will click to change the value of the bits, and then I'll have to pass the value of the buttons to the string which I will send via UDP as response to the information I recieved. And I have to use labels and buttons because they're requirements of the client.
I hope to be specific enough.
And thanks again.
-
I think QSignalMapper [1] was written for exactly this problem :)
-
@ambershark Thank you, you're so helpful :)
-
@ambershark said in How to add one parameter to the clicked SIGNAL?:
@aha_1980 That sure does do what he needs, lol. I didn't know about QSignalMapper.. It's funny that even after 16+ years of Qt there's still classes I don't know about. :)
I only knew it because I had the same requirement as @ChristianMontero some time ago :)
-
Hello @ChristianMontero hope you ok,
Just implement a QPushButton::clicked() slot that is connected to multiple push buttons. Inside that cast sender() to QPushButton* to access the button and identify it. You can then emit a user defined signal with additional parameters.
Another method would be using a C++11 lambda:
int counter = 1; // Create button or access it using the ui member QPushButton *button = new QPushButton; //In your case is m_button button->setProperty("myId", counter++); // in your case counter++ should be m_button[i] connect(button, &QPushButton::clicked, [this, button](){ //here you call your function pasing parameters slotButtonClicked(true, button->property("myId").toInt()); });
I took this from here https://www.codeproject.com/Questions/1257831/Qt-custom-slot-sending-an-integer