QPixmap or QImage exchange the buffers



  • Hello, I am interested in the following topic: Can I exchange the QPixmap/QImage buffers or source file? Consider this small code:
    @
    static inline void CellOnPaint(QPainter* qp,
    const QStyleOptionGraphicsItem* qsi,
    QWidget* wd, QGraphicsItem* ari ) {
    MY_UNUSED(qp,qsi,wd,ari)
    QRectF rec = ((ActionItem*)ari)->boundingRect();
    QPixmap door = ((ActionItem)ari)->getMyPixmap();
    QImage eframe = door->toImage();
    qp->drawImage(rec, eframe);
    wd->update(); //needed
    }
    @

    WHere:

    @
    getMyPixmap()
    @

    is rturning a private member from my custom ActionItem class. The member is set in the construction, kind of

    @
    ActionItem* a1 = new ActionItem(QPixmap(":/res/p1.png"...));
    @

    @
    a1->setNewPixmap(":/res/p2.png");
    @

    Now the call to CellOnPaint will display "p2.png". BUt how to exchange the buffers?



  • Hi,

    I didn't understand what you meant by exchanging buffers. Where is a buffer in the code you have provided?



  • Maybe I post it bad. Consider the CellOnPaint. See line 6. It assigns per call the ref from ActionItems private pixmap, which is set in the constructor and is known. I want to just exchange the pixmap. Ive wrote setNewQPix(QPix...) but on the call the program fails.



  • Check out the QPixmap constructors. If you want to pass in a file path, use the constructor that accepts a QString. Note there is another constructor that accepts const char* but that one expects data not a path.

    Try a1->setNewQPixmap( QPixmap(QString( ":/...png") ) ).



  • Let me show you the full code of the setQpix...
    @
    static inline void SwapOn(QGraphicsSceneMouseEvent* me,
    QGraphicsItem* ari) {
    ((ActionItem*)ari)->setMyPixmap(
    MainWindow::getActionItemByName("Wolf1")->getPixmapRes()
    );
    }
    @
    Sorry, its a bit hard to read, I suppose, but the idea is qute, simple, take the QGaraphics item clicked, and set its pixmap to another QGraphics derivate found by name from a heap simulation list of items. I`ll give a try to your suggestion.



  • I am sorry but you are not showing me anything here:

    • What do you pass in to that function? What does getPixmapRes() mean? get Pixmap Resolution? What does it return?
    • How is the setNewPixmap() or setMyPixmap() or setQpix... is implemented?
    • What does it mean that the program fails? Does it crash? Does it fail to compile? Does it fail to link?

    Here is how a QPixmap is "updated":http://doc.qt.io/qt-5/qpixmap.html#QPixmap-6:
    @
    QPixmap a;
    QPixmap b;
    b = a;
    @



  • Sorry I kind of suck at forums :)
    @
    void ActionItem::setMyPixmap(QString s) {
    pixmap = new QPixmap(s);
    }

    QPixmap* ActionItem::getMyPixmap()
    {
    return pixmap;
    }
    @
    Let me give a try to your suggestion and I`ll write back. Probably your help was enough. Hope so. :)

    [quote author="ckakman" date="1421180662"]I am sorry but you are not showing me anything here:

    • What do you pass in to that function? What does getPixmapRes() mean? get Pixmap Resolution? What does it return?
    • How is the setNewPixmap() or setMyPixmap() or setQpix... is implemented?
    • What does it mean that the program fails? Does it crash? Does it fail to compile? Does it fail to link?

    Here is how a QPixmap is "updated":http://doc.qt.io/qt-5/qpixmap.html#QPixmap-6:
    @
    QPixmap a;
    QPixmap b;
    b = a;
    @[/quote]



  • You don't need a pointer to QPixmap as member variable. QPixmap is implicitly shared.

    @
    private:
    QPixmap pixmap;
    @

    If you somehow have to use a pointer, than you have a memory leak there. Delete the pixmap before assigning a new one.


Log in to reply
 

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