我创建了一个QListWidget ... QListWidgetItem由QHBoxLayout中对齐的QPushButton和QLineEdit组成。
QListWidgetItem中的QPushButton链接到应在单击时从QListWidget中删除当前QListWidgetItem的函数。
我正在使用方法“ takeItem()”向其传递方法“ currentRow()”的输出以删除条目...
问题是,当我单击删除按钮时,未选择QListWidgetItem,因此“ currentRow()”什么也不返回...
我的问题:点击删除按钮后,如何使QListWidgetItem条目被选中...
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_())
答案 0 :(得分:3)
您必须订购代码,最好的方法是使用类。在这种情况下,小部件(QLineEdit + QPushButton)必须是一个类,并通过属于该类的信号来显示按钮的单击信号。
以相同的方式创建另一个处理QListWidget的类,以获取按钮的行,任务是使用几何,在这种情况下,我们将使用sender()获得小部件(sender()返回对象发出信号),然后使用mapToGlobal()获得该小部件的左上角相对于屏幕的位置,并使用QToWidget的viewport()将全局位置转换为局部位置mapFromGlobal(),然后使用局部位置,我们使用itemAt()获得该项目,并且使用该项目,任务很简单。
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_())
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_())