0

I am looking for a way to connect the QPixmap and the data being written on it, so that in the end I can have an accurate record of what is currently on the screen.

void QPainter::drawRects(const QRectF *rects, int rectCount)

This is an example function from qpainter.cpp (found in ./src/gui/painting in the Qt SDK) What I am trying to get is the Pixmap that the rectangle is being written to (in the case when device()->devType() is a Pixmap). So I have this check:

if(this->device()->devType() == QInternal::Pixmap) {
    const QPixmap *Pixmap = dynamic_cast<const QPixmap *>(this->device());
    qDebug() << "rect2pixmap - address: "<<Pixmap<<" - key: "<<Pixmap.cacheKey();
}

I tried finding the memory address of the Qpixmap and checking it against the memory addresses of the QPixmaps being written to the screen.

void QPainter::drawPixmap(const QPointF &p, const QPixmap &pm)
{
    const QWidget *widget = dynamic_cast<const QWidget *>(this->device());
    qDebug() << "pixmap - address: "<<&pm << " - key: "<<pm.cacheKey();
    [...]

But the addresses are all different. I also tried using cachekeys, but I faced the same problem.

Here's a sample console output:

rect2pixmap - address:  0x7fffac1dc9c0  - key: 72057594037927937
rect2pixmap - address:  0x7fffac1d9f80  - key: 72057598332895233 
rect2pixmap - address:  0x7fffac1dcd60  - key: 72057602627862529
rect2pixmap - address:  0x7fffac1dcd60  - key: 72057606922829825
rect2pixmap - address:  0x7fffac1dd100  - key: 72057611217797121
rect2pixmap - address:  0x7fffac1df2e0  - key: 72057615512764417 
pixmap - address:  0x7fffac1dffa0  - key:  197568495616 
pixmap - address:  0x7fffac1e0170  - key:  214748364800 
pixmap - address:  0x7fffac1e0340  - key:  231928233984 
pixmap - address:  0x7fffac1e0510  - key:  249108103168 
pixmap - address:  0x7fffac1e06e0  - key:  261993005056
rect2pixmap - address:  0x7fffac1dadb0   - key: 72057619807731713 

So what I am asking is there anyway to link between the pixmap that the rect is being written to and the pixmap that is being printed later on to the screen? And can I safely assume that the rects aren't being shown on the screen if no Pixmaps are ever called?

If anything isn't clear abut the question, please tell me, and I'll try to clarify as best as I can.

Thank you.

Raghd Hamzeh
  • 314
  • 10
  • 17
  • So you want to draw something onto some QPixmaps and then draw the QPixmaps onto a painter? – Stephen Chu Apr 29 '12 at 15:39
  • I'm trying to keep track of whatever is being drawn to the screen. Using widgets and internal elements I can get that, but with Pixmaps, it becomes trickier, since I can't map elements globally, and because pixmaps are usually drawn later to the screen. So any rect, or line drawn to a Pixmap would pose a problem because a) I do not know when it is actually drawn to the screen, and b) I do not know where it's global position lies. – Raghd Hamzeh Apr 29 '12 at 16:42
  • 1
    If the goal is to track and link individual graphic elements to your data, you may want to consider a different approach. I would use QGraphicsScene and QGraphicsItems for that. – Stephen Chu Apr 29 '12 at 17:45

1 Answers1

1

The simpler way as mentioned by @Stephen Chu is using QGraphicsScene.

Here is an example that prints with qdebug when you click on a rectangle.

clickrect.hpp

#ifndef _HANDLER_HPP_
#define _HANDLER_HPP_

#include <QtGui>

class ClickRect : public QGraphicsRectItem
{
    public:
        ClickRect(
            const QRect& rect,
            const QColor& color,
            const QString& name,
            QGraphicsItem* parent = 0);

    protected:
        virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);

    private:
        QString mName;
};

#endif

clickrect.cpp

#include "clickrect.hpp"

ClickRect::ClickRect(
    const QRect& rect,
    const QColor& color,
    const QString& name,
    QGraphicsItem* par
) :
    QGraphicsRectItem(rect, par),
    mName(name)
{
    setBrush(QBrush(color));
}

void ClickRect::mousePressEvent(QGraphicsSceneMouseEvent* ev)
{
    qDebug() << mName;
}

test.cpp

#include <QtGui>
#include "clickrect.hpp"

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.show();

    ClickRect rect_a(QRect(0, 0, 30, 30), QColor("#FDD"), "A");
    ClickRect rect_b(QRect(40, 0, 30, 30), QColor("#DFD"), "B");

    scene.addItem(&rect_a);
    scene.addItem(&rect_b);


    return app.exec();
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

find_package(Qt4 4.8 REQUIRED QtCore QtGui)
include(${QT_USE_FILE})

add_executable(test
    test.cpp
    clickrect.hpp
    clickrect.cpp
    ${MOC_FILES})
target_link_libraries(test ${QT_LIBRARIES})
Community
  • 1
  • 1
Silas Parker
  • 8,017
  • 1
  • 28
  • 43