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. [SOLVED] QLabel won't update pixmap from inside function
QtWS25 Last Chance

[SOLVED] QLabel won't update pixmap from inside function

Scheduled Pinned Locked Moved General and Desktop
pixmapqt4qpixmap
36 Posts 3 Posters 27.5k Views
  • 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.
  • T Offline
    T Offline
    Tymer
    wrote on 8 Jul 2015, 15:24 last edited by Tymer
    #1

    SOLUTION IN POST 32

    Hi all. I've been working with Qt now for a few weeks, and I've finally run into a problem I have not been able to solve.

    I've tried absolutely everything I know, and I've come to the conclusion that this issue is over my head. I've tried running repaint(), update() and this->update(); and everything else that I could think of. Pixmap works outside of the function (in the constructor) but not inside a function. Here is code (only relevant is pasted, please indicate if you would like more):

    myWidget.h

    #define NUM_POINTERS 10
    
    
    QLabel* pointerArray[NUM_POINTERS];
    QPixmap circle;
    QPixmap* triangle;
    QPixmap* whitex;
    int activePointer;
    

    myWidget.cpp

    activePointer = 0;
    
    QPixmap circle (":/Resources/greencircle.png");
    this->whitex = new QPixmap(":/Resources/white_x.png");
    this->triangle = new QPixmap(":/Resources/redtriangle.png");
    
    //create an array of pointers to the label1-10 objects
    pointerArray[0] = ui->label1;
    pointerArray[1] = ui->label2;
    pointerArray[2] = ui->label3;
    ...
    pointerArray[9] = ui->label10;
    
    for (int i = 0;i < 10; i++)
    {
        pointerArray[i]->setPixmap(circle);
    }
    
    void myWidget::changeImage()
    {
      updatesEnabled();
      if (activePointer < 10){
          pointerArray[activePointer]->setPixmap(*this->whitex);
          activePointer++;
          update();
    }
     else{
         printf("end of array\n");
         fflush(stdout);
     }
    
    }
    

    I get a row of circles printed where I want them, but I won't get any white Xs. The pixmap changes to the whitex, but it will not update. It does not crash, it continues adding to activePointer until the end of the array. I have tried pointerArray[activePointer]->update(); with no luck.

    I also asked this question on Stack Overflow and answered the following questions:

    -The paths are all correct. I copied them directly by right clicking on the resource and selecting "copy URL."
    -I can't declare it on the stack, because then it will not work inside the function.
    -I'm sure that the setPixmap is executing, because the debugger shows the pixmap changing to the 50x50 image (iirc it also says it is the whitex). I even tried having no pixmap set, and even then it wouldn't set the pixmap from inside the function.

    Thanks in advance.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 8 Jul 2015, 16:27 last edited by
      #2

      Hi and welcome to devnet

      Did you try already repaint()?

      Vote the answer(s) that helped you to solve your issue(s)

      T 1 Reply Last reply 8 Jul 2015, 16:32
      0
      • K koahnig
        8 Jul 2015, 16:27

        Hi and welcome to devnet

        Did you try already repaint()?

        T Offline
        T Offline
        Tymer
        wrote on 8 Jul 2015, 16:32 last edited by
        #3

        @koahnig I've tried repaint() with no success. I just tried it again to be sure, still no luck.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Jul 2015, 20:59 last edited by
          #4

          HI and welcome to devnet,

          What would be the problem with triangle and whitex on the stack rather that the heap ? By the way you are shadowing circle in your constructor.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply 8 Jul 2015, 21:04
          0
          • S SGaist
            8 Jul 2015, 20:59

            HI and welcome to devnet,

            What would be the problem with triangle and whitex on the stack rather that the heap ? By the way you are shadowing circle in your constructor.

            T Offline
            T Offline
            Tymer
            wrote on 8 Jul 2015, 21:04 last edited by
            #5

            @SGaist I actually just tried it with them on the stack, it seems to work now, not sure why it wouldn't before. Unfortunately, that didn't fix my issue.

            Thank you for all the help.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 8 Jul 2015, 22:20 last edited by
              #6

              How are you calling changeImage ?
              Are you calling setUpdatesEnabled(false); somewhere ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tymer
                wrote on 9 Jul 2015, 13:15 last edited by
                #7

                Not calling changeImage() anywhere, just setPixmap(). I actually make a point of running updatesEnabled() before the pixmap is changed.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 9 Jul 2015, 13:28 last edited by
                  #8

                  updatesEnabled is a getter that will tell you whether updates are enabled or not. They are by default.

                  If you don't call changeImage anywhere, how can you expect your labels to get updated with whitex ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tymer
                    wrote on 9 Jul 2015, 14:45 last edited by Tymer 7 Sept 2015, 14:48
                    #9

                    I don't understand changeImage, I haven't read anything about it until now. Can you send some documentation?

                    EDIT: I just remembered that I didn't mention this outside of my tags. I'm using Qt 4.8.7.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 9 Jul 2015, 15:03 last edited by
                      #10

                      It's the void myWidget::changeImage()that you wrote in your first post

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Tymer
                        wrote on 9 Jul 2015, 15:10 last edited by Tymer 7 Sept 2015, 15:11
                        #11

                        Shoot, I'm sorry, my brain is all over the place today, and the function names are changed from the originals. In another class I have a connection:

                        connect(this, SIGNAL(changeImageBtnClicked()),[myWidget pointer], SLOT(changeImage()));

                        I know this connection works, because if I press the button enough times it starts printing the "end of array" repeatedly.

                        Again, apologies for the confusion. Thank you for your help and patience.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 11 Jul 2015, 23:00 last edited by
                          #12

                          Are you doing anything else with these labels ? e.g. configuration ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          T 1 Reply Last reply 13 Jul 2015, 12:44
                          0
                          • S SGaist
                            11 Jul 2015, 23:00

                            Are you doing anything else with these labels ? e.g. configuration ?

                            T Offline
                            T Offline
                            Tymer
                            wrote on 13 Jul 2015, 12:44 last edited by
                            #13

                            @SGaist Just the original loop in the constructor of myWidget. The next time my code touches them is the changeImage().

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              Tymer
                              wrote on 13 Jul 2015, 13:54 last edited by
                              #14

                              Back at work today, I remembered that the QLabels are in a QHorizontalLayout. AFAIK this shouldn't affect anything, but it's worth bringing up.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 13 Jul 2015, 21:49 last edited by
                                #15

                                Just to rule out a QPixmap problem.

                                What happens if you do

                                QImage img(120, 120, QImage::Format_ARGB32);
                                img.fill(Qt::red);
                                pointerArray[activePointer]->setPixmap(QPixmap::fromImage(img));
                                activePointer++;
                                

                                ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                T 1 Reply Last reply 14 Jul 2015, 12:50
                                0
                                • S SGaist
                                  13 Jul 2015, 21:49

                                  Just to rule out a QPixmap problem.

                                  What happens if you do

                                  QImage img(120, 120, QImage::Format_ARGB32);
                                  img.fill(Qt::red);
                                  pointerArray[activePointer]->setPixmap(QPixmap::fromImage(img));
                                  activePointer++;
                                  

                                  ?

                                  T Offline
                                  T Offline
                                  Tymer
                                  wrote on 14 Jul 2015, 12:50 last edited by
                                  #16

                                  @SGaist Thank you for your continued help.

                                  I tried that, no dice. When I debug, after running the line with setPixmap, I get pixmap (120x120) QVariant (QPixmap) as a property of the label that would be changed.

                                  I'm also noticing that in a different part of a project, the layout doesn't want to update when I run a function. The only similarity is that both of them are labels in QLayouts.

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 14 Jul 2015, 23:21 last edited by
                                    #17

                                    Can you try with the following widget:

                                    widget.h

                                    #ifndef WIDGET_H
                                    #define WIDGET_H
                                    
                                    #include <QWidget>
                                    
                                    class QLabel;
                                    
                                    class Widget : public QWidget
                                    {
                                        Q_OBJECT
                                    
                                    public:
                                        Widget(QWidget *parent = 0);
                                        ~Widget();
                                    
                                    public slots:
                                        void onClicked();
                                    
                                    private:
                                        QVector<QLabel*> _labels;
                                        int _currentLabel;
                                        QColor _color;
                                    };
                                    
                                    #endif // WIDGET_H
                                    

                                    widget.cpp

                                    #include "widget.h"
                                    #include <QString>
                                    #include <QLabel>
                                    #include <QPushButton>
                                    #include <QHBoxLayout>
                                    #include <QVBoxLayout>
                                    
                                    Widget::Widget(QWidget *parent)
                                        : QWidget(parent)
                                        , _currentLabel(0)
                                        , _color(Qt::blue)
                                    {
                                        QHBoxLayout *buttonLayout = new QHBoxLayout;
                                        QImage img(120, 120, QImage::Format_ARGB32);
                                        img.fill(Qt::red);
                                        for (int i = 0 ; i < 4 ; ++i) {
                                            QLabel *label = new QLabel;
                                            label->setPixmap(QPixmap::fromImage(img));
                                            buttonLayout->addWidget(label);
                                            _labels << label;
                                        }
                                    
                                        QPushButton *button = new QPushButton(tr("Test"));
                                    
                                        QVBoxLayout *layout = new QVBoxLayout(this);
                                        layout->addLayout(buttonLayout);
                                        layout->addWidget(button);
                                    
                                        connect(button, &QPushButton::clicked, this, &Widget::onClicked);
                                    }
                                    
                                    Widget::~Widget()
                                    {
                                    
                                    }
                                    
                                    void Widget::onClicked()
                                    {
                                        QImage img(120, 120, QImage::Format_ARGB32);
                                        img.fill(_color);
                                    
                                        _labels[_currentLabel]->setPixmap(QPixmap::fromImage(img));
                                        ++_currentLabel;
                                        if (_currentLabel == _labels.count()) {
                                            _currentLabel = 0;
                                            _color = QColor(qrand() % 255, qrand() % 255, qrand() % 255);
                                        }
                                    }
                                    

                                    main.cpp

                                    #include "widget.h"
                                    #include <QApplication>
                                    
                                    int main(int argc, char *argv[])
                                    {
                                        QApplication a(argc, argv);
                                    
                                        Widget w;
                                        w.show();
                                    
                                        return a.exec();
                                    }
                                    

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    1
                                    • T Offline
                                      T Offline
                                      Tymer
                                      wrote on 15 Jul 2015, 13:02 last edited by
                                      #18

                                      That's working perfectly for me. Maybe this will give me the clue I need to make it work...

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        Tymer
                                        wrote on 15 Jul 2015, 13:19 last edited by Tymer
                                        #19

                                        Ah, that one works because it is from within the same widget. I added a push button to myWidget and it was working. Unfortunately, the button that I need to push is in a different window.

                                        ~~ EDIT: The new button on myWidget works perfectly. I thought maybe I could circumvent the problems of the changeImage button by making it a slot for "emit buttonclicked," which I connected to the new button on myWidget. It had the same problem as before, where it does nothing and prints "end of array" after 11 clicks. BUT strangely, if I press the button on myWidget, it resets the value of activePointer. I'm not sure why this is.

                                        Essentially, I can push the changeImage button 11 times until it prints "end of array," but then I can push the myWidget pushbutton 11 times and it will change the image 10 times and start printing "end of array" after that. Curious. ~~

                                        EDIT EDIT: It looks like right now it's only changing the value of activePointer locally. Whoops. Problem still exists, however.

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 15 Jul 2015, 22:22 last edited by
                                          #20

                                          The fact that the button is inside another widget should not have any impact here. Can you reproduce your bug using may sample as base ?

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          1/36

                                          8 Jul 2015, 15:24

                                          • Login

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