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. QHboxLaoyut and QVboxlayout (Can some one suggest a better code)
Qt 6.11 is out! See what's new in the release blog

QHboxLaoyut and QVboxlayout (Can some one suggest a better code)

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 4.6k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    Can some one help me to optimize this code

    class myView : public QWidget {

    };
    myView:myView()
    {
    setWindowTitle(title);

    //creating two splitters
    //1) horizontal splitter
    //2) vertical   splitter
    QSplitter* horizontalSplitter =  new  QSplitter;
    QSplitter* verticalSplitter   =  new  QSplitter;
    
    QTreeWidget* treeWIdget = new QTreeWidget;
    QTableView* tableView = new QTableView;
    

    horizontalSplitter->addWidget(treeWIdget );
    horizontalSplitter->addWidget(tableView );

     // layout for ErrorTree|ErrorTable
    QVBoxLayout *container_layout = new  QVBoxLayout;
    container_layout->addWidget(horizontalSplitter);
    QWidget     *container =        new  QWidget;
    container->setLayout(container_layout);
    
    /* creating the button/checkbox at the bottom widget*/
    QWidget *bottomWidget = new QWidget;
    QHBoxLayout *controlsLayout = new  QHBoxLayout;
    QPushButton* button1 =  new QPushButton("Button1");
    QCheckBox* checkBox      =  new  QCheckBox("CheckBox1");
    QCheckBox *checkBox1  =  new  QCheckBox("CheckBox2);
    button1->setMaximumWidth(80);
    checkBox->setMaximumWidth(120);
    
    /* addd widgets to control layout*/
    controlsLayout->addWidget(button1);
    controlsLayout->addWidget(checkBox);
    controlsLayout->addWidget(checkBox1);
    bottomWidget->setLayout(controlsLayout);
    
    /* Vertical Splitter for Tree/Table and bottomWidge 
    verticalSplitter->setOrientation(Qt::Vertical);
    verticalSplitter->addWidget(container);
    verticalSplitter->addWidget(bottomWidget);
    

    Basically I want QWidget as follows
    I want to split the QWidget vertically ,

    the first pane is a should be treeWidget abd QTableView splltted with a horizonalSpillter (QSplitter) and

    2 )the bottomWidget should have button

    Please help me to oraganize the code

    Q 1 Reply Last reply
    0
    • M Offline
      M Offline
      mar0029
      wrote on last edited by
      #2

      Hello!

      How do you want to split the Main QWidget?
      Split it so there is a left and right side or a top and bottom?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        The main QWidget is spilt vertically between two parts (with a vertical splitter)

        1. upper part

        2. lower part

        3. upper part is again split into two parts with horizontal splitter . The upper part is split into QTreeWidget and QTreeView

        4. lower part is HboxLayout where buttons are placed horizontally

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          This is very easy to make in Designer.

          https://www.dropbox.com/s/h1zwohe7uarslrr/untitled6.zip?dl=0

          The trick to insert a Qsplitter in Designer, is to first place 2 widgets
          then right click and in layout u can select splitter.

          PS, Im not sure what you mean by
          "Can some one help me to optimize this code"

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            I am able to achive the desired results ,What I meant "Can some one help me to optimize this code" . is this the best way to achieve the results or there can be a better way

            1 Reply Last reply
            0
            • Q Qt Enthusiast

              Can some one help me to optimize this code

              class myView : public QWidget {

              };
              myView:myView()
              {
              setWindowTitle(title);

              //creating two splitters
              //1) horizontal splitter
              //2) vertical   splitter
              QSplitter* horizontalSplitter =  new  QSplitter;
              QSplitter* verticalSplitter   =  new  QSplitter;
              
              QTreeWidget* treeWIdget = new QTreeWidget;
              QTableView* tableView = new QTableView;
              

              horizontalSplitter->addWidget(treeWIdget );
              horizontalSplitter->addWidget(tableView );

               // layout for ErrorTree|ErrorTable
              QVBoxLayout *container_layout = new  QVBoxLayout;
              container_layout->addWidget(horizontalSplitter);
              QWidget     *container =        new  QWidget;
              container->setLayout(container_layout);
              
              /* creating the button/checkbox at the bottom widget*/
              QWidget *bottomWidget = new QWidget;
              QHBoxLayout *controlsLayout = new  QHBoxLayout;
              QPushButton* button1 =  new QPushButton("Button1");
              QCheckBox* checkBox      =  new  QCheckBox("CheckBox1");
              QCheckBox *checkBox1  =  new  QCheckBox("CheckBox2);
              button1->setMaximumWidth(80);
              checkBox->setMaximumWidth(120);
              
              /* addd widgets to control layout*/
              controlsLayout->addWidget(button1);
              controlsLayout->addWidget(checkBox);
              controlsLayout->addWidget(checkBox1);
              bottomWidget->setLayout(controlsLayout);
              
              /* Vertical Splitter for Tree/Table and bottomWidge 
              verticalSplitter->setOrientation(Qt::Vertical);
              verticalSplitter->addWidget(container);
              verticalSplitter->addWidget(bottomWidget);
              

              Basically I want QWidget as follows
              I want to split the QWidget vertically ,

              the first pane is a should be treeWidget abd QTableView splltted with a horizonalSpillter (QSplitter) and

              2 )the bottomWidget should have button

              Please help me to oraganize the code

              Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #6

              I want to have following Window in which main QWidget is split into two parts with a vertical splitter (QSplitter) ( how to achive the results in a better way)

              1 Win1 2) Win2

              Win1 is again split with horizontal splitter (QSplitter) . The left part of Win1 should contain a QTreeWidget and right side should contain a QTableView . For Win2 , contains Button1 (QPushButton) and Button2 (QPushButton). I am using following code .

              class myView:public QWidget {
              }
              myView::myView() {
              setWindowTitle("Window");

                QSplitter* horSplitter =  new  QSplitter;
                QSplitter* verSplitter =  new  QSplitter;
              
               QTreeWidget tree = new QTreeWidget;
               QTableView table = new QTableView;     
               horSplitter->addWidget(tree)
               horSplitter->addWidget(table) 
               QVBoxLayout *con_layout = new QVBoxLayout;
               con_layout->addWidget(horizontalSplitter);
               QWidget     *container =        new IN_CURRENT_POOL QWidget;
               container->setLayout(con_layout);
              
               QWidget *bottomWidget = new  QWidget;
               QHBoxLayout *controlsLayout = new  QHBoxLayout;
               QPushButton* button1 =  new  QPushButton("Button1");
               QPushButton* button2    =  new QPushButton("Button1");
               controlsLayout->addWidget(button1);
               controlsLayout->addWidget(button2);
               bottomWidget->setLayout(controlsLayout);
              
               verSplitter->setOrientation(Qt::Vertical);
               verticalSplitter->addWidget(container);
               verticalSplitter->addWidget(bottomWidget);
              
               // layouts
               QVBoxLayout *layout =   new IN_CURRENT_POOL QVBoxLayout;
               layout->addWidget(verSplitter);
               setLayout(layout);
              

              }
              My Question is there any other way in QT code or more optimized way to achieve the desired window as shown in the image

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

                Hi,

                What images ?

                What is the use of container ? You seem to only put your horizontal splitter in it so it's just wasting resources. QSplitter is already a widget so you can put it in your vertical splitter directly.

                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
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by
                  #8

                  is that fine . COuld you let me know if the code is optimized Could you review the code an Also please let me know if it is proper logical sequence

                  //creating two splitters
                  //1) horizontal splitter
                  //2) vertical splitter
                  QSplitter* horizontalSplitter = new IN_CURRENT_POOL QSplitter;
                  QSplitter* verticalSplitter = new IN_CURRENT_POOL QSplitter;

                  // layouts
                  QVBoxLayout *mainLayout =   new IN_CURRENT_POOL QVBoxLayout;
                  setLayout(mainLayout);
                  verticalSplitter->setOrientation(Qt::Vertical);
                  mainLayout->addWidget(verticalSplitter);
                  verticalSplitter->addWidget(horizontalSplitter);
                  

                  QTreeWidget * tree = new QTreeWidget;
                  QTableView* table = new QTableView;
                  horizontalSplitter ->addWidget(tree);
                  horizontalSplitter ->addWidget(table);

                  /* for refresh/IncludeBlockage checkbox*/
                  QWidget *bottomWidget = new IN_CURRENT_POOL QWidget;
                  verticalSplitter->addWidget(bottomWidget);
                  
                  /* creating the refresh button/checkbox at the bottom widget*/
                  QHBoxLayout *controlsLayout = new IN_CURRENT_POOL QHBoxLayout;
                  bottomWidget->setLayout(controlsLayout);
                  d_refreshButton =  new IN_CURRENT_POOL QPushButton("Refresh");
                  d_checkBox      =  new IN_CURRENT_POOL QCheckBox("Include Blockages");
                  d_zoomCheckBox  =  new IN_CURRENT_POOL QCheckBox("Zoom");
                  
                  d_refreshButton->setMaximumWidth(80);
                  d_checkBox->setMaximumWidth(120);
                  controlsLayout->addWidget(d_refreshButton);
                  controlsLayout->addWidget(d_checkBox);
                  controlsLayout->addWidget(d_zoomCheckBox);
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    From a pure comprehension point of view. It's hard to read.

                    You should build each widget clearly e.g.

                    // Top widget
                    QTreeWidget * treeWidget = new QTreeWidget;
                    QTableView* tableView = new QTableView;
                    QSplitter* horizontalSplitter = new QSplitter;
                    horizontalSplitter ->addWidget(treeWidget);
                    horizontalSplitter ->addWidget(tableView);
                    

                    That is one clear element of your complex widget. The same for the bottom part. Create all the elements, then the layout, put them in it and then in the container widget. And only after that, create the vertical splitter. That will make your code clearer to read.

                    By the way, what is IN_CURRENT_POOL ?

                    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

                    • Login

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