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

[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 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
                • T Offline
                  T Offline
                  Tymer
                  wrote on 16 Jul 2015, 12:47 last edited by
                  #21

                  I'll post the full code, just to be thorough. The error is exactly the same. When I debugged, it showed that my signal had reached the slot...But there was no change to the image.

                  widget1.h

                  #ifndef WIDGET1_H
                  #define WIDGET1_H
                  
                  #include <QWidget>
                  #include "/absolute/path/to/widget.h"
                  
                  namespace Ui {
                  class widget1;
                  }
                  
                  class widget1 : public QWidget
                  {
                      Q_OBJECT
                  
                      Widget* widget;
                  
                  public:
                      explicit widget1(QWidget *parent = 0);
                      ~widget1();
                  
                  signals:
                      void clicked();
                      void buttonPush();
                  
                  private slots:
                      void on_pushButton_clicked();
                  
                  private:
                      Ui::widget1 *ui;
                  };
                  
                  #endif // WIDGET1_H
                  
                  

                  widget1.cpp

                  #include "widget1.h"
                  #include "ui_widget1.h"
                  
                  widget1::widget1(QWidget *parent) :
                      QWidget(parent),
                      ui(new Ui::widget1)
                  {
                      ui->setupUi(this);
                  
                      widget = new Widget;
                  
                      connect(this, SIGNAL(buttonPush()),widget,SLOT(onClicked()));
                  }
                  
                  widget1::~widget1()
                  {
                      delete ui;
                  }
                  
                  void widget1::on_pushButton_clicked()
                  {
                      emit buttonPush();
                  }
                  
                  

                  An instance of widget1 was created in main, then shown. I created a ui file in designer, with all default values.

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

                    Is it me or are you not showing widget at all ?

                    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 17 Jul 2015, 13:27 last edited by
                      #23

                      My main.cpp:

                      #include "widget.h"
                      #include "widget1.h"
                      #include <QApplication>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                      
                          Widget w;
                          widget1 w1;
                          w1.show();
                          w.show();
                      
                      
                          return a.exec();
                      }
                      
                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 17 Jul 2015, 22:31 last edited by
                        #24

                        Ok, you are not showing the widget you have connected. Your Widget w in your main.cpp is not the same as the one you have instantiated in your widgte1 constructor.

                        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 20 Jul 2015, 13:14 last edited by
                          #25

                          So I should make my widget a singleton, more or less? How would I go about that?

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

                            No, not at all. Either:

                            1. Remove the widget from MainWindow and connect the one in main.cpp
                            2. Remove the one from main.cpp and show the one from MainWindow

                            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 20 Jul 2015, 13:18 last edited by
                              #27

                              How would I connect to the instance in main?

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

                                By using the static version of QObject::connect

                                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 20 Jul 2015, 13:54 last edited by
                                  #29

                                  I know this is really basic, but I'm having a bit of a brain fart. I want to make a pointer to the instance I create in main, but I don't want my class to know too much about what's going on in main. How do I do this so that I can create a connection to the object in main from a different widget?

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

                                    Right now I have:

                                    main.cpp

                                    
                                    myWidget* w = new myWidget;
                                    actionTestWindow* t = new actionTestWindow(w, new QWidget);
                                    

                                    actionTestWindow.cpp

                                    actionTestWindow::actionTestWindow(myWidget* Widget, QWidget *parent) :
                                        QWidget(parent),
                                        ui(new Ui::actionTestWindow)
                                        {
                                            ui->setupUi(this);
                                    
                                            Widget = new myWidget;
                                    

                                    The actionTestWindow is no longer showing, and I think it has something to do with how I insert the QWidget parameter.

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

                                      Roll back to before you modified that constructor. Just call '''Widget->show();'''

                                      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 23 Jul 2015, 18:04 last edited by
                                        #32

                                        Alright, finally got this problem SOLVED!

                                        I pass in an instance of myWidget as a parameter of actionTestWindow in main. It's important to note that you need to specify the QWidget as parent = 0 or it will not show. The relevant code looks something like this:

                                        main.cpp

                                        QWidget* parent = new QWidget;
                                        parent = 0;
                                        myWidget* w = new myWidget;
                                        actionTestWindow* a = new actionTestWindow(w,parent);
                                        
                                        w->show();
                                        a->show();
                                        

                                        actionTestWindow.h

                                        class actionTestWindow : public QWidget {
                                        myWidget* m;
                                        
                                        public:
                                        actionTestWindow(myWidget* mW, QWidget* parent = 0);
                                        

                                        actionTestWindow.cpp

                                        actionTestWindow::actionTestWindow(myWidget* mW, QWidget* parent)
                                        {
                                        this->m = mW;
                                        /*use this->m to reference myWidget*/
                                        }
                                        

                                        **note: irrelevant or standard code was mostly emitted.

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

                                          And there you have a memory leak.

                                          Why don't you just show the myWidget you had originally created in Widget ?

                                          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 27 Jul 2015, 14:42
                                          0

                                          23/36

                                          17 Jul 2015, 13:27

                                          • Login

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