@@ -118,6 +118,9 @@ def run(self):
118
118
class ImageDisplayWidget (QLabel ):
119
119
"""Custom widget for displaying and zooming images with barcode annotations."""
120
120
121
+ # Define signal for file drop
122
+ file_dropped = Signal (str )
123
+
121
124
def __init__ (self ):
122
125
super ().__init__ ()
123
126
self .setMinimumSize (400 , 300 )
@@ -261,18 +264,39 @@ def toggle_annotations(self, show):
261
264
def dragEnterEvent (self , event : QDragEnterEvent ):
262
265
"""Handle drag enter events."""
263
266
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;" )
265
287
266
288
def dropEvent (self , event : QDropEvent ):
267
289
"""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
+
268
296
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 ])
276
300
event .acceptProposedAction ()
277
301
278
302
class ExportFormatDialog (QDialog ):
@@ -329,6 +353,7 @@ def __init__(self):
329
353
super ().__init__ ()
330
354
self .setWindowTitle ("Dynamsoft Barcode Reader - PySide6 GUI" )
331
355
self .setGeometry (100 , 100 , 1400 , 900 )
356
+ self .setAcceptDrops (True ) # Enable drag-and-drop for main window
332
357
333
358
# Initialize variables
334
359
self .cvr_instance = None
@@ -550,6 +575,8 @@ def create_image_display_panel(self):
550
575
551
576
self .image_widget = ImageDisplayWidget ()
552
577
self .image_widget .setMinimumSize (400 , 300 )
578
+ # Connect drag-and-drop signal
579
+ self .image_widget .file_dropped .connect (self .load_file_path )
553
580
scroll_area .setWidget (self .image_widget )
554
581
555
582
layout .addWidget (scroll_area )
0 commit comments