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
Related
I am using QT 4.8 and C++ and I need a default push button which will be pressed when some text are input in a lineEdit and ENTER key is pressed.
According to the QT document for the property "default" of pushbutton.
the behavior is only for dialogs.
"The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus."
How can I have this behavior for a widget, which is a container for all my GUI components.
Thanks.
You can connect the signal QLineEdit::returnPressed() to the slot QAbstractButton::click().
Method 1
from the Designer you simply can:
Right-click on lineEdit
Select "Go to slot..."
Choose returnPressed():
Press ok
In the code view you will see the slot connected to the chosen signal
You can simply call the click() slot of a button you need:
Method 2
You can manually connect signal of any lineEdit to a slot of any pushButton:
In Qt 4 syntax:
connect(ui->lineEdit, SIGNAL(returnPressed()),
ui->anyButton, SLOT(click()));
In new Qt 5 syntax:
connect(ui->lineEdit, &QLineEdit::returnPressed,
ui->anyButton, &QPushButton::click);
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))
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.
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.
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.