QIcon crop when drag and display it in a QScrollArea
-
Hello
I want to make a drag and drop system with .exe application.
The application look like this for now :
So, I want to take application in the left scroll area and drag it in the scroll area of the different detected screens.
The system of drag and drop and receive my application object, that contains the icon in QIcon, works.
But, when I drop my application in a QScrollArea(), the icon is cropping like in the picture below:
I tried some little things to avoid that but the same weird crop. How can I solve it ?
The part of the code of a screen area :class QScreenApplication(QWidget): def __init__(self, screen: QScreen): super().__init__() self.setAcceptDrops(True) self.applications: List[Application] = [] # Store all the applications drag in the screen self.init_UI(screen) def init_UI(self, screen: QScreen): main_layout = QVBoxLayout() # Scrollable area for application icons self.scroll_area = QScrollArea() self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded) # Container widget for icons to have the layout left-aligned self.icons_widget = QWidget() self.icons_layout = QHBoxLayout() self.icons_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) self.icons_widget.setLayout(self.icons_layout) self.scroll_area.setWidget(self.icons_widget) main_layout.addWidget(self.scroll_area, 85) screen_name = screen.name() # Detect if the screen is a laptop screen (name starts with \\) if screen_name.startswith(r"\\"): screen_name = "Laptop Screen" screen_name_label = QLabel(f"Screen: {screen_name}") main_layout.addWidget(screen_name_label, 15) self.setLayout(main_layout) def add_application_icon(self, application: Application): """ Add the icon of the application to the scroll area :param application: Application received from the drag and drop """ icon_label = QLabel() icon_label.setFixedSize(32, 32) icon_label.setToolTip(application.name) # Show the name on hover # Prevent icon cropping icon_label.setScaledContents(True) # Allow the image to fit the label size icon_label.setAlignment(Qt.AlignmentFlag.AlignCenter) # Center the icon if application.icon: pixmap = application.icon.pixmap(32, 32) if not pixmap.isNull(): # Resize the pixmap to fit in the QLabel scaled_pixmap = pixmap.scaled(32, 32, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation) icon_label.setPixmap(scaled_pixmap) else: QMessageBox.warning( self, "Icon Error", f"The application {application.name} does not have a valid icon." ) self.applications.remove(application) return icon_label.setPixmap(application.icon.pixmap(32, 32)) else: QMessageBox.warning( self, "Icon Error", f"The application {application.name} does not have a valid icon." ) self.applications.remove(application) # Remove the application if it has no icon return # Add the icon to the layout of the scroll area self.icons_layout.addWidget(icon_label) #region Drag and Drop Events
-
Hi and welcome to devnet,
Why are you doing all these scaling operation to, in the end, not used that scaled pixmap ?
In any case, you should start by making it work without particular scaling or fixed size. Once you have ensured that you have the icon properly working, you can start scaling the icon, and only once you are sure you have what you want, fix the size of the QLabel.
That said, why not use a QListView/QListWidget to show these icon.