Qt Radio Button only call SLOT() when button is actually checked - c++

I've got a group of radio buttons. These are all connected to the same SLOT(updateImage()).
Here's how they are set up:
connect(m_radio1, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio2, SIGNAL(toggled(bool)), this, SLOT(updateImage());
connect(m_radio3, SIGNAL(toggled(bool)), this, SLOT(updateImage());
When changing the radio button from one to the next, this SLOT() is called twice, once for de-selecting the previously selected radio button, and once for selecting the clicked radio button.
I was wondering, is there a way to modify my SLOT() to only occur in one of these cases, when the clicked item becomes checked?
Thanks

You are asking to connect to the toggled signal - you are called whenever the object is toggled. What's surprising?
You could connect to the "clicked" signal which provides a "checked" argument you can test.
Btw; if using a modern Qt version, you should ditch the SIGNAL and SLOTS macros and instead use the new connect() syntax that's checked at compile time.

Since your signal have a bool parameter.
toggled(bool)
you could add that parameter to your SLOT method.Then check if the parameter is checked.

(As a complement: How to do so in Python?) Assume you have a radio button radio_button and you would like to connect it to slot _my_slot while passing args as the arguments, such that radio_button only send a signal once it is selected, then you need to add the following:
radio_button.clicked.connect(lambda: _my_slot(args))

Related

Qt combobox: Event when item is selected

i need to know what is the way to run a function when one item of the combobox is selected/clicked, i tried with the standar connect:
connect(ui->combobox,SIGNAL(clicked()),this,SLOT(clickedaction()));
... but it doesnt work.
I dont even need a custom action per element, just the same for all.
Thanks in advance.
There is no signal clicked() in QComboBox.
If you need to detect the item activation either by mouse click or enter pressed then use activated(int index).
connect(ui->combobox,SIGNAL(activated(int)),this,SLOT(clickedaction(int)));

Possible bug in QPushbutton?

I want to implement something similar to Ribbon UI found in MS Office 2007.
I am using QPushbutton's along with QStacked Widget. When a QPushbutton is pressed corresponding widget from Stacked Widget is displayed. I want to implement in such way that when a PushButton is pressed down we should not be able to press it again except if some other QPushButton is pressed.
So for the clicked() SLOT of my QPushButton I am calling this: button->setDown(true); in my 1st line.
According to the documentation:
If this property is true, the button is pressed down. The signals
pressed() and clicked() are not emitted if you set this property to
true.
So when I click it at run time the button is shown as pressed down. Thats good! However the SIGNAL's are still emitted i.e. pressed() & clicked() are emitted.
I have tried for the same property using different combinations of SIGNAL's & SLOT's. However its just the same. I am using Qt 4.8.1.
What is going wrong here?
Thank You.
If you want to set the button visual appearance to pressed you can use the setDown() function. The documentation isn't very clear but:
If this property is true, the button is pressed down. The signals pressed() and clicked() are not emitted if you set this property to true.
It doesn't mean that that signals won't be emitted while the button is pressed but that they won't be emitted when you call setDown() (after all the button is enabled and a 2nd click may simply toggle its state).
What you can do is to check if the button is pressed (isDown()) inside your handler for clicked(). As alternative you may change the button to be checkable (with setCheckable())): 2nd click on the button will "close" it (if it's the behavior you need).
set all the buttons to checkable and then read up on
http://qt-project.org/doc/qt-4.8/qbuttongroup.html

How can I check if QPushButton is clicked

I want to use an if-else statement to check if QPushButton is clicked or not.
How can I do this?.
The question does not make sense. Being clicked is not a state that you can check; clicking a button is an event. It is important to distinguish between states and events.
You handle a button click event by connecting a slot to the signal QAbstractButton::clicked().
Maybe you mean "How do I check if a button is down?". Being down is a state; you check that state using the method QAbstractButton::isDown().
QAbstractButton, QPushButton's parent class, has a checked property (setChecked/isChecked) if you're trying to determine if the button is depressed with the checkable property set to true.

Qt Designer, missing "go to slot" in context menu?

I've been watching a Qt tutorial series on YouTube, in which the author shows how to call a function, when a button is pressed. He right-clicked on the button in Qt Creator IDE and chose "Go to slot", from where he chose the signal which would fire the generated function. Since I am used to develop with Netbeans, I simply tried to follow his example using the embedded Qt Designer. Unfortunately, there is no "Go to slot..." entry when I right-click on my button or any widget. I could, of course, create a new slot for my main window and then connect the button's signal to it, but doing it with a single function just seems way more convenient and clean to me. Is there a way to fix is, or if not, at least a way to do with via code entirely, without having to add a new slot to the main window for every single button that servers a different purpose? Thanks for your help.
While I don't know why the QtDesigner in Netbeans doesn't provide this feature, I know what the feature does: It just creates a slot with a special name in your widget class. Note that it does not add a connect statement. It uses the automatic connection feature, which works like this:
For each slot which name matches this pattern:
void on_X_Y(...)
it will be connected to the signal named Y of the object named X. So if you have a QPushButton named button, and you want to handle the signal pressed, simply create a slot with the following signature:
void on_button_pressed()
If you wonder how this slot gets connected to the signal: This happens in the ui_...h file at the end of setupUi():
QMetaObject::connectSlotsByName(WidgetName);

Callback for button in Qt Designer?

I just started using QtCreator tonight, and it seems it puts all of the interface stuff inside of the ui file. I followed a tutorial to create a resource for my icons, then I added them to a menu bar at the top.
I need to make a connection when one of them is clicked though, and cannot figure out how to make a callback for it.
Am I going to have to completely create them through code or is there some way to add a callback for them (rather than just making them interact with other objects).
Menu bar items are action objects. To do something when they are clicked, you need to catch the triggered() signal from the action. Read more about signals and slots here.
To do this, you need to declare a new slot in your MainWindow class. Qt also supports doing this automatically, without the need to connect anything, but I prefer doing it myself. If you're not interested, just skip this part.
First, we declare a new slot in your window class:
private slots:
void clickMenuButton();
Then, in your constructor, you need to connect the triggered signal to your new slot:
connect(ui.actionObject, SIGNAL(triggered()), this, SLOT(clickMenuButton()));
The first argument is the object that holds the signal we'll listen to (your menu button). The second is the name of the signal. The third is the object that holds the receiving slot (in this case, our window). The fourth is the slot.
And just like that, clickMenuButton() will be called whenever the action is clicked.
As I said before, Qt can also automatically connect signals to slots. The disadvantage here seems to be that you can't change the slot's name, but you don't need to connect it either.
Qt Creator supports creation of slots for widgets: in the case of your menu action, you should go to the form designer, and you should see a list of actions in your form (if you don't, find the Action Editor). Right click the action you want, and push Go to slot.... There, double click triggered().
Qt Creator will then open the new slot in your code editor, and you can do whatever you want to here!
To do that you'll need to add a QAction, add it to the menu, associate an icon with it and then create a callback for it. I'm using the VS Integration so I don't know the details of how to do it in Creator but it should be possible without creating stuff in code.
There should be somewhere an actions editor. from there you add an action, then right-click it or something to add an icon to it, then drag it do the menu and then possibly double click it to create a slot for it. This is how it works in the VS Integration atleast.