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. QPushButton highlight.

QPushButton highlight.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpushbutton
11 Posts 3 Posters 10.0k Views 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #2

    Hi! You could create your own button class that inherits QPushButton and override the paintEvent like this:

    void MyButton::paintEvent(QPaintEvent *event)
    {
        const QColor backgroundColor = isDown() ? QColor("white") : QColor("grey");
    
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
    
        QPen pen(QColor("orange"));
        pen.setStyle(Qt::SolidLine);
        painter.setPen(pen);
    
        painter.setFont( QFont("Helvetica", 16) );
    
        const QRect rect( event->rect() );
    
        painter.fillRect( rect, QColor("black"));
        painter.fillRect( QRect(rect.left()+1,rect.top()+1,rect.width()-2, rect.height()-2), backgroundColor);
        painter.drawText( rect, Qt::AlignCenter, this->text() );
    }
    

    The result works just like any QPushButton but does no highlighting:

    kshegunovK 1 Reply Last reply
    1
    • K Offline
      K Offline
      Kaluss
      wrote on last edited by
      #3

      It works for me, but do You know maybe a way to achieve similar effect by setStyleSheet function?
      I ask, because I do not want so radically interfere to paint event.
      For example, In Your solution I have to also add some additional code with QFontMetrics to keep proper size of text on buttons.

      Best regards,
      Tomek

      1 Reply Last reply
      0
      • ? A Former User

        Hi! You could create your own button class that inherits QPushButton and override the paintEvent like this:

        void MyButton::paintEvent(QPaintEvent *event)
        {
            const QColor backgroundColor = isDown() ? QColor("white") : QColor("grey");
        
            QPainter painter(this);
            painter.setRenderHint(QPainter::Antialiasing);
        
            QPen pen(QColor("orange"));
            pen.setStyle(Qt::SolidLine);
            painter.setPen(pen);
        
            painter.setFont( QFont("Helvetica", 16) );
        
            const QRect rect( event->rect() );
        
            painter.fillRect( rect, QColor("black"));
            painter.fillRect( QRect(rect.left()+1,rect.top()+1,rect.width()-2, rect.height()-2), backgroundColor);
            painter.drawText( rect, Qt::AlignCenter, this->text() );
        }
        

        The result works just like any QPushButton but does no highlighting:

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

        @Wieland
        Isn't it simpler to actually delegate the painting? I.e:

        void MyButton::paintEvent(QPaintEvent *event)
        {
            if (isDown())
                setDown(false);
        
            QPushButton::paintEvent(event);
        }
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kaluss
          wrote on last edited by
          #5

          No, because button is not highlighted because is down, but because the mouse pointer is on it.

          kshegunovK 1 Reply Last reply
          0
          • K Kaluss

            No, because button is not highlighted because is down, but because the mouse pointer is on it.

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

            @Kaluss
            Hello Tomek,
            Then I don't understand how @Wieland's example will work for you. I have not in fact added anything to his suggestion, except for delegating the paint event to the base class ...

            Kind regards.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kaluss
              wrote on last edited by
              #7

              Because in Your example button will be highlighted anyway. Highlight it tuned on when mouse pointer enters on button area. Your example will work when button is pressed and doesn't change anything when pointer will enter button area.

              kshegunovK 1 Reply Last reply
              0
              • K Kaluss

                Because in Your example button will be highlighted anyway. Highlight it tuned on when mouse pointer enters on button area. Your example will work when button is pressed and doesn't change anything when pointer will enter button area.

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

                @Kaluss
                Ugh, sorry, of course you're right! I guess I'm not in shape today. I think there is a way to set it with the stylesheet (your original idea), but I've never worked with those. Maybe something like this:

                QPushButton#myButtonName:hover  {
                    background-color: ...
                }
                

                or:

                QPushButton:hover  {
                    background-color: ...
                }
                

                might work?

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kaluss
                  wrote on last edited by
                  #9

                  Could it be :) I will check when get back to home :)
                  setStyleSheet("QPushButton#myButtonName:hover { background-color: transparent}");
                  It will work? I ask because I never used this functionality...

                  kshegunovK 1 Reply Last reply
                  0
                  • K Kaluss

                    Could it be :) I will check when get back to home :)
                    setStyleSheet("QPushButton#myButtonName:hover { background-color: transparent}");
                    It will work? I ask because I never used this functionality...

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

                    @Kaluss
                    It should, I think. Take a peek at the stylesheet examples, I think it'll be more clear than me trying to explain something I've not used ... :)

                    Kind regards.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Kaluss
                      wrote on last edited by Kaluss
                      #11

                      I found solutinion:

                      void myButton::enterEvent( QEvent *event )
                      {
                              this->setAttribute(Qt::WA_UnderMouse, false)
                      }
                      
                      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