面向对象菜单系统

OOP menu system

本文关键字:系统 菜单 面向对象      更新时间:2023-10-16

我正在尝试使用Qt4.8中的QGraphics制作菜单系统(带子菜单)。复合模式听起来是一个很好的解决方案,但是我被卡住了。

这是UML图:https://i.stack.imgur.com/JgWpQ.png

对象的结构像树。

      +-------------+
      |Root         |+---------------------+
      +-------------+|                     |
         +           |                     |
         |           |                     |
         |           |                     |
         |           v                     v
+-------<++         +----------+    +----------------+
|Leaf1    |    +---+|MenuNode1 |    | MenuNode2      |+--------+
+---------+    |    +----------+    +----------------+         |
               |            +                  +               |
               |            |                  |               |
               v            v                  v               v
       +---------+ +-----------+     +-------------+       +-------------+
       |Leaf2    | |Leaf3      |     | MenuNode4   |       |Leaf5        |
       +---------+ +-----------+     +-------+-----+       +-------------+
                                     +       |
                                     |       |
                                     |       v
                         +---------+ |  +------------+
                         |         <-+  |            |
                         +---------+    +------------+

第一个问题是Qt绘制了孔结构,但我只想显示当前节点的子节点。因此,我试图通过使所有AbstractMenuItems不可见和

来解决这个问题
void MenuNode::paint( QPainter *painter, ....) {
    if( this->parentWidget() == AbstractMenuItem::currentPosition) { //show only the direct children
         for(int i = 0; i<3; i++) {//show only 3 elements at a time
              child->at(i)->setVisibile(true);
              child->at(i)->setFocus( i == 1 );
          }  
    } else { //paint as a child
       painter->drawText("Child MenuNode");
    }
}

我认为我的想法不是太离谱。有什么建议可以让它变得更好吗?

首先,我假定您不希望您的菜单项是Qt小部件。

接下来,为了封装图形对象,您可以使用QGraphicsScene作为可编程的"字段",并将QGraphicsItem用于您想要编程的所有菜单项。但是,需要更多的问题/答案来指定更详细的实现。我相信这些类在Qt 4.8中也是可用的。您还可以轻松地获得基于opengl的编程的优势。