0

I have problem with sharing pointer to Pixmap via signal-slot mechanism. Inside slot function I have correct QPixmap with fulled data. In reciever I have unaccessible QPixmap with empty data. Any ideas?

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;
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Kate Zz
  • 3,002
  • 5
  • 14
  • 17
  • 1
    Yes. Don't use pointers with Qt copy-on-write data types, unless you really, absolutely have to, and you know exactly why. The QPixmap object is really just a pointer to shared data already. – hyde Jun 18 '18 at 16:05
  • I tryed the same with non-pointers, the same result. – Kate Zz Jun 18 '18 at 16:12
  • We are programmers here, not psychics, we do not possess the supernatural powers, required to know what's wrong with your code without examining it. – dtech Jun 18 '18 at 16:34
  • provide a [mcve] – eyllanesc Jun 18 '18 at 16:39
  • @hyde using anything is OK if you know what you are doing, using anything is bad if you don't :) – dtech Jun 18 '18 at 16:40
  • And I also cannot help but wonder how come this question is being voted a close on grounds of "why isn't this code working" when it contains no code? – dtech Jun 18 '18 at 16:44
  • @KateZz which part of `Minimal, Complete, and Verifiable example` don't you get? That code is completely invalid... – dtech Jun 18 '18 at 17:00
  • it can be perceived as a pseudo-code, here it is visible that I try the object b to get access to pixmap data – Kate Zz Jun 18 '18 at 17:05
  • @KateZz where is QXXAplication? `return 0;` or `a.exec();`? – eyllanesc Jun 18 '18 at 17:05
  • @eyllanesc I haven't problem with this part of code) – Kate Zz Jun 18 '18 at 17:07
  • @KateZz Please improve your code, what we want is to see where the error is and teach you that this is not correct. The easiest thing is to show you a code that is correct but probably on another occasion you ask the same thing – eyllanesc Jun 18 '18 at 17:08
  • @dtech Well... I would say there are a lot of things (generally called anti-patterns) you can't both be using, and at the same time know what you are doing while thinking you are doing a right thing... :-) – hyde Jun 18 '18 at 19:22
  • @katezz Your code seems to be missing emitting the signal, so I don't know how the slot ever gets called... also it looks likely your fillPixmap has some errors. Also looks like there is a memory leak. Or possibly a dangling pointer. Or both. – hyde Jun 18 '18 at 19:26

1 Answers1

3

I can not tell you what is wrong with your code because as you indicate you are showing a pseudo-code, so I will show you the correct way to do it.

#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"·
eyllanesc
  • 235,170
  • 19
  • 170
  • 241