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. QIcon not actually generating variations for QToolButton?
QtWS25 Last Chance

QIcon not actually generating variations for QToolButton?

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 2.9k Views
  • 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 last edited by davecotter
    #1

    all my "icons" start life as QImage (possibly programmatically drawn), converted to QPixMap, then to QIcon.

    so i get my QIcon, and set it into my QToolButton with toolButtonP->setIcon(pix);

    says in the doc:

    The simplest use of QIcon is to create one from a QPixmap file or resource, and then use it, allowing Qt to work out all the required icon styles

    so i thought the "pushed down" state (drawn slightly darker) (as in when the user clicks on the button) would be generated on the fly for me.

    if that's not the case, i can easily generate a darker image, but then how do i programmatically add it into QIcon as the "down state" so it's used by QToolButton?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      davecotter
      wrote on last edited by
      #23

      filed this bug

      please vote for it / watch it, if you care. thanks.

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        You can add via
        https://doc.qt.io/qt-5/qicon.html#addPixmap
        for the various states.
        And yes normally it does generate for the other states when assigned in
        Designer so try add same pixmap to that role and see if it does darken it.

        D 1 Reply Last reply
        4
        • D Offline
          D Offline
          davecotter
          wrote on last edited by
          #3

          i've tried this:

          QIcon			qIcon;
          
          qIcon.addPixmap(pix, QIcon::Normal);
          
          pix.Lighten(-0.2f);	//	negative is "darken"
          // qIcon.addPixmap(pix, QIcon::Active);
          qIcon.addPixmap(pix, QIcon::Selected);
          
          toolButtonP->setIcon(qIcon);
          

          but the QToolButton still doesn't show the darkened version when the user clicks on the button. tried both Active and Selected.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            Are you sure these methods do in place modifications ? Aren't they returning a modified copy of the pixmap ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • D Offline
              D Offline
              davecotter
              wrote on last edited by
              #5

              i have ensured it's doing a deep copy, here is updated code:

              QIcon			qIcon;
              
              qIcon.addPixmap(pix, QIcon::Normal);
              
              CPixels			darkPix(pix);	//	deep copy, trust me
              
              darkPix.Lighten(-0.2f);	//	negative is "darken"
              qIcon.addPixmap(darkPix, QIcon::Active);
              //qIcon.addPixmap(darkPix, QIcon::Selected);
              
              toolButtonP->setIcon(qIcon);
              
              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                I'm not sure I am following you on that one. How did you ensure of the deep copy ?
                But you also didn't answer my question: do these two methods do in place modification ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply
                0
                • SGaistS SGaist

                  I'm not sure I am following you on that one. How did you ensure of the deep copy ?
                  But you also didn't answer my question: do these two methods do in place modification ?

                  D Offline
                  D Offline
                  davecotter
                  wrote on last edited by
                  #7

                  @sgaist cuz that's what my constructor does. it does this:

                  CPixels::CPixels(const CPixels& other)
                  {
                  	*this = other;
                  
                  	#if _QT_
                  	{
                  		QImage&		self(*this);
                  
                  		self = other.copy();
                  	}
                  	#endif
                  }
                  

                  if you're asking if "Lighten()" does in-place mod, then yes. what other method are you referring to?

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    Hi
                    You can add via
                    https://doc.qt.io/qt-5/qicon.html#addPixmap
                    for the various states.
                    And yes normally it does generate for the other states when assigned in
                    Designer so try add same pixmap to that role and see if it does darken it.

                    D Offline
                    D Offline
                    davecotter
                    wrote on last edited by
                    #8

                    @mrjj it's not assigned in Designer, it's assigned in code

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      davecotter
                      wrote on last edited by
                      #9

                      note that "state" doesn't seem to be different between "off" and "on", so what is its purpose?

                      image states

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #10

                        Hi
                        on/off is used for Checkable buttons.
                        pr default its shown via the frame when checked but could
                        also have another icon. like big fat checkmark :)

                        Adding
                        icon.addFile(QStringLiteral(":/test.svg"), QSize(), QIcon::Normal, QIcon::Off);
                        also generates the other icons as i can later paint with them.
                        (in paintevent)
                        icon().paint(&p,rect(),Qt::AlignCenter, QIcon::Mode::Disabled, QIcon::State::Off);

                        1 Reply Last reply
                        2
                        • D Offline
                          D Offline
                          davecotter
                          wrote on last edited by
                          #11

                          i'm using QToolButton, and QImages which have already been created. I'm not loading QIcons directly from files. How do i create a QIcon that i can then load into QToolButton, such that when the user clicks it, it shows a "darkened" or "pressed" image?

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            davecotter
                            wrote on last edited by davecotter
                            #12

                            does nobody else create QIcons programmatically, to be used in QToolButton?

                            ODБOïO 1 Reply Last reply
                            0
                            • D davecotter

                              does nobody else create QIcons programmatically, to be used in QToolButton?

                              ODБOïO Offline
                              ODБOïO Offline
                              ODБOï
                              wrote on last edited by ODБOï
                              #13

                              @davecotter

                              QImage img(":/Icon.png");
                              
                              auto toolButton = new QToolButton(toolBar);
                              toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
                              auto act = new QAction();
                              act->setIcon(QPixmap::fromImage(img));
                              act->setText("text");
                              toolButton->setDefaultAction(act);
                              
                              1 Reply Last reply
                              1
                              • D Offline
                                D Offline
                                davecotter
                                wrote on last edited by
                                #14

                                that code snip only adds in a single state (the up state). i have separate pngs for the down and disabled state that never get used. how do i cause my tool button to use those states?

                                kshegunovK 1 Reply Last reply
                                0
                                • D davecotter

                                  that code snip only adds in a single state (the up state). i have separate pngs for the down and disabled state that never get used. how do i cause my tool button to use those states?

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by
                                  #15

                                  I believe you're looking for this.

                                  Read and abide by the Qt Code of Conduct

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    davecotter
                                    wrote on last edited by
                                    #16

                                    @kshegunov that's literally the code i'm already using but it's not working. see the code snips at the start of this thread, am I doing it wrong?

                                    kshegunovK 1 Reply Last reply
                                    0
                                    • D davecotter

                                      @kshegunov that's literally the code i'm already using but it's not working. see the code snips at the start of this thread, am I doing it wrong?

                                      kshegunovK Offline
                                      kshegunovK Offline
                                      kshegunov
                                      Moderators
                                      wrote on last edited by kshegunov
                                      #17

                                      Yeah, sorry. I think it worked for a push button. could you try that? I don't recall currently if it's supposed to work with the toolbar button, I remember recently doing something akin but it was a table delegate so I was drawing it manually.

                                      EDIT:
                                      I think my brain had gone on a vacation ...
                                      What style are you using? Is it possible the style doesn't support that? I usually use Fusion (i.e. Qt drawing).

                                      Read and abide by the Qt Code of Conduct

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        davecotter
                                        wrote on last edited by
                                        #18

                                        not sure what you mean by "style", i'm just using Qt out of the box, i haven't specifically selected a "style".

                                        unfortunately the "QPushButton" does not draw the icon as "pressed" either, it just darkens the background which is not what i want:

                                        Screen Shot 2019-09-24 at 10.40.24 AM.png

                                        kshegunovK 1 Reply Last reply
                                        0
                                        • D davecotter

                                          not sure what you mean by "style", i'm just using Qt out of the box, i haven't specifically selected a "style".

                                          unfortunately the "QPushButton" does not draw the icon as "pressed" either, it just darkens the background which is not what i want:

                                          Screen Shot 2019-09-24 at 10.40.24 AM.png

                                          kshegunovK Offline
                                          kshegunovK Offline
                                          kshegunov
                                          Moderators
                                          wrote on last edited by
                                          #19

                                          not sure what you mean by "style", i'm just using Qt out of the box, i haven't specifically selected a "style".

                                          Judging by your screenshot you're using the native osx style.

                                          From what I can see here: https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html#1700
                                          The tool button only takes 3 icon states - active, disabled and normal. Selected isn't one of them. But that's for Fusion, I have no clue what goes in the native osx style. So first advice - switch to fusion and use Normal/Active to alternate between unpressed/pressed. If that's what you're expecting go back to the native style and see if the problem is there. If so I'd say it's either a bug or limitation of the native style's implementation.

                                          Read and abide by the Qt Code of Conduct

                                          1 Reply Last reply
                                          1
                                          • D Offline
                                            D Offline
                                            davecotter
                                            wrote on last edited by
                                            #20

                                            Fusion is un-mac-like, so i would not want to use it. strangely, "active" seems to mean "focused". when i click in the button it does turn color as expected but if i track the mouse (keep holding) while dragging OFF the button, it should re-draw in it's UP state, but it doesn't. when i let go it should do the same, but it STAYS looking pressed. if i then click the volume slider, the button THEN draws in the UP state. am i just not getting how this is designed to work?

                                            1 Reply Last reply
                                            0

                                            • Login

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