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. [Solved] Dynamic Layout should be better then that what I've done
Forum Updated to NodeBB v4.3 + New Features

[Solved] Dynamic Layout should be better then that what I've done

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.5k 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.
  • C Offline
    C Offline
    casisto
    wrote on last edited by
    #1

    Hi there,

    I'm very new to QT (using 5.4). First, I get trough the zetcode Tutorial (for QT 4) and now I try to develop my very first own Application with QT.

    My problem: to get the UI Dynamic Layout in a nice behaviour (I mean a behaviour where it looks good when its small and when its big), I think I've used more lines then necessary.

    My Code:
    mainwindow.cpp
    @#include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
    {
    /VBox deklarieren - nur einmal QLayout definieren/
    QVBoxLayout *vbox = new QVBoxLayout(this);
    vbox->setMargin(20);
    /HBoxes deklarieren/
    QHBoxLayout *hbox_1 = new QHBoxLayout();
    QHBoxLayout *hbox_2 = new QHBoxLayout();
    QHBoxLayout *hbox_3 = new QHBoxLayout();

    /*Strings definieren*/
    QLabel *l_title    = new QLabel("Titel", this);
    QLabel *l_subtitle = new QLabel("Subtitel", this);
    
    /*Strings formatieren*/
    l_title->setAlignment(Qt::AlignCenter);
    l_title->setFont(QFont("Calibri", 36, QFont::Bold));
    l_title->setStyleSheet("QLabel {color: green}");
    
    l_subtitle->setAlignment(Qt::AlignCenter);
    
    /*Buttons definieren*/
    QPushButton *b_1 = new QPushButton("Button 1 ", this);
        b_1->setMaximumWidth(200);
    QPushButton *b_2 = new QPushButton("Button 2", this);
        b_2->setMaximumWidth(200);
    QPushButton *b_3  = new QPushButton("Button 3", this);
        b_3 ->setMaximumWidth(200);
    
    /*Layout definieren*/
        /*HBox1*/
    vbox->addStretch(1);
    hbox_1->addWidget(b_1, 0, Qt::AlignVCenter);
    vbox->addStretch(1);
    
        /*HBox2*/
    vbox->addStretch(1);
    hbox_2->addWidget(b_2, 0, Qt::AlignVCenter);
    vbox->addStretch(1);
    
        /*HBox3*/
    vbox->addStretch(1);
    hbox_3->addWidget(b_3,  0, Qt::AlignVCenter);
    vbox->addStretch(1);
    
        /*VBox*/
    vbox->addStretch(1);
    vbox->addWidget(l_title,    0, Qt::AlignHCenter);
    vbox->addStretch(1);
    vbox->addWidget(l_subtitle, 0, Qt::AlignHCenter);
    vbox->addStretch(5);
    vbox->addLayout(hbox_1);
    vbox->addStretch(1);
    vbox->addLayout(hbox_2);
    vbox->addStretch(1);
    vbox->addLayout(hbox_3);
    vbox->addStretch(10);
    vbox->addSpacing(50);
    
    /*Layout setzen*/
    setLayout(vbox);
    

    }@

    It would be nice If someone can give me some hints.
    greetings
    -casisto

    1 Reply Last reply
    0
    • C Offline
      C Offline
      casisto
      wrote on last edited by
      #2

      Sure? I had asked for help, not for a jester...

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Spam removed

        Your code doesn't look bad, you could use a loop to create and setup your three buttons since you do three times the exact the same thing

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • C Offline
          C Offline
          casisto
          wrote on last edited by
          #4

          Hi sGaist,

          thanks, and thanks for removing the Spam.

          I've now edited my Layout with a loop. But I get the error:
          "QLayout::addChildLayout: layout "" already has a parent"

          The arrays a declared in mainwindow.h and lang.h:
          QPushButton *btn[2];
          QHBoxLayout *hbox[2];
          QString str_btn[] = {"btn1", "btn2", "btn3"};

          @#include "mainwindow.h"
          #include "lang.h"

          MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
          {
          /VBox deklarieren - nur einmal QLayout definieren/
          QVBoxLayout *vbox = new QVBoxLayout(this);
          vbox->setMargin(20);

          /*Strings definieren*/
          QLabel *l_title    = new QLabel("Titel", this);
          QLabel *l_subtitle = new QLabel("Subtitel", this);
          
          /*Strings formatieren*/
          l_title->setAlignment(Qt::AlignCenter);
          l_title->setFont(QFont("Calibri", 36, QFont::Bold));
          l_title->setStyleSheet("QLabel {color: green}");
          
          l_subtitle->setAlignment(Qt::AlignCenter);
          
          for(int i = 0; i < 3; i++)
          {
              /*Buttons definieren*/
              btn[i] = new QPushButton(str_btn[i], this);
          
              /*Hboxes definieren*/
              hbox[i] = new QHBoxLayout();
          
              /*HBoxLayout definieren*/
              hbox[i]->addStretch(1);
              hbox[i]->addWidget(btn[i], 0, Qt::AlignVCenter);
              hbox[i]->addStretch(1);
          }
          
          vbox->addStretch(1);
          vbox->addWidget(l_title,    0, Qt::AlignHCenter);
          vbox->addStretch(1);
          vbox->addWidget(l_subtitle, 0, Qt::AlignHCenter);
          vbox->addStretch(5);
          vbox->addLayout(hbox[0]);
          vbox->addStretch(1);
          vbox->addLayout(hbox[1]);
          vbox->addStretch(1);
          vbox->addLayout(hbox[2]);
          vbox->addStretch(1);
          
          /*Layout setzen*/
          setLayout(vbox);
          

          }
          @

          It would be great if you could give me hint what throw the warning? (Also, I get the first button in the upper left corner, but I think the first error conditioned the second...)

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I've missed that one so:

            @QVBoxLayout *vbox = new QVBoxLayout(this);@

            Here you already set vbox as layout of your widget so the call to

            @setLayout(vbox);@

            is causing the warning.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C Offline
              C Offline
              casisto
              wrote on last edited by
              #6

              Ok,

              now I've get it.
              Funny thing, I had change the first line to this:
              @ QVBoxLayout *vbox = new QVBoxLayout;@

              Comment out the
              @setLayout(vbox);@

              has finished in an "unexpected close".

              Thanks, Samuel

              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