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

Problem with QCheckBox



  • Hi guys, i'm working in a project and i find a problem with the widget QCheckBox
    i want to setStyleSheet in the QCheckBox ( color ) and in the same time i want to allign my check in center, but the problem is i can't do this 2 options in the same time ?

      QCheckBox *check=new QCheckBox(this);	
      check->setStyleSheet("QWidget {background-color:#0000FF;color:white;}");
      check->setStyleSheet("margin-left:50%; margin-right:50%;}");
    

    this 2 option does not work together, i tried

         check->setStyleSheet("QWidget {background-color:#0000FF;color:white;margin-left:50%;margin-right:50%;}");
    

    and it didn't work also, can someone help me please ? thanks


  • Lifetime Qt Champion

    hi
    Seems to work fine for me with
    check->setStyleSheet("QWidget {background-color:#0000FF;color:white;margin-left:50%;margin-right:50%;}");

    alt text

    What are you seeing since you say its not working?



  • @mrjj the QCheckBox is not in the center, only the color work, this is my problem


  • Lifetime Qt Champion

    Hi
    but

    check->setStyleSheet("margin-left:50%; margin-right:50%;}");

    alone do net center checkmark anyway ?

    So is Question how to center checkbox with stylesheet ?



  • yeah, i tried to set only color alone , it works

    setStyleSheet("QWidget {background-color:#0000FF;color:white};
    

    i tried to set in the center alone , it works

    setStyleSheet("margin-left:50%; margin-right:50%;}");
    

    but when i tried the two options in the same time, only one works (the color) in your exemple
    So my is Question how to center checkbox with stylesheet



  • @Zunneh Hi,

    50%doesn't work in stylesheet (except in specific case) so it takes it as 50px.
    Note also that background is hidden by the margins.

    @mrjj You could upvote yourslef because you gave me the right way to look https://forum.qt.io/topic/90556/unexpected-behavior-using-percentage-in-stylesheet/2 :)



  • @Gojir4 so do you have solution for me ?



  • @Zunneh Actually the only solution I have in mind is wrapping combo box in a widget with blue background, and center the combo box inside the widget using spacer items


Log in to reply