将QPixmaps与其上绘制的数据相关联

时间:2012-04-29 15:26:52

标签: qt qpixmap

我正在寻找一种方法来连接QPixmap和写在其上的数据,这样我最终可以准确记录屏幕上当前的内容。

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

这是qpainter.cpp的一个示例函数(在Qt SDK中的./src/gui/painting中找到) 我想要得到的是正在写入矩形的Pixmap(在device() - > devType()是Pixmap的情况下)。所以我有这个检查:

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

我尝试找到Qpixmap的内存地址,并根据写入屏幕的QPixmaps的内存地址进行检查。

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();
    [...]

但是地址都不同。我也试过使用缓存键,但我遇到了同样的问题。

以下是控制台输出示例:

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 

所以我要问的是,无论如何要在写入rect的像素图和稍后打印到屏幕上的像素图之间进行链接? 如果没有调用Pixmaps,我可以安全地假设屏幕上没有显示rects吗?

如果问题不清楚,请告诉我,我会尽力澄清。

谢谢。

1 个答案:

答案 0 :(得分:1)

@Stephen Chu提到的更简单的方法是使用QGraphicsScene

以下是单击矩形时使用qdebug打印的示例。

<强> 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})