[英]Qt Widget with QLineEdit-like background
我正在寻找一个自定义小部件,其中一部分是一个看起来像QLineEdit(或QProgressBar)的“背景”,
例如 但没有文字。
我想出了几种方法来实现此目的,但似乎都不是一个好的解决方案:
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);
这具有无法完全控制的缺点,尤其是在按我希望的布局内部放置时
2。
使用QLineEdit小部件,但将其设置为NoFocus和ReadOnly。
在我看来,这似乎太过分了,因为我永远都不需要任何文本功能
最好的解决方案是什么?
使用带有特殊stylesheet
QLabel
:
ui->label->setText("");
ui->label->setStyleSheet("QLabel{ border: 1px solid gray; background-color:white; border-radius:2px}");
样式表:
QLabel
{
border: 1px solid gray;
background-color:white;
border-radius:2px
}
QLabel
没有其他不必要的东西,所以它比QLineEdit
或QProgressBar
更好。
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.