Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QIcon crop when drag and display it in a QScrollArea
Forum Updated to NodeBB v4.3 + New Features

QIcon crop when drag and display it in a QScrollArea

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 58 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Magico
    wrote last edited by
    #1

    Hello
    I want to make a drag and drop system with .exe application.
    The application look like this for now :
    86566a95-d4dc-4966-a487-1d8bd1d03685-{119F0717-F3CC-43DB-A109-F372908EB912}.png

    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:
    87f0f4f9-7636-4ea7-9224-3b54624ad0ab-{49EAC160-19D1-456A-B182-0521BD2BFAEC}.png
    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
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved