How I can draw shadows on the QQuickPaintedItem?



  • Hi everyone!

    I have a UI element as a subclass of QQuickPaintedItem. This class must draw a some datas using paint "method". How I can draw shadow for this element? For now I create a QML file which draw shadow using "DropShadow" element but I have a problem when my element has a subviews. I need to drag and drop functions. If I add a MouseArea I can't detect click to child. Could you tell me best solution for this?

    If I use QQuickPaintedItem I have a two problem:

    I don't know how to draw shadows using QPainter. I can draw a black rectangle as a background but how I can apply blur etc?

    I can't turn on drag and croup for item.

    My *.h file:
    @
    class MslViewItem : public QQuickPaintedItem
    {
    Q_OBJECT
    Q_CLASSINFO("version", "1.0.0")

        Q_PROPERTY(MslElement* element READ getElement WRITE setElement NOTIFY elementChanged)
        Q_PROPERTY(QList<MslViewItem*> subviews READ getSubviews NOTIFY subviewsChanged)
    
    private:
        MslElement* m_element;
        QList<MslViewItem*> m_subviews;
    
        void loadViewSettings();
    public:
        explicit MslViewItem(QQuickPaintedItem *parent = 0);
        MslViewItem(const MslViewItem& obj);
        MslViewItem& operator=(const MslViewItem& obj);
        MslViewItem(MslElement* elem, QQuickPaintedItem *parent = 0);
    
        virtual void paint(QPainter *painter);
        virtual void dragMoveEvent(QDragMoveEvent * event);
    
        inline MslElement* getElement(){return m_element;}
        inline QList<MslViewItem*> getSubviews(){return m_subviews;}
        inline void setElement(MslElement* obj){m_element = obj; loadViewSettings(); emit elementChanged(m_element);}
    signals:
        void elementChanged(MslElement* obj);
        void subviewsChanged(QList<MslViewItem*> list);
    public slots:
    

    };
    QML_DECLARE_TYPE(MslViewItem)
    @

    My *.cpp file
    @
    void MslViewItem::loadViewSettings()
    {
    setFillColor(Qt::transparent);

    if(m_element)
    {
        QVariant frame = m_element->propertyByKey("frame");
        if(!frame.isNull() && frame.canConvert<QVariantMap>())
        {
            QVariantMap map = frame.toMap();
            setWidth(map["width"].toFloat());
            setHeight(map["height"].toFloat());
        }
    }
    

    }

    MslViewItem::MslViewItem(QQuickPaintedItem *parent) :
    QQuickPaintedItem(parent)
    {
    m_element = NULL;
    m_subviews.clear();

    setFlag(ItemHasContents, true);
    setFlag(ItemClipsChildrenToShape, true);
    setFlag(ItemAcceptsDrops, true);
    setRenderTarget(QQuickPaintedItem::InvertedYFramebufferObject);
    setParentItem(parent);
    

    }

    MslViewItem::MslViewItem(MslElement* elem, QQuickPaintedItem *parent) :
    QQuickPaintedItem(parent)
    {
    m_element = elem;
    m_subviews.clear();

    setFlag(ItemHasContents, true);
    setFlag(ItemClipsChildrenToShape, true);
    setFlag(ItemAcceptsDrops, true);
    setRenderTarget(QQuickPaintedItem::InvertedYFramebufferObject);
    setParentItem(parent);
    
    loadViewSettings();
    

    }

    MslViewItem::MslViewItem(const MslViewItem& obj)
    {
    m_element = obj.m_element;
    m_subviews = obj.m_subviews;
    }

    MslViewItem& MslViewItem::operator=(const MslViewItem& obj)
    {
    try
    {
    m_element = obj.m_element;
    m_subviews = obj.m_subviews;

        return *this;
    }
    catch(std::exception& exception)
    {
        Q_UNUSED(exception)
    
        return *this;
    }
    

    }

    void MslViewItem::paint(QPainter *painter)
    {
    //Some draw code here
    }

    void MslViewItem::dragMoveEvent(QDragMoveEvent * event)
    {
    Q_UNUSED(event)
    }
    @

    use in qml:
    @
    MslViewItem {
    id: viewItem
    anchors.centerIn: parent
    element: viewRoot.element

             Repeater {
                 id: subviewsRepeater
                 anchors.fill: parent
                 model: (element !== null) ? element.toSubViewsList() : 0
                 delegate: Loader {
                     id: subviewLoader
    
                     function subviewSelected(obj)
                     {
                         viewRoot.clicked(obj);
                     }
    
                     Component.onCompleted: {
                         var obj = element.subviewByKey(element.toSubViewsList()[index]);
                         if(obj !== null)
                         {
                             var options = {appRoot: viewRoot.appRoot,
                                            element: obj};
                             subviewLoader.setSource("MslQmlViewItem.qml", options);
                         }
                     }
    
                     onLoaded: {
                         if(subviewLoader.item !== null)
                         {
                             subviewLoader.item.clicked.connect(subviewSelected);
                         }
                     }
                 }
             }
         }
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.