简体   繁体   中英

Qt Widget with QLineEdit-like background

I'm looking to create a custom widget where part of it is a "background" that looks like a QLineEdit (or QProgressBar),

eg 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?

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM