I want to paint a circle with user provided color and keep a line edit adjustment on it on horizontal alignment.
Used painter function call on slot, but its not working
#include <QPainter>
#include "cascadeColorHighlightWidget.h"
CascadeColorHighlightWidget::CascadeColorHighlightWidget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::Widget);
setAttribute( Qt::WA_DeleteOnClose, true );
setFixedSize(187,164);
setContentsMargins(0,0,0,0);
}
void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
painter.setPen(QPen(QColor(176, 176, 176),1));
painter.setBrush(QColor(255,255,255));
painter.drawRect(contRect);
painter.setPen(QPen(QColor(51,51,51),1));
QFont font( "Calibri" );
font.setPixelSize(14);
painter.setFont( font );
painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");
}
void CascadeColorHighlightWidget::focusOutEvent(QFocusEvent *event)
{
Q_UNUSED(event);
close();
}
void CascadeColorHighlightWidget::setColors(QColor color)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
int rectYPos = contRect.y() + 55;
painter.setPen(Qt::NoPen);
QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);
painter.setPen(Qt::NoPen);
painter.setBrush(color);
painter.drawEllipse(ellipseRect);
/*After this ellipse I need to draw a line edit where user can edit anytime*/
}
But by calling setcolot its not drawing the ellipse on the widget. Only the items in paintEvent worked.
Is it possible to do with painter or I need to keep widgetItems and insert in this wideget. please give some suggestions
All painting work should happen in paintEvent
. You have to keep state, and paint items accordingly. Have methods that take QPainter
as an argument and call them from within paintEvent method, passing to them the QPainter
object you created there.
Example:
In your widget header have:
private:
void setColors(QColor c) { color = c; }
void drawEllipse(QPainter & painter);
QColor color;
bool draw_ellipse;
As you can see, the setColors method only sets a color and you keep that color in a private instance variable color
.
A new method hosts the painting job (previously in setColors):
void CascadeColorHighlightWidget::drawEllipse(QPainter &painter)
{
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
int rectYPos = contRect.y() + 55;
painter.setPen(Qt::NoPen);
QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);
painter.setPen(Qt::NoPen);
painter.setBrush(color);
painter.drawEllipse(ellipseRect);
/*After this ellipse I need to draw a line edit where user can edit anytime*/
}
The variable color
in this line
painter.setBrush(color);
is the one you set using the setColors
method.
The paintEvent
method should be like:
void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
painter.setPen(QPen(QColor(176, 176, 176),1));
painter.setBrush(QColor(255,255,255));
painter.drawRect(contRect);
painter.setPen(QPen(QColor(51,51,51),1));
QFont font( "Calibri" );
font.setPixelSize(14);
painter.setFont( font );
painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");
if(draw_ellipse)
{
drawEllipse(painter);
}
}
At the end of it, you test draw_ellipse
(don't forget to initialize it to false
in the constructor) and call the drawEllipse
method if it's true
.
Let's draw the ellipse, for example using QWidget
's mousePressEvent
:
void CascadeColorHighlightWidget::mousePressEvent(QMouseEvent *event)
{
setColors(QColor(Qt::red));
draw_ellipse = true;
update();
}
Here, you set a color, first, then set draw_ellipse
to true
, then (and it matters a lot) you call the update slot of QWidget:
[...] it schedules a paint event for processing when Qt returns to the main event loop.
So the paintEvent method will be called, and your paintings updated accordingly to your class's state (color
and draw_ellipse
variables).