Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-83961

QSpinBox cannot set its QLineEdit colours

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.12.2
    • None
    • Linux with Qt 5.12.2
      Windows with Qt 5.14.x
      Believed to apply to all platforms/versions.
    • Linux/X11, Windows

    Description

      Summary:

      You cannot set background/foreground colour on the QLineEdit of a QSpinBox, if any ancestor has its  background/foreground colour set.  This may apply to other attributes, not tested.

      I have seen this under Linux with Qt 5.12.2.  Someone else has confirmed it is the case under Windows with Qt 5.14.x, so believed to apply to all platforms/versions.

       

      QSpinBox spin = new QSpinBox(whatever);
      QLineEdit lineEdit = spin->findChild<QLineEdit*>();
      Q_ASSERT(lineEdit);
      lineEdit->setStyleSheet("background-color: blue;");

       

      has no effect.

      Example:

       

      #include <QApplication>
      #include <QLineEdit>
      #include <QSpinBox>
      #include <QVBoxLayout>
      #include <QWidget>
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QWidget w;
          QVBoxLayout layout;
          w.setGeometry(100, 100, 200, 200);
          w.setLayout(&layout);
          // commenting in/out next line makes `QLineEdit` in `QSpinBox` ignore/respect its own stylesheet colors
          // yet standalone `QLineEdit` respects its own stylesheet colors in either case
          w.setStyleSheet("background-color: yellow; color: green;");
          QLineEdit lineEdit;
          layout.addWidget(&lineEdit);
          lineEdit.setText("99999");
          // standalone `QLineEdit` always respects stylesheet, and behaves correctly
          lineEdit.setStyleSheet("background-color: cyan; color: red;");
          QSpinBox spinBox;
          layout.addWidget(&spinBox);
          QLineEdit *spinLineEdit = spinBox.findChild<QLineEdit *>();
          spinBox.setMaximum(99999);
          spinBox.setValue(99999);
          // `QLineEdit` in `QSpinBox` respects stylesheet, and behaves correctly, *ONLY IF ANCESTOR `QWidget` DOES NOT SPECIFY COLORS*
          // if parent `QWidget` does specify colors, next line has no effect on it!
          spinLineEdit->setStyleSheet("background-color: cyan; color: red;");
          w.show();
          return a.exec();
      }
       
      

      Comments describe behaviour.

      With parent/ancestor w.setStyleSheet("background-color: yellow; color: green;"); line enabled, QSpinBox alone goes wrong while comparison QLineEdit works correctly, screenshot:

      With parent/ancestor // w.setStyleSheet("background-color: yellow; color: green;"); line commented out, both behave correctly, screenshot:

      [And btw if I set the stylesheet on the {{QSpinBox}} itself rather than its embedded line edit then that does work regardless of ancestor widget colours, but that is not what I want. It is an issue on the {{QLineEdit}} embedded in a {{QSpinBox}} only.]

      Subclassing and passing your own QLineEdit to QAbstractSpinBox::setLineEdit() does not alter the behaviour.

      I am looking for any suggested workaround right now if possible?

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            jonbrave J Barchan
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes