c++ - Qt4 : Decorating a QLineEdit (painting around it)

标签 c++ qt qlineedit paintevent

我试图“装饰”一个QLineEdit,或者更准确地说,在它周围绘制我自己的自定义框架,以获得以下结果:

enter image description here

我尝试使用 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 : Decorating a QLineEdit (painting around it),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53956025/

相关文章:

c++ - 'cout' 语句未被执行

c++ - 有没有更好的方法来检查一个值是否大于 double 类型?

c++ - 如何使用 glDrawElementsInstanced + Texture Buffer 对象?

c++ - 使用 C++ 的 Web 服务

python - 在 UI 元素上使用 setattr()

python - PyQt 自动调整 qlineedit 字符间距

java - 转换类型 JNI JAVA

c++ - 使用 Qt Creator 在 CDB 中快速调试信息

c++ - Qt中如何在图片上显示文字?

python - 访问多个小部件