QTablewidget setcurrwidget设置多个不重叠
时间: 2024-05-14 07:14:19 浏览: 94
您可以使用QTableWidget的setCellWidget方法来设置多个不重叠的单元格小部件。以下是一个示例代码:
```python
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QPushButton, QWidget, QHBoxLayout
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('QTableWidget Example')
# Create a QTableWidget object
self.table_widget = QTableWidget(self)
self.table_widget.setGeometry(50, 50, 250, 200)
self.table_widget.setColumnCount(2)
self.table_widget.setRowCount(3)
# Create QPushButton objects and add them to the table
button1 = QPushButton("Button 1")
button2 = QPushButton("Button 2")
button3 = QPushButton("Button 3")
# Create QHBoxLayout objects to add the buttons to
layout1 = QHBoxLayout()
layout2 = QHBoxLayout()
layout3 = QHBoxLayout()
# Add the buttons to the layouts
layout1.addWidget(button1)
layout2.addWidget(button2)
layout3.addWidget(button3)
# Create QWidget objects to hold the QHBoxLayouts
widget1 = QWidget()
widget2 = QWidget()
widget3 = QWidget()
# Set the QHBoxLayouts as the layouts for the QWidgets
widget1.setLayout(layout1)
widget2.setLayout(layout2)
widget3.setLayout(layout3)
# Add the QWidgets to the table using setCellWidget
self.table_widget.setCellWidget(0, 0, widget1)
self.table_widget.setCellWidget(1, 0, widget2)
self.table_widget.setCellWidget(2, 0, widget3)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在此示例中,我们创建了一个QTableWidget对象,并将其添加到QWidget对象中。我们然后创建三个QPushButton对象,并将它们添加到每个QHBoxLayout对象中。我们然后创建三个QWidget对象,并将每个QHBoxLayout设置为其布局。最后,我们使用setCellWidget方法将这三个QWidget对象添加到QTableWidget的不同单元格中。这样,我们就可以在三个不同的单元格中看到三个不同的按钮。
阅读全文