Skip to content

Commit 322ed06

Browse files
committed
Update
1 parent 2d9a667 commit 322ed06

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

examples/official/pdf/gui_barcode_reader_pyside6.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def run(self):
118118
class ImageDisplayWidget(QLabel):
119119
"""Custom widget for displaying and zooming images with barcode annotations."""
120120

121+
# Define signal for file drop
122+
file_dropped = Signal(str)
123+
121124
def __init__(self):
122125
super().__init__()
123126
self.setMinimumSize(400, 300)
@@ -261,18 +264,39 @@ def toggle_annotations(self, show):
261264
def dragEnterEvent(self, event: QDragEnterEvent):
262265
"""Handle drag enter events."""
263266
if event.mimeData().hasUrls():
264-
event.acceptProposedAction()
267+
# Check if any of the URLs are valid file types
268+
urls = event.mimeData().urls()
269+
for url in urls:
270+
file_path = url.toLocalFile()
271+
if file_path:
272+
ext = os.path.splitext(file_path)[1].lower()
273+
if ext in ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.tif', '.webp', '.pdf']:
274+
event.acceptProposedAction()
275+
# Change border to indicate drop zone is active
276+
self.setStyleSheet("border: 2px dashed #007acc; background-color: #e6f3ff; color: #666; font-size: 14px;")
277+
return
278+
event.ignore()
279+
280+
def dragLeaveEvent(self, event):
281+
"""Handle drag leave events."""
282+
# Restore normal styling when drag leaves
283+
if self.original_image is None:
284+
self.setStyleSheet("border: 2px dashed #ccc; background-color: #f9f9f9; color: #666; font-size: 14px;")
285+
else:
286+
self.setStyleSheet("border: 2px solid #ccc; background-color: white;")
265287

266288
def dropEvent(self, event: QDropEvent):
267289
"""Handle file drop events."""
290+
# Restore normal styling
291+
if self.original_image is None:
292+
self.setStyleSheet("border: 2px dashed #ccc; background-color: #f9f9f9; color: #666; font-size: 14px;")
293+
else:
294+
self.setStyleSheet("border: 2px solid #ccc; background-color: white;")
295+
268296
files = [url.toLocalFile() for url in event.mimeData().urls()]
269-
if files and hasattr(self.parent(), 'load_file_path'):
270-
# Get the main window and call load_file_path
271-
main_window = self.parent()
272-
while main_window and not hasattr(main_window, 'load_file_path'):
273-
main_window = main_window.parent()
274-
if main_window:
275-
main_window.load_file_path(files[0])
297+
if files:
298+
# Emit signal with the first dropped file
299+
self.file_dropped.emit(files[0])
276300
event.acceptProposedAction()
277301

278302
class ExportFormatDialog(QDialog):
@@ -329,6 +353,7 @@ def __init__(self):
329353
super().__init__()
330354
self.setWindowTitle("Dynamsoft Barcode Reader - PySide6 GUI")
331355
self.setGeometry(100, 100, 1400, 900)
356+
self.setAcceptDrops(True) # Enable drag-and-drop for main window
332357

333358
# Initialize variables
334359
self.cvr_instance = None
@@ -550,6 +575,8 @@ def create_image_display_panel(self):
550575

551576
self.image_widget = ImageDisplayWidget()
552577
self.image_widget.setMinimumSize(400, 300)
578+
# Connect drag-and-drop signal
579+
self.image_widget.file_dropped.connect(self.load_file_path)
553580
scroll_area.setWidget(self.image_widget)
554581

555582
layout.addWidget(scroll_area)

0 commit comments

Comments
 (0)