Qt 在QGridLayout中放置小部件

Qt 在QGridLayout中放置小部件,qt,pyqt,grid-layout,Qt,Pyqt,Grid Layout,我需要在QgridLayout中创建QWidget(QtoolButton),而不指定行和列的索引。它应该根据所提到的行和列自动创建到布局中的下一个空单元 我在QgridLayouthelp中找不到任何方法 我尝试了.addWidget(self,QWidget w),但它将所有QWidget添加到(0,0)的索引中,并且所有按钮都相互重叠 提前感谢。您可以通过自己计算行和列来处理“下一个空单元格”。例如,您可以根据需要将QGridLayout子类化以实现任何“下一个空单元格”算法: class

我需要在
QgridLayout
中创建
QWidget
QtoolButton
),而不指定行和列的索引。它应该根据所提到的行和列自动创建到布局中的下一个空单元

我在
QgridLayout
help中找不到任何方法

我尝试了
.addWidget(self,QWidget w)
,但它将所有
QWidget
添加到(0,0)的索引中,并且所有按钮都相互重叠

提前感谢。

您可以通过自己计算行和列来处理“下一个空单元格”。例如,您可以根据需要将QGridLayout子类化以实现任何“下一个空单元格”算法:

class AutoGridLayout(QGridLayout):
    def __init__(self):
        QGridLayout.__init__(self)
        self.column = 0
        self.row = 0

    def addNextWidget(self, widget):
        self.addWidget(widget, self.row, self.column)
        self.column = self.column + 1   # Automatically advance to next column

# Setup main widget
app = QApplication(sys.argv)
mainWindow = QMainWindow()
centralWidget = QWidget()
mainWindow.setCentralWidget(centralWidget)

# Add widgets using the AutoGridLayout
layout = AutoGridLayout()
centralWidget.setLayout(layout)
layout.addNextWidget(QPushButton("1", centralWidget))
layout.addNextWidget(QPushButton("2", centralWidget))
layout.addNextWidget(QPushButton("3", centralWidget))

# Show and run the application
mainWindow.show()
app.exec_()
此源代码仅显示总体思路-您可以根据需要管理行和列索引。只需在addNextWidget()方法中通过计算下一个所需的行/列(在本例中,使用第0行中的下一列)来实现必要的逻辑。

您可以通过自己计算行和列来处理“下一个空单元格”。例如,您可以根据需要将QGridLayout子类化以实现任何“下一个空单元格”算法:

class AutoGridLayout(QGridLayout):
    def __init__(self):
        QGridLayout.__init__(self)
        self.column = 0
        self.row = 0

    def addNextWidget(self, widget):
        self.addWidget(widget, self.row, self.column)
        self.column = self.column + 1   # Automatically advance to next column

# Setup main widget
app = QApplication(sys.argv)
mainWindow = QMainWindow()
centralWidget = QWidget()
mainWindow.setCentralWidget(centralWidget)

# Add widgets using the AutoGridLayout
layout = AutoGridLayout()
centralWidget.setLayout(layout)
layout.addNextWidget(QPushButton("1", centralWidget))
layout.addNextWidget(QPushButton("2", centralWidget))
layout.addNextWidget(QPushButton("3", centralWidget))

# Show and run the application
mainWindow.show()
app.exec_()

此源代码仅显示总体思路-您可以根据需要管理行和列索引。只需通过计算下一个所需的行/列(在本例中,使用第0行中的下一列)来实现addNextWidget()方法中的必要逻辑。

假设您有一个包含4行和3列的
QGridLayout
,并且希望从上到下和从左到右自动向其添加按钮。如果您能够预测要添加的下一个按钮的位置,那么很容易实现这一点。就我们而言:

  • 行=添加的按钮数/列数
  • column=添加的按钮数%列数
(其他类型的填充工作类似)。让我们把它写成代码:

from PyQt4.QtGui import *

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.central = QWidget(self)
        self.grid = QGridLayout(self.central)
        self.rows = 4
        self.cols = 3
        self.items = self.grid.count()
        while(self.items < (self.rows * self.cols)):
            self.addButton()
        self.setCentralWidget(self.central)

    def addButton(self):
        # the next free position depends on the number of added items
        row = self.items/self.cols
        col = self.items % self.cols
        # add the button to the next free position
        button = QPushButton("%s, %s" % (row, col))
        self.grid.addWidget(button, row, col)
        # update the number of items
        self.items = self.grid.count()

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    ui = MyMainWindow()
    ui.show()
    sys.exit(app.exec_())
从PyQt4.QtGui导入*
类MyMainWindow(QMainWindow):
def uuu init uuu(self,parent=None):
超级(MyMainWindow,self)。\uuuuu初始化\uuuuuuu(父级)
self.central=QWidget(self)
self.grid=QGridLayout(self.central)
self.rows=4
self.cols=3
self.items=self.grid.count()
而(self.items<(self.rows*self.cols)):
self.addButton()
self.setCentralWidget(self.central)
def添加按钮(自身):
#下一个自由位置取决于添加的项目数
行=self.items/self.cols
col=self.items%self.cols
#将按钮添加到下一个自由位置
button=QPushButton(“%s,%s”%(行,列))
self.grid.addWidget(按钮、行、列)
#更新项目数
self.items=self.grid.count()
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=QApplication(sys.argv)
ui=MyMainWindow()
ui.show()
sys.exit(app.exec_())

假设您有一个包含4行3列的
QGridLayout
,并且您希望从上到下和从左到右自动向其添加按钮。如果您能够预测要添加的下一个按钮的位置,那么很容易实现这一点。就我们而言:

  • 行=添加的按钮数/列数
  • column=添加的按钮数%列数
(其他类型的填充工作类似)。让我们把它写成代码:

from PyQt4.QtGui import *

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.central = QWidget(self)
        self.grid = QGridLayout(self.central)
        self.rows = 4
        self.cols = 3
        self.items = self.grid.count()
        while(self.items < (self.rows * self.cols)):
            self.addButton()
        self.setCentralWidget(self.central)

    def addButton(self):
        # the next free position depends on the number of added items
        row = self.items/self.cols
        col = self.items % self.cols
        # add the button to the next free position
        button = QPushButton("%s, %s" % (row, col))
        self.grid.addWidget(button, row, col)
        # update the number of items
        self.items = self.grid.count()

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    ui = MyMainWindow()
    ui.show()
    sys.exit(app.exec_())
从PyQt4.QtGui导入*
类MyMainWindow(QMainWindow):
def uuu init uuu(self,parent=None):
超级(MyMainWindow,self)。\uuuuu初始化\uuuuuuu(父级)
self.central=QWidget(self)
self.grid=QGridLayout(self.central)
self.rows=4
self.cols=3
self.items=self.grid.count()
而(self.items<(self.rows*self.cols)):
self.addButton()
self.setCentralWidget(self.central)
def添加按钮(自身):
#下一个自由位置取决于添加的项目数
行=self.items/self.cols
col=self.items%self.cols
#将按钮添加到下一个自由位置
button=QPushButton(“%s,%s”%(行,列))
self.grid.addWidget(按钮、行、列)
#更新项目数
self.items=self.grid.count()
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=QApplication(sys.argv)
ui=MyMainWindow()
ui.show()
sys.exit(app.exec_())

其他答案的补充:如果您只需要具有可变项数的行,而不是实际的网格,那么您应该使用嵌套在一个QVBoxLayout中的多个QHBOxLayout(每行一个)。这也会让你得到你想要的行为,按需创建新项目,没有令人讨厌的间隙。

除了其他答案:如果你只需要具有可变项目数的行,而不是实际的网格,那么你应该使用嵌套在一个QVBoxLayout中的多个QHBOxLayout(每行一个)。这也会让你得到你想要的行为,按需创建新项目,没有令人讨厌的间隙。

@Andreas,谢谢,所以没有自动方式。我也在手动操作
self.column=self.column+1
它将永远存在,需要添加检查。@Andreas,谢谢,所以没有自动方式。我也在手动操作
self.column=self.column+1
它将永远存在,需要添加检查。这看起来像我一直在寻找的(自动化),但是添加多个
QHBoxLayout
将使UI不必要地沉重。沉重,如何?注意使用vBLayout->addLayout而不是添加额外的小部件。嵌套布局的开销是不相关的。这看起来像我想要的(自动化),但是添加多个
QHBoxLayout
将使UI不必要地沉重。沉重,如何?注意使用vBLayout->addLayout而不是添加额外的小部件。嵌套布局的开销是不相关的。哇,很好的解释性解决方案,我实际上是为了一些自动化,我已经在计算行和列,