[Moved] How hard can it be to access a toolbar button and change it’s state via code?
-
Hello everyone,
So, I have a toolbar on my small program, with some buttons that I placed by hand on the UI with the design tool.
I also created some triggering actions associated to them, so I can do other stuff by code.
Now, I want to make the toolbar buttons checkable. So, if I'm doing something related to that button, is stays pressed, and then when I select another button, another action is triggered, and I want the previous button goes unchecked. Basically, I want one checked button at a time on my toolbar.How can I do this on my code? How can I access to a specific toolbar button on my code and change its state to unchecked?
-
Hello,
do you use Qt Components? There are already components that can to this for you - "ButtonRow"http://doc.qt.nokia.com/qt-components-symbian/qml-buttonrow.html and "ButtonColumn":http://doc.qt.nokia.com/qt-components-symbian/qml-buttoncolumn.html
-
Well, I guess you have chosen the wrong forum then. You are in Qt Quick, discussing mainly QML and JavaScript.
As to your question: QAction has a checkable property, which makes the toolbutton checkable as well. If you want a set of acions to be exclusive add them to a QActionGroup.
-
If you created your UI using Designer, then you can manipute all widgets on there using the same way you setup the connections. [[doc:QToolButton]] is a [[doc:QAbstractButton]], which has a property called checked. It seems logical to me that calling:
@
m_ui->myToolButton->setChecked(false);
@
would make sure your toolbutton is unchecked.However, I would certainly considder abstracting your actions in QActions instead. You can add QActions to your toolbar, and then manipulate the action instead of the tool button directly. This way, it is easier to offer the same action at multiple places simultaniously, and keep their state in sync. I often even expose functionality in my internal API as QActions, so I can bundle the information about the state of the action (it's availability for instance) and the action itself into one neat class.