Enable/Disable 在 QTableWidget 中选择行时的 QPushButton

Enable/Disable QPushButton upon row selection in QTableWidget

我有一个 QTableWidget 和一个 Delete QPushButton。

我希望仅当从 QTableWidget 中选择了一行时才启用删除按钮,如果未选择任何行,则应禁用该按钮。

我正在初始化时:

#Make rows selectable
self.tableWidget.setSelectionBehavior(QtWidgets.QTableWidget.SelectRows)
# Diable button initially
if not len(self.tableWidget.selectionModel().selectedRows()):
        self.btnDelete.setEnabled(False)

这会将“删除”按钮设置为禁用状态。

如何在行选择中启用它?

使用: Python 3.8,PyQt5 在 Windows10

编辑 1: 最小工作示例:

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'Fruits'
        self.left = 0
        self.top = 0
        self.width = 300
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.createTable()

        self.btndelete = QtWidgets.QPushButton("DELETE")
        self.btndelete.setObjectName("btndelete")
        self.btndelete.clicked.connect(self.deleteRow)
        if not len(self.tableWidget.selectionModel().selectedRows()):
            self.btndelete.setEnabled(False)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget) 
        self.layout.addWidget(self.btndelete)

        self.setLayout(self.layout) 

        # Show widget
        self.show()

    def createTable(self):
       # Create table
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(4)
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setItem(0,0, QTableWidgetItem("Apple"))
        self.tableWidget.setItem(0,1, QTableWidgetItem("15"))
        self.tableWidget.setItem(1,0, QTableWidgetItem("Banana"))
        self.tableWidget.setItem(1,1, QTableWidgetItem("11"))
        self.tableWidget.setItem(2,0, QTableWidgetItem("Dogs"))
        self.tableWidget.setItem(2,1, QTableWidgetItem("10"))
        self.tableWidget.setItem(3,0, QTableWidgetItem("Cats"))
        self.tableWidget.setItem(3,1, QTableWidgetItem("2"))
        self.tableWidget.move(0,0)

        # table selection 
        self.tableWidget.setSelectionBehavior(QtWidgets.QTableWidget.SelectRows)

    def deleteRow(self):
        pass

    @pyqtSlot()
    def on_click(self):
        print("\n")
        for currentQTableWidgetItem in self.tableWidget.selectedItems():
            print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_()) 

试试这个,启用一个按钮。

self.btnDelete.setDisabled(False)

你可以试试

bool_enable = len(self.tableWidget.selectionModel().selectedRows()) > 0
self.btnDelete.setEnabled(bool_enable)

您必须使用与 QTableWidget 关联的 selectionModel 的 selectionChanged 信号来根据所选行数更改按钮的状态:

    # ...
    self.tableWidget.setSelectionBehavior(QtWidgets.QTableWidget.SelectRows)
    self.tableWidget.selectionModel().selectionChanged.connect(
        self.on_selection_changed
    )
    self.on_selection_changed()

def on_selection_changed(self):
    self.btnDelete.setEnabled(
        bool(self.tableWidget.selectionModel().selectedRows())
    )

MWE:

import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QTableWidget,
    QTableWidgetItem,
    QPushButton,
    QVBoxLayout,
)
from PyQt5.QtCore import pyqtSlot


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = "Fruits"
        self.left = 0
        self.top = 0
        self.width = 300
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.createTable()

        self.btndelete = QPushButton("DELETE")
        self.btndelete.setObjectName("btndelete")
        self.btndelete.clicked.connect(self.deleteRow)

        self.on_selection_changed()

        lay = QVBoxLayout(self)
        lay.addWidget(self.tableWidget)
        lay.addWidget(self.btndelete)

        # Show widget
        self.show()

    def createTable(self):
        # Create table
        self.tableWidget = QTableWidget(4, 2)
        self.tableWidget.setItem(0, 0, QTableWidgetItem("Apple"))
        self.tableWidget.setItem(0, 1, QTableWidgetItem("15"))
        self.tableWidget.setItem(1, 0, QTableWidgetItem("Banana"))
        self.tableWidget.setItem(1, 1, QTableWidgetItem("11"))
        self.tableWidget.setItem(2, 0, QTableWidgetItem("Dogs"))
        self.tableWidget.setItem(2, 1, QTableWidgetItem("10"))
        self.tableWidget.setItem(3, 0, QTableWidgetItem("Cats"))
        self.tableWidget.setItem(3, 1, QTableWidgetItem("2"))

        # table selection
        self.tableWidget.setSelectionBehavior(QTableWidget.SelectRows)
        self.tableWidget.selectionModel().selectionChanged.connect(
            self.on_selection_changed
        )

    @pyqtSlot()
    def on_selection_changed(self):
        self.btndelete.setEnabled(
            bool(self.tableWidget.selectionModel().selectedRows())
        )

    def deleteRow(self):
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())