Results 1 to 7 of 7

Thread: How to fill QLabel Circle with color

  1. #1
    Join Date
    Jun 2006
    Location
    San Diego, USA
    Posts
    95
    Thanks
    9
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to fill QLabel Circle with color

    I am writing a Qt application, which has MainWindow->groupboxes->labels designed with designer.

    I used paintEvent for one of the groupBox to make one of the label as circle. I need to identify the color outside the paintEvent and fill that color to circle.

    Can you give me any idea, on how to proceed?

    Thanks & Regards,
    Arun

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to fill QLabel Circle with color

    I need to identify the color outside the paintEvent and fill that color to circle.
    Can u elaborate ? didnt get u

  3. #3
    Join Date
    Jun 2006
    Location
    San Diego, USA
    Posts
    95
    Thanks
    9
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to fill QLabel Circle with color

    Normally what we will do to fill the color in widget with paintEvent is by using setPen & setBrush, as shown below.

    Qt Code:
    1. void paintEvent(QPaintEvent *event)
    2. {
    3. QPainter p(this);
    4. p.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap));
    5. p.setBrush(QBrush(Qt::black, Qt::SolidPattern));
    6. p.drawEllipse(0, 0, width() - 1, height() - 1);
    7. p.end();
    8. QLabel::paintEvent(event);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Here in the above shown example I am filling circle with black color.

    But my problem is I have to fill the circle with random colors taken from some other functionality. I cannot pass that color into this paintEvent function. So I have to do some other things which fills color for the circle outside paintEvent functionality.

    So how to fill color for the circle ?
    Last edited by jpn; 7th January 2009 at 21:13. Reason: missing [code] tags

  4. #4
    Join Date
    Feb 2006
    Posts
    31

    Default Re: How to fill QLabel Circle with color

    You can have a QColor variable named m_color. Lets say if you have funcA() to set the m_color randomly, then you just need to call update() after that. The paintEvent() will get called and you can then use this m_color in your setPen()/setBrush().

  5. #5
    Join Date
    Jun 2006
    Location
    San Diego, USA
    Posts
    95
    Thanks
    9
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to fill QLabel Circle with color

    Ok Thanks for your reply. If I want to fill random colors with solidpattern for 3 circles
    how can I do that?

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to fill QLabel Circle with color

    Qt Code:
    1. QColor getRandomColor()
    2. {
    3. int r = rand() % 256;
    4. int g = rand() % 256;
    5. int b = rand() % 256;
    6. return QColor(r,g,b);
    7. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to fill QLabel Circle with color

    You can also use the palette for this. See QWidget::palette() and QWidget::setPalette(). Adding new variables storing colours is often unnecessary as there already is a container for colours in every widget.

Similar Threads

  1. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 23:25
  2. Change color of a link in QLabel using Style Sheets?
    By codeslicer in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2008, 12:00
  3. Replies: 9
    Last Post: 21st June 2007, 11:27
  4. How to fill the grid with the color?
    By merry in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2007, 12:10
  5. QLabel background color
    By munna in forum Newbie
    Replies: 3
    Last Post: 1st May 2006, 16:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.