0

我正在构建一个具有多种形式的 Qt 插件。我有一个主窗体,它在窗体的左侧放置了一个树小部件。

我想将项目添加到此树中,以便单击这些项目将在同一表单上加载相应的表单。但我希望树小部件处于活动状态,以便我也可以选择任何其他形式。

我能够使用以下代码在主窗体上显示一个窗体:

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

其中 Form1 是我打算显示的表单的类。但是,这也掩盖了树小部件。而且我必须对被点击的树中的项目进行字符串比较以显示适当的表单。

有人可以帮我解决这个问题,因为我对 Qt 编程很陌生。

谢谢

4

2 回答 2

0

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.

于 2011-10-18T17:16:32.023 回答
0

我不完全理解您要实现的目标,但您显示的代码表明您不使用 Qt 提供的布局。

如果您的目标是能够根据在树中单击的项目动态加载表单,则可以通过在其中插入树的布局(比如说 QHBoxLayout)和可以在其中插入的QStackedWidget来实现“存储”每个表单(通过使用 addWidget())并通过调用 setCurrentIndex() 选择要显示的表单。

于 2011-10-18T08:52:59.503 回答