Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Moved] How hard can it be to access a toolbar button and change it’s state via code?

    General and Desktop
    5
    6
    3633
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      DManel last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • T
        task_struct last edited by

        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

        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

        • Linu...
        1 Reply Last reply Reply Quote 0
        • D
          DManel last edited by

          Hi,

          No, I created and placed the buttons on the Designer (on the "mainWindow.ui" file by hand) and i setted some connections to work when clicked.

          But I want to make them checkable, in such a way that only the last one pressed remains checked...

          1 Reply Last reply Reply Quote 0
          • L
            lgeyer last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • M
              mlong last edited by

              Moved this thread to General and Desktop

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post