[英]Qt4: Decorating a QLineEdit (painting around it)
I am trying to "decorate" a QLineEdit
, or to be more accurate, to paint my own custom frame around it, to get the following result: 我试图“装饰”一个QLineEdit
,或更准确地说,是在它周围绘制我自己的自定义框架,以获得以下结果:
I tried to use Qt Style Sheets (CSS), but this will only enable trivial frame decorating (changing width/color/size etc...), nothing fancy like the above. 我尝试使用Qt样式表 (CSS),但这只会启用琐碎的框架装饰(更改宽度/颜色/大小等),没有像上面这样的幻想。
I also tried inheriting from QLineEdit
and overriding its void QLineEdit::paintEvent(QPaintEvent* e)
, but then I realized that reimplementing it means I will lose the QLineEdit
s "editness" (sorry for butchering the language here) - the textbox, the cursor, and the ability to insert text. 我还尝试了从QLineEdit
继承并覆盖其void QLineEdit::paintEvent(QPaintEvent* e)
,但是后来我意识到重新实现它意味着我将失去QLineEdit
的“编辑性”(抱歉,在此处使用这种语言)-文本框,光标,以及插入文本的功能。
How can I achieve the above text box? 如何实现上述文本框?
Is this a combination of QLabel
perfectly located behind a QLineEdit
? 这是QLabel
完美结合,位于QLineEdit
后面吗?
Try to use composition. 尝试使用合成物。 Create your own Widget inherited it from QWidget
, paint what you want in QWidget::paintEvent
and place QLineEdit
above it. 创建自己的Widget,它继承自QWidget
,在QWidget::paintEvent
绘制所需的内容,并将QLineEdit
放在其上方。 Probably you'll have to center it and use css
for QLineEdit
to make it look smooth. 可能您必须将其居中并使用css
进行QLineEdit
使其看起来平滑。
class MyWidget: public QWidget
{
explicit MyWidget(QWidget* parent = 0):
QWidget(parent),
line_edit(new QLineEdit(this))
{
// place line_edit in center of QWidget
}
private:
QLineEdit* line_edit;
}
Or you can override void QLineEdit::paintEvent(QPaintEvent* e)
like this 或者您可以像这样重写void QLineEdit::paintEvent(QPaintEvent* e)
void QLineEdit::paintEvent(QPaintEvent* e)
{
//paint your border
QLineEdit::paintEvent(e);
}
And you won't lose the QLineEdits
"editness". 而且您不会失去QLineEdits
“编辑性”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.