Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel not refreshing with QPixmap "loadFromData"

QLabel not refreshing with QPixmap "loadFromData"

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    polch
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        polch
        wrote on last edited by
        #3

        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();
        }
        @

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved