Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Create unusual horizontal layout with QWidgets

    General and Desktop
    2
    4
    518
    Loading More Posts
    • 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
      Suares last edited by

      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 Reply Quote 0
      • ?
        A Former User last edited by A Former User

        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 Reply Quote 1
        • S
          Suares last edited by Suares

          Hello, @Wieland!

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

          ? 1 Reply Last reply Reply Quote 0
          • ?
            A Former User @Suares last edited by

            @Suares Great :-)

            1 Reply Last reply Reply Quote 0
            • First post
              Last post