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



  • 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



  • 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?



  • 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


  • Qt Champions 2016

    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"



  • 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



  • 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


  • Lifetime Qt Champion

    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.



  • 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);

  • Lifetime Qt Champion

    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 ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.