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 group two widgets so that they look like one widget?
Forum Updated to NodeBB v4.3 + New Features

How to group two widgets so that they look like one widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 267 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.
  • M Offline
    M Offline
    mjs225
    wrote on last edited by
    #1

    I need to display rich text on a checkbox. I tried grouping a QCheckBox and QLabel in a QHBoxLayout. The problem is that it doesn't look like one widget.

    struct CheckBox : QWidget{
    	QHBoxLayout layout{this};
    	QCheckBox checkbox;
    	QLabel label;
    	CheckBox(){
    		layout.addWidget(&checkbox);
    		layout.addWidget(&label);
    		// layout.addStretch();
    	}
    };
    

    If no stretch is added and the group has a large width, there will be a large space between the checkbox and label.
    If a stretch is added after the label, other widgets cannot be added close next to the group.

    How to group the two widgets so that they look like one widget?

    M 1 Reply Last reply
    0
    • M mjs225

      I need to display rich text on a checkbox. I tried grouping a QCheckBox and QLabel in a QHBoxLayout. The problem is that it doesn't look like one widget.

      struct CheckBox : QWidget{
      	QHBoxLayout layout{this};
      	QCheckBox checkbox;
      	QLabel label;
      	CheckBox(){
      		layout.addWidget(&checkbox);
      		layout.addWidget(&label);
      		// layout.addStretch();
      	}
      };
      

      If no stretch is added and the group has a large width, there will be a large space between the checkbox and label.
      If a stretch is added after the label, other widgets cannot be added close next to the group.

      How to group the two widgets so that they look like one widget?

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @mjs225
      use QLayout::setAlignment(QWidget *w, Qt::Alignment alignment) after addWidget

      And create your objects dynamically (with new), i'm suprised this doesn't crash !

      M 1 Reply Last reply
      0
      • M mpergand

        @mjs225
        use QLayout::setAlignment(QWidget *w, Qt::Alignment alignment) after addWidget

        And create your objects dynamically (with new), i'm suprised this doesn't crash !

        M Offline
        M Offline
        mjs225
        wrote on last edited by mjs225
        #3

        @mpergand Thanks. Here's the code that works in my case:

        CheckBox(){
        	layout.addWidget(&checkbox);
        	layout.setAlignment(&checkbox, Qt::AlignRight);
        	layout.addWidget(&label);
        	layout.setAlignment(&label, Qt::AlignLeft);
        }
        
        JonBJ 1 Reply Last reply
        0
        • M mjs225

          @mpergand Thanks. Here's the code that works in my case:

          CheckBox(){
          	layout.addWidget(&checkbox);
          	layout.setAlignment(&checkbox, Qt::AlignRight);
          	layout.addWidget(&label);
          	layout.setAlignment(&label, Qt::AlignLeft);
          }
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @mjs225

          @mpergand said in How to group two widgets so that they look like one widget?:

          And create your objects dynamically (with new), i'm suprised this doesn't crash !

          Still suggest you follow @mpergand's advice for this, for safety.

          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