更改QGridLayout

时间:2016-02-03 16:22:50

标签: qt user-interface c++11

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

我的问题是,如何更改QGridLayout中各个QWidgets之间的间距?我添加了图像以供参考。 (抱歉质量低,不得不使用MS Paint)

enter image description here

2 个答案:

答案 0 :(得分:1)

下面是一个示例程序,它将创建3 X 3网格布局(主网格)并用3 X 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

答案 1 :(得分: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>