在PyQt中,可以通过以下步骤实现从表格微件中选择多行后,单元格微件(按钮)显示在正确的位置:
这样,当从表格中选择多行时,按钮微件将显示在所选行的每个单元格中。
以下是一个示例代码片段,演示了如何实现上述功能:
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QPushButton
from PyQt5.QtCore import QRect
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.table = QTableWidget(self)
self.table.setColumnCount(3)
self.table.setRowCount(5)
for row in range(5):
for col in range(3):
item = QTableWidgetItem(f"Row {row}, Col {col}")
self.table.setItem(row, col, item)
self.table.setSelectionBehavior(QTableWidget.SelectRows)
self.table.setSelectionMode(QTableWidget.SelectItems)
self.table.selectionChanged.connect(self.handleSelectionChanged)
def handleSelectionChanged(self):
selectedIndexes = self.table.selectedIndexes()
for index in selectedIndexes:
row = index.row()
col = index.column()
rect = self.table.visualRect(index)
button = QPushButton("Button", self.table)
button.move(rect.topLeft())
button.show()
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
在这个示例中,我们创建了一个包含5行3列的表格,并为每个单元格添加了一个文本项。当选择表格中的多行时,按钮微件将显示在所选行的每个单元格中。
请注意,这只是一个简单的示例,实际应用中可能需要根据具体需求进行适当的修改和扩展。
腾讯云相关产品和产品介绍链接地址:
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云