通过信号槽发送QPixmap指针,空像素图数据

时间:2018-06-18 16:03:53

标签: c++ qt qt5 qt-signals qpixmap

我有通过信号槽机制共享指向Pixmap的指针的问题。 内部插槽功能我有正确的QPixmap与丰富的数据。 在接收器中,我有无法访问的QPixmap和空数据。 有什么想法吗?

class A
{
    public:
        A:A():pixmap(0){};
        void fillPixmap()
        {
            // correct Pixmap and data isn't null
        }
    public signals:
        void sendQPixmapToB(QPixmap*);
    private:
        QPixmap *pixmap;
}

class B
{
    public:
        B:B(){};

    public slots:

        void recievePixmap(QPixmap* pixmap)
        {
            // here in debugger pixmap is unaccesible and data is 0
        }
}

void onButtonClicked()
{
    a.fillPixmap();
}

int main()
{
    A a;
    B b;



    .....


    connect(a, SIGNAL(sendQPixmapToB(QPixmap*)),b,SLOT(recievePixmap(QPixmap*)));

    return 0;
}

1 个答案:

答案 0 :(得分:2)

我无法告诉您代码有什么问题,因为当您指示您正在显示伪代码时,我会告诉您正确的方法。

#include <QGuiApplication>
#include <QPixmap>
#include <QTimer>

#include <QDebug>

class A: public QObject{
    Q_OBJECT
public:
    using QObject::QObject;

    void sendPixmap(){
        fillPixmap();
        emit sendQPixmapToB(pixmap);
    }
signals:
    void sendQPixmapToB(const QPixmap & pixmap);
private:
    void fillPixmap(){
        pixmap = QPixmap(64, 64);
        pixmap.fill(Qt::red);
    }
    QPixmap pixmap;
};

class B: public QObject{
  Q_OBJECT
public:
    using QObject::QObject;
public slots:
    void recievePixmap(const QPixmap & pixmap){
        qDebug()<<pixmap;
    }
};
int main(int argc, char *argv[])
{
    QGuiApplication a(argc, argv);
    A obja;
    B objb;

    QObject::connect(&obja, &A::sendQPixmapToB, &objb, &B::recievePixmap);

    QTimer::singleShot(1000, &obja, &A::sendPixmap);
    return a.exec();
}

#include "main.moc"·