简体   繁体   English

如何将 TableWidget 的 CellWidget 与 pyqt5 中的项目中心对齐

[英]How can i align a CellWidget of a TableWidget to the center of the Item in pyqt5

i have a comboBox inside of a tableWidget and the verticalHeader DefaultSectionSize is 60.我在 tableWidget 中有一个 comboBox,verticalHeader DefaultSectionSize 为 60。

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self,parent)
        self.table = QTableWidget()
        self.setCentralWidget(self.table)
       
        self.table.verticalHeader().setDefaultSectionSize(60)
        self.table.setColumnCount(2)
        self.table.setRowCount(2)
        
        data = ["1","2"]
    
        for i in range(2):
            item = QTableWidgetItem(data[i])
            self.table.setItem(i,0,item)
            self.combo_sell = QComboBox()
            self.combo_sell.setMaximumHeight(30)
            self.table.setCellWidget(i,1,self.combo_sell)

But since i set the maximum size of the comboBox to 30, it stays in the top of the item.但由于我将 comboBox 的最大尺寸设置为 30,它停留在项目的顶部。

图片

I want to know if there's a way to align it to the center.我想知道是否有办法将其与中心对齐。

When setting an index widget, the view tries to set the widget geometry based on the visualRect() of the index.设置索引小部件时,视图会尝试根据索引的visualRect()设置小部件几何形状。 Setting a fixed dimension forces the widget to align itself to the default origin, which is usually the top left corner.设置固定尺寸会强制小部件将自身与默认原点对齐,默认原点通常是左上角。

The only way to vertically center a widget with a fixed height is to use a container with a vertical box layout and add the combo to it:将具有固定高度的小部件垂直居中的唯一方法是使用具有垂直框布局的容器并将组合添加到其中:

        for i in range(2):
            item = QTableWidgetItem(data[i])
            item.setTextAlignment(Qt.AlignCenter)
            self.table.setItem(i,0,item)
            container = QWidget()
            layout = QVBoxLayout(container)
            combo_sell = QComboBox()
            layout.addWidget(combo_sell)
            combo_sell.setMaximumHeight(30)
            self.table.setCellWidget(i, 1, container)

Note: setting instance attributes in a for loop is pointless, as the reference is lost every time the cycle loops.注意:在 for 循环中设置实例属性是没有意义的,因为每次循环循环都会丢失引用。

If you need a simple reference to the combo, you can set it as an attribute of the widget:如果您需要对组合的简单引用,可以将其设置为小部件的属性:

    container.combo_sell = QComboBox()

In this way you can easily access it when required:通过这种方式,您可以在需要时轻松访问它:

        widget = self.table.cellWidget(row, column)
        if widget and hasattr(widget, 'combo'):
            combo = widget.combo
            print(combo.currentIndex())

Note that the reference is created for the python wrapper of the widget, and that behavior might change in future versions of Qt.请注意,该引用是为小部件的 python 包装器创建的,并且该行为可能会在 Qt 的未来版本中发生变化。 A better and safer way to achieve this would be to use a subclass, which would also allow easier access to the combo:实现这一点的更好和更安全的方法是使用子类,这也将允许更轻松地访问组合:

class TableCombo(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        self.combo = QComboBox()
        layout.addWidget(self.combo)
        self.combo.setMaximumHeight(30)
        self.currentIndex = self.combo.currentIndex
        self.setCurrentIndex = self.combo.setCurrentIndex
        self.addItems = self.combo.addItems

# ...

            combo_sell = TableCombo()
            self.table.setCellWidget(i, 1, combo_sell)

# ...

        combo = self.table.cellWidget(row, column)
        print(combo.currentIndex())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM