我试图“装饰”一个QLineEdit
,或更准确地说,在其周围绘制自己的自定义框架,以得到以下结果:
我尝试使用Qt Style Sheets(CSS),但这只会启用琐碎的框架装饰(更改宽度/颜色/大小等),没有像上面这样的幻想。
我还尝试了从QLineEdit
继承并覆盖其void QLineEdit::paintEvent(QPaintEvent* e)
,但是后来我意识到重新实现它意味着我将失去QLineEdit
的“编辑性”(很抱歉,此处不提供该语言)-文本框,光标和插入文字的能力。
如何实现上述文本框?
这是QLabel
完美地位于QLineEdit
后面的组合吗?
最佳答案
尝试使用合成物。创建自己的Widget,继承自QWidget
,在QWidget::paintEvent
中绘制所需内容,然后在其上方放置QLineEdit
。可能您必须将其居中并为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;
}
或者您可以像这样覆盖
void QLineEdit::paintEvent(QPaintEvent* e)
void QLineEdit::paintEvent(QPaintEvent* e)
{
//paint your border
QLineEdit::paintEvent(e);
}
而且您不会丢失
QLineEdits
“编辑”。
关于c++ - Qt4:装饰QLineEdit(在其周围绘画),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53956025/