0

我想拖出QListWidgetItem一个QListWidget。我正在制作一个程序,通过拖动球员(带有像素图的标签)来创建足球阵容。我想将球员从“替补席”拖到“外场”,用户可以在足球队形中自由拖动他们:

启动程序:
程序开始

在“替补席”和“外场”之间拖拽球员:
在左侧和右侧之间拖动玩家

我考虑过为“外场”球员使用带有像素图的标签,但我想知道是否可以使用QListWidgetItemout of QListWidget. 有一个更好的方法吗?

代码:

import json
import os
import sys

from PyQt5 import QtGui
from PyQt5.Qt import QPixmap
from PyQt5.QtCore import QPoint, Qt, QSize
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QListWidget, QListWidgetItem


class MainWindow(QMainWindow):
    def __init__(self, *args, obj=None, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        class MovableLabel(QLabel):
            """WToolBar is a personalized toolbar."""

            homeAction = None

            oldPos = QPoint()

            def __init__(self, mainWindow : MainWindow, filename: str):
                super().__init__(mainWindow)
                self.mainWindow = mainWindow
                self.filename = filename
                self.clicked = False

            def mousePressEvent(self, evt):
                """Select the toolbar."""
                self.oldPos = evt.globalPos()
                if not self.clicked:
                    globalPos = self.mapToGlobal(self.pos())
                    self.setParent(self.mainWindow)
                    self.move(self.mapFromGlobal(globalPos))
                    self.raise_()
                    self.show()
                    self.clicked = True
                    self.grabMouse()

            def mouseMoveEvent(self, evt):
                """Move the toolbar with mouse iteration."""
                delta = QPoint(evt.globalPos() - self.oldPos)
                self.move(self.x() + delta.x(), self.y() + delta.y())
                self.oldPos = evt.globalPos()

            def mouseReleaseEvent(self, ev: QtGui.QMouseEvent) -> None:
                self.releaseMouse()
                self.clicked = False
                if ev.globalPos().x() < listWidget.width():
                    if self in self.mainWindow.players:
                        self.mainWindow.players.remove(self)
                if self not in self.mainWindow.players:
                    self.mainWindow.players.append(self)

        self.players = []
        pixmap = QPixmap()

        class MyItem(QListWidgetItem):
            def __init__(self, label : MovableLabel):
                super(MyItem, self).__init__()
                self.label = label

        class MyListWidget(QListWidget):
            def __init__(self, mainWindow : MainWindow):
                super(MyListWidget, self).__init__(mainWindow)
                self.mainWindow = mainWindow

            def mouseReleaseEvent(self, e: QtGui.QMouseEvent) -> None:
                item = self.selectedItems()[0]
                imageSize = item.icon().actualSize(QSize(100, 200))
                self.removeItemWidget(item)
                label = MovableLabel(self.mainWindow, 'Pogba.jpg')
                pixmap = item.icon().pixmap(imageSize)
                label.setPixmap(pixmap)
                label.setFixedSize(imageSize)
                label.move(e.globalPos())
                label.show()

        listWidget = MyListWidget(self)
        listWidget.setViewMode(QListWidget.IconMode)
        listWidget.setFixedSize(500, 700)
        listWidget.setIconSize(QSize(100, 200))
        listWidget.setDragDropMode(listWidget.InternalMove)


        '''json file contains positions of "outfield" players'''
        try:
            with open('players.txt', 'r') as file:
                data = json.load(file)
        except:
            data = []

        dir = 'Players/'

        for filename in os.listdir(dir):
            pixmap = QPixmap(dir + filename).scaledToHeight(100, Qt.SmoothTransformation)
            label = MovableLabel(self, filename)
            label.setPixmap(pixmap)
            label.setFixedSize(pixmap.width(), pixmap.height())

            playersIndex = next((i for i, player in enumerate(data) if player['filename'] ==
                                 filename), None)
            if playersIndex == None:
                item = QListWidgetItem(QIcon(dir + filename), '<Name>', listWidget)
            else:
                label.setParent(self)
                label.move(data[playersIndex]['x'], data[playersIndex]['y'])
                self.players.append(label)
        self.setStyleSheet("background-color: black;")
        self.showMaximized()

    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        with open('players.txt', 'w') as outfile:
            json.dump([{'filename': player.filename, 'x': player.x(), 'y': player.y()} for player in self.players],
                      outfile, indent= 4)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    app.exec_()

编辑:

我尝试在拖出时使用带有 pixmap 的标签来实现QListWidgetItemQListWidget但是 listWidgetItem 不会从 listWidget 中消失,我无法让标签与项目原样对齐(我只是将标签移动到光标在哪里),我不确定如何将播放器替换为 listWidget。项目被拖出listWidget时会触发什么事件?任何反馈将不胜感激。

4

0 回答 0