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. How to prevent checkbox text from overlapping when multiple checkboxes in QHBoxLayout?
Forum Updated to NodeBB v4.3 + New Features

How to prevent checkbox text from overlapping when multiple checkboxes in QHBoxLayout?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 298 Views 2 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.
  • R Offline
    R Offline
    RickyRister
    wrote on 5 Feb 2025, 09:07 last edited by RickyRister 2 May 2025, 09:08
    #1

    In the minimal reproducible example below, the text of the checkbox is overlapping with the next checkbox over. Is there a way to prevent the text from overlapping?

    The checkbox row is an QHBoxLayout that contains three QCheckBox plus a stretch . I want the checkboxes to be left-aligned in the layout, so I added a stretch to the right side. However, that seems to cause the checkbox text to overlap with the next checkbox over. Increasing the spacing or the content margins does not fix the issue.

    Screenshot 2025-02-05 at 12.43.57 AM.png

    Here is the code

    QVBoxLayout layout = QVBoxLayout(this);
    layout.setSpacing(0);
    layout.setContentsMargins(9, 0, 9, 5);
    setLayout(&layout);
    
    QHBoxLayout row1Layout = QHBoxLayout(this);
    row1Layout.setSpacing(3);
    row1Layout.setContentsMargins(9, 0, 9, 0);
    
    QLabel testLabel = QLabel("this a QLabel on row 1 that is longer than the next row ==============");
    row1Layout.addWidget(&testLabel);
    
    layout.addLayout(&row1Layout);
    
    QHBoxLayout row2Layout = QHBoxLayout(this);
    row2Layout.setContentsMargins(9, 0, 9, 0);
    
    QCheckBox checkbox1 = QCheckBox(this);
    QCheckBox checkbox2 = QCheckBox(this);
    QCheckBox checkbox3 = QCheckBox(this);
    
    checkbox1.setText(tr("checkbox 1"));
    checkbox2.setText(tr("checkbox 2 + text"));
    checkbox3.setText(tr("checkbox 3"));
    
    row2Layout.addWidget(&checkbox1);
    row2Layout.addWidget(&checkbox2);
    row2Layout.addWidget(&checkbox3);
    row2Layout.addStretch();
    
    layout.addLayout(&row2Layout);
    
    QDialog *dlg = new QDialog(this);
    dlg->setLayout(&layout);
    dlg->exec();
    
    P 1 Reply Last reply 5 Feb 2025, 12:27
    0
    • R RickyRister
      5 Feb 2025, 09:07

      In the minimal reproducible example below, the text of the checkbox is overlapping with the next checkbox over. Is there a way to prevent the text from overlapping?

      The checkbox row is an QHBoxLayout that contains three QCheckBox plus a stretch . I want the checkboxes to be left-aligned in the layout, so I added a stretch to the right side. However, that seems to cause the checkbox text to overlap with the next checkbox over. Increasing the spacing or the content margins does not fix the issue.

      Screenshot 2025-02-05 at 12.43.57 AM.png

      Here is the code

      QVBoxLayout layout = QVBoxLayout(this);
      layout.setSpacing(0);
      layout.setContentsMargins(9, 0, 9, 5);
      setLayout(&layout);
      
      QHBoxLayout row1Layout = QHBoxLayout(this);
      row1Layout.setSpacing(3);
      row1Layout.setContentsMargins(9, 0, 9, 0);
      
      QLabel testLabel = QLabel("this a QLabel on row 1 that is longer than the next row ==============");
      row1Layout.addWidget(&testLabel);
      
      layout.addLayout(&row1Layout);
      
      QHBoxLayout row2Layout = QHBoxLayout(this);
      row2Layout.setContentsMargins(9, 0, 9, 0);
      
      QCheckBox checkbox1 = QCheckBox(this);
      QCheckBox checkbox2 = QCheckBox(this);
      QCheckBox checkbox3 = QCheckBox(this);
      
      checkbox1.setText(tr("checkbox 1"));
      checkbox2.setText(tr("checkbox 2 + text"));
      checkbox3.setText(tr("checkbox 3"));
      
      row2Layout.addWidget(&checkbox1);
      row2Layout.addWidget(&checkbox2);
      row2Layout.addWidget(&checkbox3);
      row2Layout.addStretch();
      
      layout.addLayout(&row2Layout);
      
      QDialog *dlg = new QDialog(this);
      dlg->setLayout(&layout);
      dlg->exec();
      
      P Offline
      P Offline
      Pl45m4
      wrote on 5 Feb 2025, 12:27 last edited by Pl45m4 2 May 2025, 12:28
      #2

      @RickyRister

      Sorry but cannot reproduce it...
      (used nothing but your code on Windows 10, mscv, Qt 6.8)

      LayoutTest.png

      What setup are you using?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      J R 2 Replies Last reply 5 Feb 2025, 12:47
      0
      • P Pl45m4
        5 Feb 2025, 12:27

        @RickyRister

        Sorry but cannot reproduce it...
        (used nothing but your code on Windows 10, mscv, Qt 6.8)

        LayoutTest.png

        What setup are you using?

        J Offline
        J Offline
        JonB
        wrote on 5 Feb 2025, 12:47 last edited by
        #3

        @Pl45m4
        As you can see, OP seems to be using Mac and a rather larger font that yours. maybe the issue is there somewhere?

        P 1 Reply Last reply 5 Feb 2025, 13:20
        0
        • J JonB
          5 Feb 2025, 12:47

          @Pl45m4
          As you can see, OP seems to be using Mac and a rather larger font that yours. maybe the issue is there somewhere?

          P Offline
          P Offline
          Pl45m4
          wrote on 5 Feb 2025, 13:20 last edited by
          #4

          @JonB said in How to prevent checkbox text from overlapping when multiple checkboxes in QHBoxLayout?:

          rather larger font that yours

          Then there is no reproducible example above :)
          And you cannot assume from the description that this is an issue only on Mac :)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • P Pl45m4
            5 Feb 2025, 12:27

            @RickyRister

            Sorry but cannot reproduce it...
            (used nothing but your code on Windows 10, mscv, Qt 6.8)

            LayoutTest.png

            What setup are you using?

            R Offline
            R Offline
            RickyRister
            wrote on 6 Feb 2025, 03:43 last edited by
            #5

            @Pl45m4

            What setup are you using?

            MacOS 15.1
            Apple clang
            Qt 6.7

            QApplication::font() returns QFont(.AppleSystemUIFont,13,-1,5,400,0,0,0,0,0,0,0,0,0,0,1).

            1 Reply Last reply
            0

            1/5

            5 Feb 2025, 09:07

            • Login

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