簡體   English   中英

Qt小部件具有類似QLineEdit的背景

[英]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沒有其他不必要的東西,所以它比QLineEditQProgressBar更好。

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM