Search code examples
c++qtqwidgetqlineedit

qt change QLineEdit shape


I'm trying to create a custom shape QLineEdit with QWidget::setMask(). I redefined resizeEvent for my sub class lineEdit.

void MyLineEdit::resizeEvent(QResizeEvent *ev)
{
   QPixmap pixmap(":/new/prefix1/region.png");
   setFixedSize(ev->size());
   setMask(pixmap.mask());
   setStyleSheet("background-color : gray");
}

But the QlineEdit isn't showed. Btw, it was added to a QGridlayout and I checked that pixmap.isNull() == false and the size was normal. Did I miss something? Why isn't it displayed?


Solution

  • You don't have to subclass anything. Just use style sheets.

    editor->setStyleSheet("QLineEdit  {\n"
                          "    background: url(:/new/prefix1/region.png);\n"
                          "}");
    

    or based on documentation:

    editor->setStyleSheet("QLineEdit  {\n"
                          "    border-image: url(:/new/prefix1/region.png) 3 3 3 3;\n"
                          "}");