QToolButton and emiting signals - c++

What signal is emmitted if I click on QToolButton's arrow part of a button? The clicked signal is emitted wherever I click on it but I need to detect only when the specified part is clicked on.

What you have to do is to intercept menu's aboutToShow signal and connect this signal with id est, reinitialize_menu_ slot, and in this slot you can do necessary checks and adjustments.
Good luck.

Related

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

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

Qt unwanted double execution of a pushButton pressed signal

I created with Qt Creator 3.3.1 in design mode a pushButton and with the designer I connect the button with the signal pressed(). It work fine but sometimes and when I set in the pressed event a pushButton to hide or show or change the index of a stacketWidget the signal pressed() is repeated twice consecutively. I don't have connect manually in the code, but it's all done automatically by the designer. I'm using Qt 4.8.6 embedded. Thanks
void myclass::on_pushButton_1_pressed()
{
qDebug("Pressed event");
ui->pushButton_2->hide(); //if I comment this line the pressed signal is not repeated twice
}
Qt designer forms have a feature called autoconnect. It automatically connects signals of your form widgets, if there is a slot called on_{ObjectName}_{SignalName}.
So your slot is connected twice, once by your connection in the designer, and once by auto-connect.
Either remove your connection in designer, or rename your slot to resolve the additional call

Change or clear focus on QLineEdit when done editing by pressing Enter and only send one signal in Qt

I want to create a behavior in which when I finished editing a text, the focus either moves to the next child or clear the focus on the current field when I pressed "Enter". I would then get one signal to trigger a part of the code.
When I do editingFinished() and reimplement keyReleaseEvent() for "Enter" with focusNextChild() or clearFocus(), I get two signals for the text edit, one when the "Enter" is pressed, and one when the focus is change via focusNextChild() or clearFocus(). The extra signal is undesirable.
If I do returnPressed() and reimplement keyReleaseEvent() for "Enter" with focusNextChild() or clearFocus(), it would only create one signal, but I also want the signal to be created when the user exits the lineEdit via tab() or mouse click on any other line items, which would not be the case if I use returnPressed()
Ideally, I could use a signal when a lineEdit loses focus, its connected to a slot that runs the code and I can reimplement the keyReleaseEvent() for "Enter" to set the focus, but it doesn't seem like such a signal exists in Qt.
Does anyone have recommendations on how to implement this or a better way to approach it?
You can connect/disconnect signals when you want. For example, when you focus in the QLineEdit you can connect both listed signals to slot which will disconnect those two connections and move focus to the next child. Thus, you will have only one signal processed.
Another solution - you can call QLineEdit.blockSignals(true) in the beginning of the slot and call QLineEdit.blockSignals(false) in the end of the slot. In this case whatever you do in the slot you will not trigger any signal in QLineEdit and no duplicates will arise.

What is the difference between QCheckBox::toggled() and QCheckBox::clicked()?

Is there any practical difference between the QCheckBox::toggled(bool) and the QCheckBox::clicked(bool) signals? Both have the same signature, does it matter to which one I connect?
The toggled signal is emitted every time the check state of the checkbox changes, even if it changes through code, while the clicked signal is emitted only when the user interacts with the checkbox, eg:
ui->checkbox->setChecked(true); // toggled() will be emitted, but not clicked()
QCheckBox Inherit both toggled and clicked.
void QAbstractButton::clicked ( bool checked = false ) [signal]
This signal is emitted when the button is activated (i.e. pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click() or animateClick() is called. Notably, this signal is not emitted if you call setDown(), setChecked() or toggle().
If the button is checkable, checked is true if the button is checked, or false if the button is unchecked.
void QAbstractButton::toggled ( bool checked ) [signal]
This signal is emitted whenever a checkable button changes its state. checked is true if the button is checked, or false if the button is unchecked.
This may be the result of a user action, click() slot activation, or because setChecked() was called.
The states of buttons in exclusive button groups are updated before this signal is emitted. This means that slots can act on either the "off" signal or the "on" signal emitted by the buttons in the group whose states have changed.
For example, a slot that reacts to signals emitted by newly checked buttons but which ignores signals from buttons that have been unchecked can be implemented using the following pattern:
void MyWidget::reactToToggle(bool checked)
{
if (checked) {
// Examine the new button states.
...
}
}
http://qt-project.org/doc/qt-4.8/qcheckbox.html
QCheckBox::toggled(bool)
Emitted when the check box changes its state, whether that's through clicking it or using setChecked or toggle, etc.
QCheckBox::clicked(bool)
Emitted when the check box is clicked. That is, when the user clicks and releases on check box. Also occurs when the shortcut key is typed or click is used. Check box doesn't necessarily have to be checkable for this to be emitted.
If you're listening for when the state of the check box is changing, use toggled. If you're listening for when the user clicks the check box, regardless of whether that changes state or not, use clicked.

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