问题
I created a QListWidget... The QListWidgetItem composes of a QPushButton and QLineEdit aligned inside QHBoxLayout...
The QPushButton within the QListWidgetItem is linked to function that should delete the current QListWidgetItem from the QListWidget when it's clicked...
I am using the method "takeItem()" passing to it the output of the method "currentRow()" to delete the entry...
The problem is when I click on the delete button the QListWidgetItem is not selected, hence the "currentRow()" returns nothing...
My question: How can I make the QListWidgetItem entry get selected once I hit the delete button...
import sys
from PyQt4 import QtGui, QtCore
def Add_OtherItem():
ItemOther = CustomItem()
ItemOther.SetupItem(OthersCommandsWidget)
def Delete_OtherItem():
OthersCommandsWidget.takeItem(OthersCommandsWidget.currentRow())
app = QtGui.QApplication(sys.argv)
class CustomItem(object):
def SetupItem(self, OthersCommandList):
self.Item = QtGui.QListWidgetItem()
self.MainWidget = QtGui.QWidget()
self.CommandLine = QtGui.QLineEdit("")
self.DeleteButton = QtGui.QPushButton()
self.DeleteButton.setFixedSize(22, 22)
self.DeleteButton.clicked.connect(Delete_OtherItem)
self.ItemLayoutBox = QtGui.QHBoxLayout()
self.ItemLayoutBox.addWidget(self.CommandLine)
self.ItemLayoutBox.addWidget(self.DeleteButton)
self.MainWidget.setLayout(self.ItemLayoutBox)
self.Item.setSizeHint(self.MainWidget.sizeHint())
OthersCommandList.addItem(self.Item)
OthersCommandList.setItemWidget(self.Item, self.MainWidget)
AppWindow = QtGui.QMainWindow()
AppWindow.setWindowTitle("PoC ListWidget")
AppWindow.setFixedSize(550, 550)
TabWindow = QtGui.QTabWidget(AppWindow)
TabWindow.setGeometry(8, 30, 535, 505)
WorkTAB = QtGui.QWidget()
TabWindow.addTab(WorkTAB, 'Tab.01')
OthersCommandsWidget = QtGui.QListWidget(WorkTAB)
OthersCommandsWidget.setGeometry(QtCore.QRect(8, 40, 515, 430))
AddButton = QtGui.QPushButton(WorkTAB)
AddButton.setText("Add Item")
AddButton.setGeometry(QtCore.QRect(8, 8, 0, 0))
AddButton.setFixedSize(70, 22)
AddButton.clicked.connect(Add_OtherItem)
AppWindow.show()
sys.exit(app.exec_())
回答1:
You have to order your code, and the best way is to use classes. In this case the widget (QLineEdit + QPushButton) must be a class and expose the clicked signal of the button through a signal belonging to the class.
In the same way another class is created that handles the QListWidget, to obtain the row of the button the task is to use the geometry, in this case we will obtain the widget using sender() (sender() returns the object that issued the signal), and then the position of the top-left of that widget is obtained with respect to the screen using mapToGlobal(), that global position is converted to a local position with respect to the viewport() of the QListWidget using mapFromGlobal(), then using the local position we obtain the item using itemAt(), and with the item the task is simple.
PyQt4:
import sys
from PyQt4 import QtGui, QtCore
class Widget(QtGui.QWidget):
clicked = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
line_edit = QtGui.QLineEdit()
delete_button = QtGui.QPushButton("Delete Row")
hlay = QtGui.QHBoxLayout(self)
hlay.addWidget(line_edit)
hlay.addWidget(delete_button)
delete_button.clicked.connect(self.clicked)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("PoC ListWidget")
tab_widget = QtGui.QTabWidget()
work_tab = QtGui.QWidget()
tab_widget.addTab(work_tab, 'Tab.01')
self.others_commands_widget = QtGui.QListWidget()
add_button = QtGui.QPushButton("Add")
add_button.clicked.connect(self.add_other_command)
vlay = QtGui.QVBoxLayout(work_tab)
vlay.addWidget(add_button, alignment=QtCore.Qt.AlignLeft)
vlay.addWidget(self.others_commands_widget)
self.setCentralWidget(tab_widget)
@QtCore.pyqtSlot()
def add_other_command(self):
it = QtGui.QListWidgetItem()
self.others_commands_widget.addItem(it)
widget = Widget()
widget.clicked.connect(self.remove_other_command)
self.others_commands_widget.setItemWidget(it, widget)
it.setSizeHint(widget.sizeHint())
@QtCore.pyqtSlot()
def remove_other_command(self):
widget = self.sender()
gp = widget.mapToGlobal(QtCore.QPoint())
lp = self.others_commands_widget.viewport().mapFromGlobal(gp)
row = self.others_commands_widget.row(self.others_commands_widget.itemAt(lp))
t_it = self.others_commands_widget.takeItem(row)
del t_it
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
PyQt5:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
clicked = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
line_edit = QtWidgets.QLineEdit()
delete_button = QtWidgets.QPushButton("Delete Row")
hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(line_edit)
hlay.addWidget(delete_button)
delete_button.clicked.connect(self.clicked)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("PoC ListWidget")
tab_widget = QtWidgets.QTabWidget()
work_tab = QtWidgets.QWidget()
tab_widget.addTab(work_tab, 'Tab.01')
self.others_commands_widget = QtWidgets.QListWidget()
add_button = QtWidgets.QPushButton("Add")
add_button.clicked.connect(self.add_other_command)
vlay = QtWidgets.QVBoxLayout(work_tab)
vlay.addWidget(add_button, alignment=QtCore.Qt.AlignLeft)
vlay.addWidget(self.others_commands_widget)
self.setCentralWidget(tab_widget)
@QtCore.pyqtSlot()
def add_other_command(self):
it = QtWidgets.QListWidgetItem()
self.others_commands_widget.addItem(it)
widget = Widget()
widget.clicked.connect(self.remove_other_command)
self.others_commands_widget.setItemWidget(it, widget)
it.setSizeHint(widget.sizeHint())
@QtCore.pyqtSlot()
def remove_other_command(self):
widget = self.sender()
gp = widget.mapToGlobal(QtCore.QPoint())
lp = self.others_commands_widget.viewport().mapFromGlobal(gp)
row = self.others_commands_widget.row(self.others_commands_widget.itemAt(lp))
t_it = self.others_commands_widget.takeItem(row)
del t_it
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
来源:https://stackoverflow.com/questions/55546037/how-to-properly-remove-a-custom-item-from-a-qlistwidget-and-refresh-the-list