Repaint of QQuickPaintedItem based object problem



  • I've created simple custom class. But I have problem with redrawing on Linux Ubuntu. It works fine on windows. update() is called very often. About each 20-30 ms. But method main(..) was not called. It is called only when I click on resize button.

    #ifndef BATTLECANVAS_H
    #define BATTLECANVAS_H
    
    #include "Controller/IContext.h"
    
    #include <QQuickPaintedItem>
    #include <QColor>
    #include <QImage>
    #include <QPixmap>
    
    #include <memory>
    #include <mutex>
    
    class BattleCanvas : public QQuickPaintedItem
    {
        Q_OBJECT
        Q_PROPERTY(QColor _background READ getBackground WRITE setBackground)
    
    public:
        BattleCanvas();
    
        virtual void paint(QPainter *painter) override;
        QColor getBackground() { return _background; }
        void setBackground(const QColor& color) { _background = color; }
    
        void drawScene(IContextPtr pContext);
    
    private:
        void drawMap(const BattleMap& map);
        void drawGrid(QPainter& painter, const QSize& sz, unsigned int step);
    
    private:
        QColor _background;
        std::mutex _mutex;
        std::unique_ptr<QImage> _tempImage;
        QPixmap _brickPixmap;
        QPixmap _stonePixmap;
    };
    
    typedef std::shared_ptr<BattleCanvas> BattleCanvasPtr;
    
    #endif // BATTLECANVAS_H
    
    

    And implementation. I think it has no sense to show all code because it just implements some drawings to QImage object.

    #include "UI/impl/BattleCanvas.h"
    
    #include <QBrush>
    #include <QPainter>
    #include <QSizeF>
    
    BattleCanvas::BattleCanvas()
    {
        setFlag(QQuickItem::ItemHasContents, true);
        _brickPixmap.load("data/images/brick.png");
        _stonePixmap.load("data/images/stone.png");
    }
    
    void BattleCanvas::paint(QPainter *painter)
    {
        std::lock_guard<std::mutex> lock(_mutex);
        QBrush brush(_background);
    
        painter->setBrush(brush);
        painter->setPen(Qt::NoPen);
        painter->setRenderHint(QPainter::Antialiasing);
    
        painter->drawRect(0, 0, boundingRect().width(), boundingRect().height());
        if (_tempImage)
            painter->drawImage(30, 30, *_tempImage);
    }
    
    void BattleCanvas::drawScene(IContextPtr pContext)
    {
        std::lock_guard<std::mutex> lock(_mutex);
        drawMap(pContext->getBattleMap());
    
        update();
    }
    //And so on...
    

Log in to reply
 

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