[Qt-interest] RES: RES: RES: How to draw circle on top of a label
Leonardo Bispo
leonardo.bispo at corp.orolix.com
Fri Dec 19 13:09:40 CET 2008
Hi,
Just in case,
This is the code using QRegion. This is very very simple, just to give you the idea how to do this. Can be improved!!
#include "widget.h"
class QCircleLabel : public QLabel
{
public:
QCircleLabel(QWidget *parent = 0, Qt::WindowFlags f = 0) :
QLabel(parent, f) {}
QCircleLabel(const QString &text, QWidget *parent = 0,
Qt::WindowFlags f = 0) : QLabel(text, parent, f) {}
~QCircleLabel() {}
void paintEvent(QPaintEvent *event)
{
QPainter p(this);
QPen pen;
pen.setWidth(3);
p.setPen(pen); // Trick to make the circle draw better!!!
p.drawEllipse(0, 0, width(), height());
p.end();
QLabel::paintEvent(event);
}
void resizeEvent(QResizeEvent *event)
{
QRegion region(rect(), QRegion::Ellipse);
setMask(region);
}
};
Widget::Widget(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
setStyleSheet("background-color: blue;");
circleLabel = new QCircleLabel(this);
circleLabel->setStyleSheet("background-color: yellow;");
circleLabel->setText(tr("This is a test"));
circleLabel->setGeometry(50, 50, 100, 100);
}
Widget::~Widget()
{
}
Best Regards,
Leonardo Bispo de Oliveira
Desenvolvimento de Sistemas - Analista Desenvolvedor
Tel: +55 11 5508 5887, +55 11 2122 0312
Fax: +55 11 5508 5880
http://www.orolix.com.br - leonardo.bispo at corp.orolix.com
Orolix. Você NAVEGA, a gente PAGA !!!
-----Mensagem original-----
De: qt-interest-bounces at trolltech.com em nome de Leonardo Bispo
Enviada: sex 19/12/2008 09:57
Para: ARUN_KUMAR at denso-diam.com; Qt Interest
Cc: qt-interest-bounces at trolltech.com
Assunto: [Qt-interest] RES: RES: How to draw circle on top of a label
Hi,
This is just a simple example, That will just paint a circle in a rectangle label. If you want a circle widget, than you will need also do some tricks using:
void setMask(const QRegion®ion)
/////////////////////////////////////////////
// widget.h
/////////////////////////////////////////////
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0, Qt::WFlags flags = 0);
~Widget();
private:
QLabel *circleLabel;
};
#endif
/////////////////////////////////////////////
// widget.cpp
/////////////////////////////////////////////
#include "widget.h"
class QCircleLabel : public QLabel
{
public:
QCircleLabel(QWidget *parent = 0, Qt::WindowFlags f = 0) :
QLabel(parent, f) {}
QCircleLabel(const QString &text, QWidget *parent = 0,
Qt::WindowFlags f = 0) : QLabel(text, parent, f) {}
~QCircleLabel() {}
void paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.drawEllipse(0, 0, width() - 1, height() - 1);
p.end();
QLabel::paintEvent(event);
}
};
Widget::Widget(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
circleLabel = new QCircleLabel(this);
circleLabel->setText(tr("This is a test"));
circleLabel->setGeometry(50, 50, 100, 100);
}
Widget::~Widget()
{
}
Best Regards,
Leonardo Bispo de Oliveira
Desenvolvimento de Sistemas - Analista Desenvolvedor
Tel: +55 11 5508 5887, +55 11 2122 0312
Fax: +55 11 5508 5880
http://www.orolix.com.br - leonardo.bispo at corp.orolix.com
Orolix. Você NAVEGA, a gente PAGA !!!
-----Mensagem original-----
De: ARUN_KUMAR at denso-diam.com [mailto:ARUN_KUMAR at denso-diam.com]
Enviada: qui 18/12/2008 22:09
Para: Leonardo Bispo; Qt Interest
Cc: qt-interest-bounces at trolltech.com
Assunto: Re: RES: [Qt-interest] How to draw circle on top of a label
Hi Leonardo Bispo / Qt-Team
Thanks for your reply.
I don't know how to use painter function to paint on top of QLabel.
My scenario here is I created a MainWindow and on top of it created
stacked widgets using designer.
Labels have been placed on these Stacked widgets. Now i have to draw
circle in these labels.
So now with the above scenario, how to use drawEllipse for the label.
Thanks & Regards,
Arun
"Leonardo Bispo" <leonardo.bispo at corp.orolix.com>
12/18/2008 11:48 AM
To
<ARUN_KUMAR at denso-diam.com>, "Qt Interest" <qt-interest at trolltech.com>,
<qt-interest-bounces at trolltech.com>
cc
Subject
RES: [Qt-interest] How to draw circle on top of a label
Hi,
I don't know if it is what you want, but lets go:
QLabel is a QWidget and QWidget is a QPaintDevice, so you can use
a QPainter to draw in a QLabel.
QPainter has a method called
void QPainter::drawEllipse ( const QRectF & rectangle )
that you can use to draw a circle.
But if you want to make your widget a circle, you must need create a
QRegion and then set the mask of QLabel.
QRegion has methods to make ellipse region, so this will be what you need.
Best Regards,
Leonardo Bispo de Oliveira
Desenvolvimento de Sistemas - Analista Desenvolvedor
Tel: +55 11 5508 5887, +55 11 2122 0312
Fax: +55 11 5508 5880
http://www.orolix.com.br - leonardo.bispo at corp.orolix.com
Orolix. Você NAVEGA, a gente PAGA !!!
-----Mensagem original-----
De: qt-interest-bounces at trolltech.com em nome de ARUN_KUMAR at denso-diam.com
Enviada: qui 18/12/2008 17:35
Para: Qt Interest; qt-interest-bounces at trolltech.com
Assunto: [Qt-interest] How to draw circle on top of a label
Hi all,
I am new to Qt?
Where I can view these postings on web? if i see under qt-interest mailing
list it is showing till october'2008.
How to draw circle on top of a label widget? Label widget is on
MainWindow. MainWindow is the central widget here.
Can you please give some code snippet,
Thanks & Regards,
Arun.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20081219/590a238d/attachment.html
More information about the Qt-interest-old
mailing list