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.
Forum Updated to NodeBB v4.3 + New Features

QPushButton highlight.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpushbutton
11 Posts 3 Posters 9.3k 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.
  • K Offline
    K Offline
    Kaluss
    wrote on last edited by
    #1

    Hi,
    I got problem on windows with my virtual keyboard. It's highlighting of buttons when mouse is on it.
    Example picture:
    Example

    I already tried:
    1)Override onEnter handler with ignore event.
    2)setAutoDefault(false) and setDefault(false)
    3)QPushButton::setFocusPolicy(Qt::NoFocus )
    4)QPushButton::setCheckable(false)

    Nothing works for me.
    Anybody know what I should do?

    Best regards,
    Tomek

    1 Reply Last reply
    0
    • ? 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