Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to create an icon button with separate visual up and down state?
Forum Updated to NodeBB v4.3 + New Features

how to create an icon button with separate visual up and down state?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.4k Views 3 Watching
  • 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 Offline
    D Offline
    davecotter
    wrote on 1 Oct 2019, 20:55 last edited by
    #1

    using Qt Widgets, C++ app

    I'd expect i can create a QIcon with up and down states (selected? active?), then set that into a push button or tool button, but no, that doesn't work. when the user clicks in the button, no separate image is drawn.

    how do i simply create a button that draws a different image while in its down state? is there sample code?

    i've googled A LOT and found nothing simple. looking at the paintEvent() of QToolButton, it calls into drawComplexControl() and imagining re-implementing all that makes my head spin.

    J 1 Reply Last reply 2 Oct 2019, 06:29
    0
    • D Offline
      D Offline
      davecotter
      wrote on 2 Oct 2019, 19:29 last edited by
      #7

      okay wow it wasn't THAT hard. I override QToolButton with this:

      void	QIconButton::paintEvent(QPaintEvent *e)
      {
      	QPainter	painter(this);
      	QIcon		icon(_inherited::icon());
      	QRect		frameR(QPoint(0, 0), iconSize());
      	QIcon::Mode	mode = QIcon::Normal;
      
      	if (isDown()) {
      		mode = QIcon::Active;
      	}
      
      	icon.paint(&painter, frameR, Qt::AlignCenter, mode);
      }
      
      1 Reply Last reply
      1
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 1 Oct 2019, 21:53 last edited by
        #2

        This is custom enough that I believe you would most likely need to subclass QAbstractButton and provide your own paint events...so "simply" is very relative in this case.

        1 Reply Last reply
        0
        • D davecotter
          1 Oct 2019, 20:55

          using Qt Widgets, C++ app

          I'd expect i can create a QIcon with up and down states (selected? active?), then set that into a push button or tool button, but no, that doesn't work. when the user clicks in the button, no separate image is drawn.

          how do i simply create a button that draws a different image while in its down state? is there sample code?

          i've googled A LOT and found nothing simple. looking at the paintEvent() of QToolButton, it calls into drawComplexControl() and imagining re-implementing all that makes my head spin.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 2 Oct 2019, 06:29 last edited by
          #3

          @davecotter You could set different icons in https://doc.qt.io/qt-5/qabstractbutton.html#pressed (down/pressed state) and https://doc.qt.io/qt-5/qabstractbutton.html#released (up/released state).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            davecotter
            wrote on 2 Oct 2019, 07:53 last edited by
            #4

            @jsulm how?

            P J 2 Replies Last reply 2 Oct 2019, 08:08
            0
            • D davecotter
              2 Oct 2019, 07:53

              @jsulm how?

              P Offline
              P Offline
              Pl45m4
              wrote on 2 Oct 2019, 08:08 last edited by
              #5

              @davecotter

              By using stylesheet.

              myButton->setStyleSheet("QPushButton#myButton:pressed { border-image: ..... }");
              // Same for any other state
              

              More examples here (https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qpushbutton)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              1
              • D davecotter
                2 Oct 2019, 07:53

                @jsulm how?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 2 Oct 2019, 08:24 last edited by
                #6

                @davecotter said in how to create an icon button with separate visual up and down state?:

                how?

                What about https://doc.qt.io/qt-5/qabstractbutton.html#icon-prop ?
                Or stylesheets as @Pl45m4 suggested

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • D Offline
                  D Offline
                  davecotter
                  wrote on 2 Oct 2019, 19:29 last edited by
                  #7

                  okay wow it wasn't THAT hard. I override QToolButton with this:

                  void	QIconButton::paintEvent(QPaintEvent *e)
                  {
                  	QPainter	painter(this);
                  	QIcon		icon(_inherited::icon());
                  	QRect		frameR(QPoint(0, 0), iconSize());
                  	QIcon::Mode	mode = QIcon::Normal;
                  
                  	if (isDown()) {
                  		mode = QIcon::Active;
                  	}
                  
                  	icon.paint(&painter, frameR, Qt::AlignCenter, mode);
                  }
                  
                  1 Reply Last reply
                  1

                  1/7

                  1 Oct 2019, 20:55

                  • Login

                  • Login or register to search.
                  1 out of 7
                  • First post
                    1/7
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved