PDA

View Full Version : Progress bar form QLineEdit issue



RafalNiewinski
29th May 2013, 19:09
Hello, It is my first post on the QtCentre forum so please correct any mistakes.

I have a problem with create something like QProgressBar from QLineEdit (QLineEdit functionality + QProgressBar functionality in one QWidget).

I created a class derived from QLineEdit which overloads paintEvent method.
Progress functionality works fine but I am lost QLineEdit functions such as carriage drawing, visibility of text selection etc.

My paintEvent method:

void MyLineEdit::paintEvent(QPaintEvent * event)
{
QPainter p(this);
QStyleOptionFrameV2 panel;
initStyleOption(&panel);
style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);

QPainter painter(this);
QStyleOptionFrameV2 lenap;
initStyleOption(&lenap);
QRect backgroundRect = style()->subElementRect(QStyle::SE_LineEditContents, &lenap, this);

if(!hasFocus() && progress < 100)
{
QColor loadingColor = QColor(116, 192, 250);
painter.setBrush(generateGradient(loadingColor));
painter.setPen(Qt::transparent);
int mid = backgroundRect.width() / 100 * progress;
QRect progressRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height());
painter.drawRect(progressRect);
}

painter.setPen(Qt::SolidLine);
painter.drawText(backgroundRect,Qt::AlignLeft|Qt:: AlignVCenter, this->text());
}

Lykurg
30th May 2013, 11:41
Haven't tried it but shouln't it be possible to simple set your background gradient into the palette of the line edit before calling the base class implementations QLineEdit::paint()?

RafalNiewinski
2nd June 2013, 10:19
Ok thanks

this is solved my problem:

void MyLineEdit::paintEvent(QPaintEvent * event)
{
QPainter p(this);
QStyleOptionFrameV2 panel;
initStyleOption(&panel);
style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this);

if(hasFocus() || progress == 100) QLineEdit::paintEvent(event);

QPainter painter(this);
QStyleOptionFrameV2 lenap;
initStyleOption(&lenap);
QRect backgroundRect = style()->subElementRect(QStyle::SE_LineEditContents, &lenap, this);

if(!hasFocus() && progress < 100)
{
QColor loadingColor = QColor(116, 192, 250);
painter.setBrush(generateGradient(loadingColor));
painter.setPen(Qt::transparent);
int mid = backgroundRect.width() / 100 * progress;
QRect progressRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height());
painter.drawRect(progressRect);

painter.setPen(Qt::SolidLine);
painter.drawText(backgroundRect,Qt::AlignLeft|Qt:: AlignVCenter, " " + this->text());
}
}