Qt窗口嵌入

来源:互联网 发布:中金所待遇 知乎 编辑:程序博客网 时间:2024/05/17 20:29

创建一个QGraphicsProxyWidget 的子类CustomProxy 

class CustomProxy : public QGraphicsProxyWidget 

在子类中重写事件

#ifndef CUSTOMPROXY_H#define CUSTOMPROXY_H#include <QTimeLine>#include <QGraphicsProxyWidget>class CustomProxy : public QGraphicsProxyWidget{    Q_OBJECTpublic:    explicit CustomProxy(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);    QRectF boundingRect() const Q_DECL_OVERRIDE;    void paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option,                          QWidget *widget) Q_DECL_OVERRIDE;protected:    void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE;    bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) Q_DECL_OVERRIDE;    QVariant itemChange(GraphicsItemChange change, const QVariant &value) Q_DECL_OVERRIDE;private slots:    void updateStep(qreal step);    void stateChanged(QTimeLine::State);    void zoomIn();    void zoomOut();private:    QTimeLine *timeLine;    bool popupShown;    QGraphicsItem *currentPopup;};#endif // CUSTOMPROXY_H

窗口添加阴影效果

void CustomProxy::paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option,                                   QWidget *widget){    const QColor color(0, 0, 0, 64);    QRectF r = windowFrameRect();    QRectF right(r.right(), r.top() + 10, 10, r.height() - 10);    QRectF bottom(r.left() + 10, r.bottom(), r.width(), 10);    bool intersectsRight = right.intersects(option->exposedRect);    bool intersectsBottom = bottom.intersects(option->exposedRect);    if (intersectsRight && intersectsBottom) {        QPainterPath path;        path.addRect(right);        path.addRect(bottom);        painter->setPen(Qt::NoPen);        painter->setBrush(color);        painter->drawPath(path);    } else if (intersectsBottom) {        painter->fillRect(bottom, color);    } else if (intersectsRight) {        painter->fillRect(right, color);    }    QGraphicsProxyWidget::paintWindowFrame(painter, option, widget);}

窗口变形

void CustomProxy::updateStep(qreal step){    QRectF r = boundingRect();    setTransform(QTransform()                 .translate(r.width() / 2, r.height() / 2)                 .rotate(step * 30, Qt::XAxis)                 .rotate(step * 10, Qt::YAxis)                 .rotate(step * 5, Qt::ZAxis)                 .scale(1 + 1.5 * step, 1 + 1.5 * step)                 .translate(-r.width() / 2, -r.height() / 2));}


使用 QGraphicsProxyWidget 加载需要嵌入的窗口

<pre name="code" class="cpp">QGraphicsScene scene.addItem(proxy);QGraphicsView view(&scene);view.show();


效果:


详细请查看Qt例子中的源码:embeddeddialogs

0 0