QLabel not refreshing with QPixmap "loadFromData"



  • Hi.

    I have this kind of code :

    @
    QLabel * label;
    QPixmap * pixmap;
    uint8_t data[42];

    /* init */
    [...]

    /* loop */
    {
    update_my(data);
    pixmap->loadFromData(data, sizeof(data));
    label->setPixmap(*pixmap);
    }
    @

    The problem is that the label don't refresh. I suspect a problem of cache but i thought that there wasn't any implicit cache managment...

    My current workaround is calling pixmap->fill() before reload the pixmap (or if i paint on the pixmap, the refresh is ok too).

    I could also declare the QPixmap inside the loop, but i would like to avoid memory allocations as far as possible.

    I would like to simply raise a kind of dirty flag to force the label redrawing.

    What do you suggest please ?

    Regards.

    Paul.


  • Moderators

    You create a pixmap A. You set that pixmap A on the label which keeps a copy of it B. Then you change A. Why should the B change?

    You use a pointer to the pixmap A instead of using it directly, but that does not matter at all to the Label: It gets a QPixmap and makes its copy. QPixmaps are implicitly shared, so making copies is pretty cheap.

    Hmm... reading your post again I think I misunderstood it a bit in the first round.



  • Here is a test case :

    @/* Test.hpp*/
    #include <QtGui>

    class Test : public QLabel
    {
    Q_OBJECT;

    public:

    Test();

    protected:

    void timerEvent(QTimerEvent *event);

    private:

    QPixmap * m_Pixmap;

    QBasicTimer * m_Timer;

    int m_cpt;

    char m_data[9];
    };
    @

    @/* Test.cpp */
    #include "Test.hpp"

    static const char data_black[] = "P1 1 1\n1";
    static const char data_white[] = "P1 1 1\n0";

    Test::Test() :
    m_Pixmap(new QPixmap(1, 1)),
    m_Timer(new QBasicTimer),
    m_cpt(0)
    {
    this->setScaledContents(true);
    m_Timer->start(1000, this);
    }

    void Test::timerEvent(QTimerEvent event)
    {
    if(event->timerId() == m_Timer->timerId())
    {
    // update label
    memcpy(m_data, (m_cpt++%2)?data_white:data_black, sizeof(m_data));
    m_Pixmap->loadFromData((const uchar
    )(m_data), sizeof(m_data));
    this->setPixmap(*m_Pixmap);

      fprintf(stdout, "update data : \"%s\"\n", m_data);
    
      m_Timer->start(1000, this);
    } 
    

    QWidget::timerEvent(event);
    }
    @

    @/* Main.cpp */
    #include <QtGui>

    #include "Test.hpp"

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

    Test test;

    QPushButton quitButton("Quit");
    QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

    QVBoxLayout layout;
    layout.addWidget(&test);
    layout.addWidget(&quitButton);

    QWidget window;
    window.setLayout(&layout);

    window.show();

    return app.exec();
    }
    @


Log in to reply
 

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