Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPalette doesn't apply correctly with QHBoxLayout
Qt 6.11 is out! See what's new in the release blog

QPalette doesn't apply correctly with QHBoxLayout

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 566 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vijaychsk
    wrote on last edited by vijaychsk
    #1

    Hi,
    I have a custom widget that gets added to my main window. This custom widget simply contains a QHBoxLayout with two radio buttons and QLineEdit. However, the color palette doesn't seem to be working as expected on this custom widget.

    Header:

    class myWidget : public QGroupBox {
      QHBoxLayout* _hbox = nullptr;
      QRadioButton* _rb1 = nullptr;
      QRadioButton* _rb2 = nullptr;
      QLineEdit* _lineEdit = nullptr;
    }
    

    Implementation:

       _hbox = new QHBoxLayout();
       _hbox->setContentsMargins(0, 0, 0, 0);
       _hbox->setSpacing(5);
       _rb1    = new QRadioButton("RB1", _parent);
       _rb1->setChecked(true);
       _rb2 = new QRadioButton("RB2", _parent);
       _lineEdit = new QLineEdit();
       _lineEdit->setText(tr("0"));
       _hbox->addWidget(_rb1);
       _hbox->addWidget(_rb2);
       _hbox->addWidget(_lineEdit);
       setLayout(_hbox);
    }
    

    The layout is perfect. Now, I'm trying to highlight/change the color on the lineEdit in case it is found empty. I tried to use QPalette, it is working but the color is getting applied to the entire HBox layout including the radio buttons. I only want the color for the QLineEdit which is what I specified in the code below. It is straight forward code:

    QPalette pal = _lineEdit->style()->standardPalette(); // also tried _lineEdit->palette();
    pal.setColor(QPalette::Base, Qt::red);
    setPalette(pal);
    

    What can possibly be wrong?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @vijaychsk said in QPalette doesn't apply correctly with QHBoxLayout:

      What can possibly be wrong?

      You set the palette to the whole widget, not only to the lineedit.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • V Offline
        V Offline
        vijaychsk
        wrote on last edited by
        #3

        @Christian-Ehrlicher Good catch. I missed it. Thank you.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved