English 中文(简体)
• 如何利用具有活力的“漏洞”/带有QPainter的负面空间来吸引半透明超支?
原标题:How to draw a semi-transparent overlay with dynamic "holes"/negative space with QPainter?
  • 时间:2024-04-24 09:48:35
  •  标签:
  • qt
  • qt5
问题回答

与组成模式合作是可能的,但需要完全了解他们的行为(奇怪的是,我从来没有能够把自己的头包起来)。

此外,在绘制地图时,它常常需要进一步的步骤,即把一个层推到一个临时的“ffer”(另一个QPixmap或QImage)。

As long as the "masking" is simple, it doesn t involve alpha blending of multiple images, and the image is opaque (no transparency at all), we could just follow a simpler path, which is painting over and eventually "clear" the required areas by clipping the contents.

The procedure is actually easy:

  1. draw the pixmap;
  2. fill the whole area with the "shaded" color;
  3. create a QPainterPath() and add all areas we need to be "clear";
  4. use setClipPath() on the QPainter (with the Antialiasing render hint set);
  5. draw again the pixmap;

我不能在C++中写字,但以下简单的例子就足够清楚:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class ClipTest(QWidget):
    def __init__(self):
        super().__init__()
        # This is the original image used in the question
        self.pixmap = QPixmap( baseimage.png )
        self.setFixedSize(self.pixmap.size())

    def paintEvent(self, event):
        qp = QPainter(self)

        # draw the source image
        qp.drawPixmap(0, 0, self.pixmap)

        # draw the darker "shade" on the whole widget
        qp.fillRect(self.rect(), QColor(32, 32, 32, 127))

        path = QPainterPath()
        # add a couple of rounded rects, which are also colliding
        path.addRoundedRect(250, 110, 140, 140, 20, 20)
        path.addRoundedRect(380, 110, 140, 140, 20, 20)

        # to fill all enclosed regions, including those of colliding
        # sub paths that may be otherwise be unpainted
        path.setFillRule(Qt.WindingFill)

        # clip the painting on the path above
        qp.setClipPath(path)

        # for proper antialiased clipping
        qp.setRenderHint(qp.Antialiasing)

        # redraw the pixmap again
        qp.drawPixmap(0, 0, self.pixmap)


app = QApplication([])
win = Test()
win.show()
app.exec()

其结果如下:

“运行代码的缩略”,</a





相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签