2016-02-03 216 views
0

我正在开发一个允许用户使用2D网格放置多米诺骨牌的项目。我使用QGridLayout和QWidgets做了一个基本版本,但是我意识到我希望多米诺骨牌之间的距离是可变的,并且基于它所面对的方向。更改QGridLayout中单个QWidget的间距

我的问题是,我该如何改变QGridLayout中单个QWidgets之间的间距?我已添加和图像以供参考。 (很抱歉的质量低,不得不使用MS画图)

enter image description here

+0

窗口小部件不能都是在相同的布局,如果你想单独设置自己的定位的方式。您需要将它们放入单独的'QHBoxLayout'中,并将它们堆叠在'QVBoxLayout'中。 –

+0

感谢您的回复,但我怀疑这是否行得通,因为它的2D网格(我猜图像没有传达这一点),因为同一列中的元素也必须具有可变距离。不使用布局手动创建我的网格可能更聪明吗?像在布局中有一个父窗口小部件,并把我的单元格放在窗口小部件中? – inzombiak

回答

1

下面是一个例子的程序,将创建3个X 3网格布局(主网格)和填充每个细胞与3×3的栅格布局(分格)。每个子网格将在每一侧添加一个QPushButton。子网格的其余空间将由QSpacerItem填充。这是实现的逻辑之一。编码中的评论将详细解释。

void MainWindow::on_cmdGenerate_Grid_clicked() 
{ 
    QWidget *grdParent; 
    grdParent = new QWidget(this->centralWidget()); 
    grdParent->setGeometry(QRect(0, 0, 300, 300)); 
    //Main Grid 
    QGridLayout *grdParentLayout = new QGridLayout(grdParent); 
    grdParentLayout->setObjectName(QStringLiteral("gridLayout")); 
    int intTotal=-1; 
    //Random position 
    int intArray[] = {1,5,7,6,3,4,8,2,0}; 

    for(int intRow=0;intRow<3;intRow++){ 
     for(int intCol=0;intCol<3;intCol++){ 
      intTotal++; 
      //Sub Grid that will sit in each cell of the main Grid 
      QGridLayout *grdChild = new QGridLayout(); 
      int intSubTotal=-1; 
      for(int intSubRow=0;intSubRow<3;intSubRow++){ 
       for(int intSubCol=0;intSubCol<3;intSubCol++){ 
        intSubTotal++; 
        if(intSubTotal==intArray[intTotal]) 
        { 
         //Adding push button in any one of the cell of sub grid 
         QPushButton *p = new QPushButton("Hi",grdParent); 
         p->setFixedSize(33,33); 
         grdChild->addWidget(p , intSubRow, intSubCol, 1, 1); 
        } 
        else 
        { 
         //Remaining sub grid cells will be filled by Spacer item 
         grdChild->addItem(new QSpacerItem(33,33,QSizePolicy::Fixed,QSizePolicy::Fixed) , intSubRow, intSubCol, 1, 1); 
        } 
       } 
      } 
      //adding sub grid to main grid 
      grdParentLayout->addLayout(grdChild,intRow,intCol,1,1); 
     } 
    } 
    //showing the main grid 
    grdParent->setVisible(true); 
} 

//Below code will draw boxes to visualize the Main Grid 
void MainWindow::paintEvent(QPaintEvent *paint) 
{ 
    QPainter *p = new QPainter(this); 
    for(int intX=0;intX<3;intX++){ 
     for(int intY=0;intY<3;intY++){ 
      p->drawRect(intX*100,intY*100,intX+100,intY+100); 
     } 
    } 
} 

enter image description here

+0

这适用于少量的细胞,但是一旦我有大约400个更新方向的所有细胞变得缓慢。可能切换到QGraphicsScene并手动管理间距是否有意义? – inzombiak

+0

@inzombiak,是的,(QGraphicsView + GraphicsScene)将在网格布局+间隔时变得最好。 – Jeet

+0

我接受你的答案,但QGraphicsView的工作方式更好,更容易。 – inzombiak

0

使用主电网布局内的次级网格布局。这些辅助网格布局将包含您的多米诺骨牌小部件和四个定位它的位置的间隔符(QSpacerItem)。

这是相当复杂的解决方案,但它可以让你完全控制小部件定位。您可以使用这些辅助网格布局的行拉伸和列拉伸值来获得“随机”放置的良好效果,如果这是您想要的。

我已经准备了一个小例子:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>Form</class> 
<widget class="QWidget" name="Form"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>650</width> 
    <height>543</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>Form</string> 
    </property> 
    <layout class="QHBoxLayout" name="horizontalLayout"> 
    <item> 
    <layout class="QGridLayout" name="gridLayout"> 
    <item row="1" column="0"> 
     <layout class="QGridLayout" name="gridLayout_5"> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_2"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_4"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_15"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_16"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="0" column="0"> 
     <layout class="QGridLayout" name="gridLayout_2"> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_15"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_16"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_13"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_14"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="1" column="1"> 
     <layout class="QGridLayout" name="gridLayout_7" rowstretch="0,0,0" columnstretch="0,0,0"> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_5"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_3"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_4"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_9"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_10"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="0" column="2"> 
     <layout class="QGridLayout" name="gridLayout_4"> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_7"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_3"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_3"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_4"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_8"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="2" column="0"> 
     <layout class="QGridLayout" name="gridLayout_6"> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_7"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_18"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_17"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_17"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_18"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="1" column="2"> 
     <layout class="QGridLayout" name="gridLayout_8"> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_6"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_5"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_6"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_2"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="2" column="1"> 
     <layout class="QGridLayout" name="gridLayout_9"> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_8"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_12"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_13"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_11"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_14"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="2" column="2"> 
     <layout class="QGridLayout" name="gridLayout_10"> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_9"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_9"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_10"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_5"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_6"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    <item row="0" column="1"> 
     <layout class="QGridLayout" name="gridLayout_3"> 
     <item row="1" column="0"> 
     <spacer name="horizontalSpacer_11"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="1" column="1"> 
     <widget class="QPushButton" name="pushButton_2"> 
     <property name="text"> 
      <string>PushButton</string> 
     </property> 
     </widget> 
     </item> 
     <item row="1" column="2"> 
     <spacer name="horizontalSpacer_12"> 
     <property name="orientation"> 
      <enum>Qt::Horizontal</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>40</width> 
      <height>20</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="0" column="1"> 
     <spacer name="verticalSpacer_7"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     <item row="2" column="1"> 
     <spacer name="verticalSpacer_8"> 
     <property name="orientation"> 
      <enum>Qt::Vertical</enum> 
     </property> 
     <property name="sizeHint" stdset="0"> 
      <size> 
      <width>20</width> 
      <height>40</height> 
      </size> 
     </property> 
     </spacer> 
     </item> 
     </layout> 
    </item> 
    </layout> 
    </item> 
    </layout> 
</widget> 
<resources/> 
<connections/> 
</ui> 
相关问题