PDA

View Full Version : How to fill QLabel Circle with color



arunvv
7th January 2009, 01:21
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

aamer4yu
7th January 2009, 07:40
I need to identify the color outside the paintEvent and fill that color to circle.


Can u elaborate ? didnt get u

arunvv
7th January 2009, 17:24
Normally what we will do to fill the color in widget with paintEvent is by using setPen & setBrush, as shown below.


void paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap));
p.setBrush(QBrush(Qt::black, Qt::SolidPattern));
p.drawEllipse(0, 0, width() - 1, height() - 1);
p.end();
QLabel::paintEvent(event);
}
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 ?

oob2
7th January 2009, 18:13
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().

arunvv
7th January 2009, 18:55
Ok Thanks for your reply. If I want to fill random colors with solidpattern for 3 circles
how can I do that?

aamer4yu
8th January 2009, 04:23
QColor getRandomColor()
{
int r = rand() % 256;
int g = rand() % 256;
int b = rand() % 256;
return QColor(r,g,b);
}

wysota
8th January 2009, 08:46
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.