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. Create unusual horizontal layout with QWidgets
Qt 6.11 is out! See what's new in the release blog

Create unusual horizontal layout with QWidgets

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 891 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.
  • S Offline
    S Offline
    Suares
    wrote on last edited by
    #1

    Hello, guys!
    I have a little example where green, orange and red labels:

    MainWindow::MainWindow(QWidget *parent) :
    	QMainWindow(parent)
    {
    	setMinimumSize(640, 480);
    
    	QWidget* centralWgt = new QWidget{ this };
    	setCentralWidget(centralWgt);
    
    	QHBoxLayout* mainLayout = new QHBoxLayout;
    
    	QLabel* leftLbl = new QLabel{ this };
    	leftLbl->setStyleSheet("background-color: green;");
    	leftLbl->setFixedWidth(100);
    	mainLayout->addWidget(leftLbl);
    	mainLayout->addStretch();
    
    	QLabel* centerLbl = new QLabel{ this };
    	centerLbl->setStyleSheet("background-color: orange;");
    	centerLbl->setFixedWidth(100);
    	mainLayout->addWidget(centerLbl);
    	mainLayout->addStretch();
    
    	QLabel* rightLbl = new QLabel{ this };
    	rightLbl->setStyleSheet("background-color: red;");
    	rightLbl->setFixedWidth(200);
    	mainLayout->addWidget(rightLbl);
    
    	centralWidget()->setLayout(mainLayout);
    }
    

    Independent from the main window size and label sizes I want to show the centerLbl always in the center of the central widget whereas the leftLbl move to the left side and the rightLbl always move to the right side.
    I know how to do this in QML:

    Window {
        minimumWidth: 640; minimumHeight: 480;
        visible: true
    
        Rectangle {
            id: leftId;
            height: parent.height;
            width: 100;
            anchors.left: parent.left;
            color: "green";
            visible: true;
        }
    
        Rectangle {
            id: centerId;
            width: 50; height: parent.height;
            anchors.centerIn: parent;
            color: "orange";
            visible: true;
        }
    
        Rectangle {
            id: rightId;
            width: 200; height: parent.height;
            anchors.right: parent.right;
            color: "red";
            visible: true;
        }
    }
    

    But I must avoid QML in this case.
    Could you please give me an advise how to do the same with QWidgets?

    Thanks for help!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! I've found a solution but I'm not sure if it meets your demands :-) It behaves the way you described above but only if the elements do not collide. I've made a screenshot:

      screenshot

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Suares
        wrote on last edited by Suares
        #3

        Hello, @Wieland!

        This is exactly what I want.
        Thank you! :)

        ? 1 Reply Last reply
        0
        • S Suares

          Hello, @Wieland!

          This is exactly what I want.
          Thank you! :)

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @Suares Great :-)

          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