Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Styling QComboBox's lineedit



  • I have a QComboBox subclass where custom lineedit is used like this

    QLineEdit *lineEdit = new QLineEdit(this);
    lineEdit->setReadOnly(true);
    setLineEdit(lineEdit);
    

    As result combobox looks like editable combobox
    custom combobox
    Is it possible to style lineedit or my custom combobox as whole so it looks like standard QComboBox?
    standard combobox


  • Lifetime Qt Champion

    hi

    alt text

    alt text

    But why you set a read-only QLineEdit when QComboxBox allows this with the "Editable" property?

    Or you have other reason besides being readOnly ?



  • @mrjj Thanks for fixing images.

    I want to style lineedit (second image in your post) to get same look and feel as standard QCombobox provides (first image in your post). Is this possible?


  • Lifetime Qt Champion

    @voltron
    Hi
    Are you sure you need that?
    You assign a new lineEdit to set it to read-only.

    You can actually do that without setting it manually

    alt text

    In any case to style pic 2 to look like pic 1
    is not super trivial as you have to recreate the dropdown arrow as it reset as soon as you apply stylesheet.
    So you would have to use image for that to make it look right.

    alt text

    https://doc.qt.io/archives/qt-4.8/stylesheet-examples.html#customizing-qcombobox



  • Are you sure you need that?

    Absolutely. I know why and what I do here, if using standard combobox was possible I definitely would use it.

    Probably my question was formulated not very clear. When using CSS I can not maintain style changes like Fusion, Windows... and can not easily support themes. Also this way 3D bevel and shadow effects are not available.

    BTW, to apply color there is much simpler way

    QPalette palette = qApp->palette();
    palette.setBrush(QPalette.Base, palette.button());
    lineEdit->setPalette(palette);
    

    The only missed bit is a pseudo 3D appearance.


  • Lifetime Qt Champion

    Hi
    Ok just asking as making the editing part of a combobox read-only is directly supported.

    I understand the issue using stylesheets since its then not following the platform look.

    Im not sure how one would style it to look like the
    normal combo with noedit.

    I think with some fiddling you can use a ProxyStyle
    to only paint the text on top but since you dont want the
    look from having "editable" disabled im not sure what you are after.

    On windows, this looks like the NoEdit version

     {
            QLineEdit *lineEdit = new QLineEdit(this);
            lineEdit->setReadOnly(true);
            lineEdit->setStyleSheet("background-color:transparent");
            setLineEdit(lineEdit);
        }
    

    But I guess on your platform, it still dont look right.


Log in to reply