Reputation: 1289
I'm looking to create a custom widget where part of it is a "background" that looks like a QLineEdit (or QProgressBar),
e.g. but without the text.
I've come up with a couple of hacky ways to do this, but neither of them seem like a good solution:
1.
QPainter painter(this);
int penwidth = painter.pen().width();
int width = this->width();
int height = this->height() - 20;
QPoint tl(penwidth / 2, penwidth / 2 + 10);
QPoint bl(penwidth / 2, height - penwidth);
QPoint tr(width - penwidth, penwidth / 2);
QPoint br(width - penwidth, height - penwidth);
QRect rect(tl, br);
QStyleOptionFocusRect option;
option.initFrom(this);
option.backgroundColor = palette().color(QPalette::Button);
option.rect = rect;
this->style()->drawControl(QStyle::CE_ProgressBarGroove, &option, &painter, this);
This has the disadvantage of not being fully controllable, especially when inside a layout as I intend it to be
2.
Using a QLineEdit widget but setting it to NoFocus and ReadOnly.
This seems like overkill to me, as I'll never want any of the text functionality
What is the best solution to this?
Upvotes: 0
Views: 342
Reputation: 18514
Use QLabel
with special stylesheet
:
ui->label->setText("");
ui->label->setStyleSheet("QLabel{ border: 1px solid gray; background-color:white; border-radius:2px}");
Stylesheet:
QLabel
{
border: 1px solid gray;
background-color:white;
border-radius:2px
}
QLabel
has no any other unnecessary things so it is better than QLineEdit
or QProgressBar
.
Result:
Upvotes: 3