निम्नलिखित काम कर सकते हैं। यह मूल रूप से किसी भी QPainter
पर अपनी पृष्ठभूमि को प्रस्तुत करने की क्षमता के साथ सामान्य QGraphicsScene
बढ़ाता है। फिर, आपके "कट आउट" ग्राफिक्स आइटम बस अन्य वस्तुओं के शीर्ष पर दृश्य पृष्ठभूमि प्रस्तुत करता है। इसके लिए काम करने के लिए, कट आउट आइटम में उच्चतम जेड-वैल्यू होना चाहिए।
#include <QtGui>
class BackgroundDrawingScene : public QGraphicsScene {
public:
explicit BackgroundDrawingScene() : QGraphicsScene() {}
void renderBackground(QPainter *painter,
const QRectF &source,
const QRectF &target) {
painter->save();
painter->setWorldTransform(
QTransform::fromTranslate(target.left() - source.left(),
target.top() - source.top()),
true);
QGraphicsScene::drawBackground(painter, source);
painter->restore();
}
};
class CutOutGraphicsItem : public QGraphicsEllipseItem {
public:
explicit CutOutGraphicsItem(const QRectF &rect)
: QGraphicsEllipseItem(rect) {
setFlag(QGraphicsItem::ItemIsMovable);
}
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) {
BackgroundDrawingScene *bgscene =
dynamic_cast<BackgroundDrawingScene*>(scene());
if (!bgscene) {
return;
}
painter->setClipPath(shape());
bgscene->renderBackground(painter,
mapToScene(boundingRect()).boundingRect(),
boundingRect());
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
BackgroundDrawingScene scene;
QRadialGradient gradient(0, 0, 10);
gradient.setSpread(QGradient::RepeatSpread);
scene.setBackgroundBrush(gradient);
scene.addRect(10., 10., 100., 50., QPen(Qt::SolidLine), QBrush(Qt::red));
scene.addItem(new CutOutGraphicsItem(QRectF(20., 20., 20., 20.)));
QGraphicsView view(&scene);
view.show();
return app.exec();
}
स्रोत
2012-06-15 20:30:01
एक कार्यक्रम मैं कहा जाता है का उपयोग किया है वास्तविक ड्रा क्या एक "को पीछे धकेलने" वस्तु कहा जाता है के साथ इस सामान्यीकृत। काफी उपयोगी, आप एक समान सामान्यीकरण का उपयोग करने पर विचार कर सकते हैं: http://www.mediachance.com/realdraw/help/index.html?pushback.htm – HostileFork