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. Is it possible to draw a colored rectangle on a pushButton?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to draw a colored rectangle on a pushButton?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.7k 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.
  • P Offline
    P Offline
    Prmego
    wrote on last edited by
    #1

    I want after choosing a color from the QColorDialog put a rectangle (as a preview) on the PushButton and change its color to the color which I have chosen.

    The PushButton before choosing the color:

    alt text

    The PushButton after choosing the color:

    alt text

    Is it possible to draw a colored rectangle on a pushButton, and how can I do that (an example)?

    mrjjM 1 Reply Last reply
    0
    • P Prmego

      I want after choosing a color from the QColorDialog put a rectangle (as a preview) on the PushButton and change its color to the color which I have chosen.

      The PushButton before choosing the color:

      alt text

      The PushButton after choosing the color:

      alt text

      Is it possible to draw a colored rectangle on a pushButton, and how can I do that (an example)?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Yes its possible by just subclassing the button and make a new paintEvent function.
      However, what about the text.?
      Anyway a quick way is just keep a copy and reset it back if needed.

      Test project with subclass
      https://www.dropbox.com/s/0vv85gv1zan9xl4/mycolorbutt.zip?dl=0

      alt text

      void ColorButton::paintEvent(QPaintEvent* event) {
        QPushButton::paintEvent(event); // call base paint
        if (!text().isEmpty()) { return; } // if it still show text we dont paint color
        QPainter p(this);
        QBrush b(Color);
        p.setBrush(b);
        QRect colorRect(rect());
        int reduced = width() / 4;
        QMargins m(reduced, 2, reduced, 2) ;
        colorRect = colorRect.marginsRemoved(m);
        p.fillRect( colorRect, b );
      }
      
      
      P 1 Reply Last reply
      6
      • mrjjM mrjj

        Hi
        Yes its possible by just subclassing the button and make a new paintEvent function.
        However, what about the text.?
        Anyway a quick way is just keep a copy and reset it back if needed.

        Test project with subclass
        https://www.dropbox.com/s/0vv85gv1zan9xl4/mycolorbutt.zip?dl=0

        alt text

        void ColorButton::paintEvent(QPaintEvent* event) {
          QPushButton::paintEvent(event); // call base paint
          if (!text().isEmpty()) { return; } // if it still show text we dont paint color
          QPainter p(this);
          QBrush b(Color);
          p.setBrush(b);
          QRect colorRect(rect());
          int reduced = width() / 4;
          QMargins m(reduced, 2, reduced, 2) ;
          colorRect = colorRect.marginsRemoved(m);
          p.fillRect( colorRect, b );
        }
        
        
        P Offline
        P Offline
        Prmego
        wrote on last edited by
        #3

        @mrjj : Thank you for your answer.

        mrjjM 1 Reply Last reply
        1
        • P Prmego

          @mrjj : Thank you for your answer.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Prmego
          np. please mark as solved if possible.

          1 Reply Last reply
          1

          • Login

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