Reputation: 1
I am building an application for my end-of-study project, in this application the first window contains keywords, the second contains file names, the third is a graphical workspace and the last is a console.
The problem I have is that I can't do a drag and drop to override the elements of the first two windows in the graphic space, so I tried by double clicking; while I found a solution the elements appear in space but I cannot move or select them.
So, my question is: how to deal with QGraphicsScene?
This is the main class:
import sys
import json
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QVBoxLayout, QSplitter, QWidget, QListWidget, QTextEdit, QLabel
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtWidgets import *
from CustomWidget import CustomWidget
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Application PyQt5")
self.setGeometry(100, 100, 800, 600)
self.current_x = 0
self.current_y = 0
self.keywords = []
self.scripts = []
self.view1 = QGraphicsView()
self.scene1 = QGraphicsScene()
self.view1.setScene(self.scene1)
self.keywordList = QListWidget()
self.fileListe = QListWidget()
self.console = QTextEdit()
top_layout = QSplitter(Qt.Horizontal)
top_layout.addWidget(self.keywordList)
top_layout.addWidget(self.view1)
bottom_layout = QSplitter(Qt.Horizontal)
bottom_layout.addWidget(self.fileListe)
bottom_layout.addWidget(self.console)
layout = QVBoxLayout()
layout.addWidget(top_layout, stretch=3)
layout.addWidget(bottom_layout, stretch=1)
self.keywordList.setDragEnabled(True)
self.fileListe.setDragEnabled(True)
self.view1.setSceneRect(QRectF(0, 0, self.view1.width(), self.view1.height()))
self.view1.setAcceptDrops(True)
self.custom_widget = CustomWidget(self.view1)
central_widget = CustomWidget(self.view1)
central_widget.setLayout(layout)
central_widget.view1 = self.view1
self.setCentralWidget(central_widget)
self.load_data()
self.keywordList.itemDoubleClicked.connect(self.add_label_to_scene)
self.fileListe.itemDoubleClicked.connect(self.add_label_to_scene)
def load_data(self):
self.keywordList.clear()
self.fileListe.clear()
with open('keywords.json', 'r') as f:
data = json.load(f)
self.keywords = data['keywords']
self.scripts = data['script']
for keyword_data in self.keywords:
keyword = keyword_data['keyword']
self.keywordList.addItem(keyword)
for script_data in self.scripts:
file_name = script_data['file_name']
self.fileListe.addItem(file_name)
def add_label_to_scene(self, item):
text = item.text()
label = QLabel(text)
label.setStyleSheet("border: 1px solid black; padding: 10px;")
proxy = self.view1.scene().addWidget(label)
self.custom_widget.enable_draggable(proxy)
proxy.setPos(self.current_x, self.current_y)
self.current_x += 50
self.current_y += 50
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
And this is the CustomWidget class:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class CustomWidget(QWidget):
def __init__(self, view1):
super().__init__()
self.view1 = view1
self.painter = QPainter(self)
self.painter.drawRect(self.rect())
def enable_draggable(self, item):
item.setFlag(QGraphicsItem.ItemIsMovable, True)
def mousePressEvent(self, event):
print("Left mouse button pressed.")
self.checkItemsUnderMouse(event.pos())
event.accept()
def checkItemsUnderMouse(self, pos):
print("Element clicked:")
for item in self.view1.scene().items():
if isinstance(item, QGraphicsProxyWidget):
widget = item.widget()
if isinstance(widget, QLabel):
print("Text:", widget.text())
print("Position:", item.pos())
def dragMoveEvent(self, event):
event.setAccepted(True)
def dragLeaveEvent(self, event):
self.update()
I tried to change all the windows to QGraphicsScene but it didn't work.
Upvotes: 0
Views: 44