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. QCheckBox background
Forum Updated to NodeBB v4.3 + New Features

QCheckBox background

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.5k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    LairMan
    wrote on last edited by
    #1

    Hello,
    I have a problem to set the QCheckBox's background color. The following code sets the background correctly but only when mouse is over the checkbox:

    @QColor col(255, 0, 0);
    QPalette p = checkbox.palette();
    p.setColor(QPalette::Background, col);
    checkbox.setPalette(p);
    @

    which produces something like this:
    !http://www.imagecode.eu/checkbox.png!

    But how to set the background when the mouse is not over the checkbox? I'd like to avoid to write my own custom drawn checkbox. I'm sure it must be possible...

    Note that my application uses the QCleanLookStyle. When I use default style then the background doesn't work anyway (neither when the mouse is over nor the mouse is not over).

    Thanks in advance!

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi LairMan,
      Welcome to DevNet.
      You can use "qss":http://qt-project.org/doc/qt-5/stylesheet-examples.html for such things.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thEClaw
        wrote on last edited by
        #3

        Have you tried using a more explicit "setColor":http://qt-project.org/doc/qt-5/qpalette.html#setColor method? Maybe set your color for the active as well as the inactive state.

        If things don't work out, just use a stylesheet on that checkbox:
        @checkbox.setStyleSheet("QCheckBox { background: red; }"; //I didn't look up the exact property to set, but the documentation is quite extensive@

        1 Reply Last reply
        0
        • L Offline
          L Offline
          LairMan
          wrote on last edited by
          #4

          qxoz:
          using the stylesheet I get this result:
          !http://www.imagecode.eu/checkbox2.png!

          The square is colored too, but I don't like it, I'd like to keep it white (or some default color).

          thEClaw:
          I tried the active and inactive (and many others) but it not worked :-(

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thEClaw
            wrote on last edited by
            #5

            If the result of using the stylesheet is not ok, you'll have to read more of the documentation and change more than the background color. There is probably a sub-control for the little box, or maybe a special property.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qxoz
              wrote on last edited by
              #6

              [quote author="LairMan" date="1395315820"]qxoz:
              using the stylesheet I get this result:
              !http://www.imagecode.eu/checkbox2.png!
              [/quote]
              hm.. very strange.
              That what i got:
              !http://s10.postimg.org/jqn1m60fd/Untitled.png(example)!
              Probably it's because you use QCleanLookStyle.
              If yes and you can't use default style you can set indicator as image (look at "qss examples":http://qt-project.org/doc/qt-5/stylesheet-examples.html)

              1 Reply Last reply
              0
              • L Offline
                L Offline
                LairMan
                wrote on last edited by
                #7

                Ok, it seems that there is no easy way :). I'll set the indicator as image, it should work.
                Thanks to all.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  thEClaw
                  wrote on last edited by
                  #8

                  There should be an easy way, just take a look into the documentation:

                  QCheckBox:

                  bq. Supports the box model. The check indicator can be styled using the ::indicator subcontrol. By default, the indicator is placed in the Top Left corner of the Contents rectangle of the widget.

                  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