Displaying forms using Tree in Qt

575 views Asked by At

I'm building a Qt plugin with multiple forms. I have a main form which has a tree widget placed on the left of the form.

I want to add items to this tree, such that clicking on these items would load the corresponding form on the same form. But I want the tree widget to be active so that I can select any other form also.

I was able to display a form on the main form using the following code:

Form1 *myform;
myform=new Form1(this);
myform->show();

where Form1 is the class of the form i intend to display. However this, covers up the tree widget also. And I have to do a string comparison of the item in tree being clicked to display the appropriate form.

Can someone please help me with this as I'm very new to Qt programming.

Thanks

2

There are 2 answers

0
ixM On

I don't exactly understand what you are trying to achieve but the bit of code you are showing suggests that you do not use the layouts provided by Qt.

If your goal is to be able to dynamically load a form depending on the item that was clicked in the tree, you could achieve that by having a layout (let's say QHBoxLayout) where you would insert your tree and a QStackedWidget in which you could "store" each form (by using addWidget()) and choose which one you want to display by calling setCurrentIndex().

0
Liz On

ixM has a good suggestion. The first step should definitely be to use layouts in your main window - separating the tree from the rest of the window - where you are going to put your form. I would suggest using a splitter, because then the user can resize the two halves. You can set the splitter as the main widget of your CentralWidget in your main window.

    QSplitter splitter = new QSplitter(CentralWidget);
    splitter->setOrientation(Qt::Horizontal);
    splitter->setHandleWidth(3);
    splitter->setChildrenCollapsible(false);

    MyTree= new QTreeWidget(splitter);  
    splitter->addWidget(MyTree);

Then add your tree widget to the splitter, which will be on the left side.

The next step is to add a placeholder widget on the right side of your splitter. We are also going to add a layout inside that widget. This layout is very important we are going to use it later.

    QWidget WidgetRightSide = new QWidget(splitter);
    QVBoxLayout setupLayout= new QVBoxLayout(WidgetRightSide);
    setupLayout->setSpacing(0);
    setupLayout->setContentsMargins(0, 0, 0, 0);

Now, at this point, this is where my answer really differs from the previous answer. You could use a QStackedWidget. That is certainly an option. The problem with that is that you have to create and load all your forms at the beginning. That uses way more memory, and will take longer to start up. That's not so bad if you have 2-5 forms, but when we are talking about 20, 30 or more forms that's really ugly.

So what I would suggest instead, is that when the user selects something in the tree, we will remove the old form, and add the newly selected form at that point.

When the selected item in the tree changes this is now what we have to do.

First, remove all the stuff from the previously selection form.

  QLayoutItem *_Item;
  while ((_Item = setupLayout->takeAt(0)))
    delete _Item;

Next, figure out what form to show next, and create it.

  QWidget *ActiveSetupForm = NULL;

  if ( I need to load form 1)
  {
     ActiveSetupForm = new YourNewForm( WidgetRightSide);
  }
  else ...

And lastly, add your new form to our layout.

   if(ActiveSetupForm)
   {
       setupLayout->addWidget(pActiveSetupForm);
   } 

Just as a side note. Layouts are tricky to do by hand. I would strongly suggest that you look into using the QtDesigner when you are creating your forms. It makes life soooo much easier. If you would like to know more about it check out this link.