Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag pixmap with semi-transparency?
Forum Updated to NodeBB v4.3 + New Features

Drag pixmap with semi-transparency?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 525 Views
  • 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.
  • S Offline
    S Offline
    Shiny
    wrote on last edited by Shiny
    #1

    I've looked and looked, and while there are a few threads about this, they never seem to come to any conclusion on how to do it properly: I want to set the drag pixmap for a drag-and-drop operation to be a semi-transparent image.

    In my contrived example, I am just trying to draw a rectangle with a 50% transparent border as the drag image, but it does not actually appear transparent when dragged over imagery behind it (the background does not show through the border). Here's a couple bits of python code (python2.7, Qt5.14), one attempt with QPixmap, two with QImage:

    def startDrag(self, supportedActions):
        pixmap = QtGui.QPixmap(120, 60)
        pixmap.fill(QtCore.Qt.transparent)
        painter = QtGui.QPainter(pixmap)
        pen = QtGui.QPen(QtGui.QColor(255,0,0,127))
        pen.setWidth(15)
        painter.setPen(pen)
        painter.drawRect(img.rect())
        painter.end()
    
        drag = QtGui.QDrag(self)
        mimeData = QtCore.QMimeData()
        mimeData.setText("mimedata")
        drag.setMimeData(mimeData)
        drag.setPixmap(pixmap)
        drag.exec_(supportedActions, QtCore.Qt.MoveAction)
    

    and here's another attempt using QImage and setting the format explicitly to ARGB:

    def startDrag(self, supportedActions):
        img = QtGui.QImage(120, 60,
                                QtGui.QImage.Format_ARGB32_Premultiplied)
        img.fill(QtCore.Qt.transparent)
        painter = QtGui.QPainter(img)
        pen = QtGui.QPen(QtGui.QColor(255,0,0,127))
        pen.setWidth(15)
        painter.setPen(pen)
        painter.drawRect(img.rect())
        painter.end()
    
        drag = QtGui.QDrag(self)
        mimeData = QtCore.QMimeData()
        mimeData.setText("mimedata")
        drag.setMimeData(mimeData)
        drag.setPixmap(QtGui.QPixmap.fromImage(img))
        drag.exec_(supportedActions, QtCore.Qt.MoveAction)
    

    and one final attempt using painter.setOpacity() to try and draw transparent:

    def startDrag(self, supportedActions):
        img = QtGui.QImage(120, 60,
                                QtGui.QImage.Format_ARGB32_Premultiplied)
        img.fill(QtCore.Qt.transparent)
        painter = QtGui.QPainter(img)
        painter.setOpacity(0.5)
        pen = QtGui.QPen(QtGui.QColor(255,0,0,255))
        pen.setWidth(15)
        painter.setPen(pen)
        painter.drawRect(img.rect())
        painter.end()
    
        drag = QtGui.QDrag(self)
        mimeData = QtCore.QMimeData()
        mimeData.setText("mimedata")
        drag.setMimeData(mimeData)
        drag.setPixmap(QtGui.QPixmap.fromImage(img))
        drag.exec_(supportedActions, QtCore.Qt.MoveAction)
    

    none of the above produce a drag image that is 50% translucent and allows the background to show through. Though they do draw an image which is at 50% intensity over white, meaning the red is being attenuated as if it was drawn 50% transparent over white...but not actually transparency. Also it's worth noting that the fully transparent area inside the rectangle does allow the background to show through, so there's definitely an alpha, or at least a mask.

    I read somewhere this might have to do with the windowing system not supporting alpha in the way drag images are drawn? I am on linux here...so maybe someone could explain if that's the issue?

    Thanks for any hints.

    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