Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Setting QPixmap to Qlabel gives segmentation fault and app crashes

    General and Desktop
    qt5 qlabel qpixmap qimage gstreamer1.0
    5
    22
    1850
    Loading More Posts
    • 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.
    • V
      vicky_mac last edited by

      GstFlowReturn MainWindow::newSample1(GstAppSink *sink1, gpointer gSelf)
      {
          GstSample* sample = NULL;
          GstBuffer* sampleBuffer = NULL;
          GstMapInfo bufferInfo;
          MainWindow* self = static_cast<MainWindow* >(gSelf);
          sample = gst_app_sink_pull_sample(GST_APP_SINK(sink1));
          QImage image ;
          QPixmap image1 ;
          if(sample != NULL)
          {
              sampleBuffer = gst_sample_get_buffer(sample);
              if(sampleBuffer != NULL)
              {
                  gst_buffer_map(sampleBuffer, &bufferInfo, GST_MAP_READ);
                  image = QImage(bufferInfo.data, 640, 480, QImage::Format_Grayscale8);
                  image1 = QPixmap::fromImage(image);
                 
                   if(!image1.isNull())
                   { 
      self->ui->CAM_VDO->setPixmap(image1);       
                   }           
                  gst_buffer_unmap(sampleBuffer, &bufferInfo);
      
              }
      
              gst_sample_unref(sample);
          }
      
          return GST_FLOW_OK;
      }
      

      This application crashes and gives segmentation fault error.
      But when i remove or comment this line :
      self->ui->CAM_VDO->setPixmap(image1);
      it runs fine.

      Can anyone please let me know why it's happening.

      jsulm J.Hilk 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @vicky_mac last edited by

        @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

        self->ui->CAM_VDO

        is CAM_VDO a valid pointer?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        V 1 Reply Last reply Reply Quote 1
        • J.Hilk
          J.Hilk Moderators @vicky_mac last edited by

          @vicky_mac

          WTH is self->ui-> ?

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          V 1 Reply Last reply Reply Quote 1
          • V
            vicky_mac @jsulm last edited by

            @jsulm yes it runs for 1 or 2 sec initially some time more too.
            but then crashes... Also shows images on CAM_VDO label in UI

            1 Reply Last reply Reply Quote 0
            • V
              vicky_mac @J.Hilk last edited by

              @J-Hilk said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

              elf->ui->

              it's an instance to the UI app running ... it access the coponent from mainwindowui and then access CAM_VDO label and displays image on it

              J.Hilk 1 Reply Last reply Reply Quote 0
              • J.Hilk
                J.Hilk Moderators @vicky_mac last edited by

                @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

                it's an instance to the UI app running ... it access the coponent from mainwindowui and then access CAM_VDO label and displays image on it

                I smell a very very big potential error source here.

                Children should not know of object instanced of their parents! One is supposed to use Signal & Slots.

                Please show how you designed this

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                V 1 Reply Last reply Reply Quote 1
                • V
                  vicky_mac @J.Hilk last edited by

                  @J-Hilk i am using gstreamer for decoding a video ... and appsink to capture the data and display it into qt label..

                  this the signal emitted by gstreamer when new sample is ready.

                  g_signal_connect(appsink,"new-sample",G_CALLBACK(newSample1),(gpointer)this);

                  and it calls above given function.

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Hi,

                    Beside what was already said, you might also be hitting a life time issue. Unless bufferInfo.data is const you are triggering this QImage constructor which requires the buffer to be valid as long as the QImage built with it.

                    In any case, you should not update GUI elements directly from a callback as it might be running in a different thread. You can use QMetaObject::invokeMethod with a queued connection to call a slot that will do the QLabel work. Just create the proper QImage in your callback.

                    Note that with recent version of Qt, you can use custom pipeline with QMediaPlayer which might be way simpler to use.

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

                    V 2 Replies Last reply Reply Quote 2
                    • V
                      vicky_mac @SGaist last edited by

                      @SGaist thank for suggestion ...

                      AS i am running qt version 5.11.3 and due to some resctrictions i cannot update it.

                      So i think QMetaObject::invokeMethod approch i'll have to follow . But i don't know how to use it.
                      Do you have any example code or any link where i can get some more details on this.

                      *i tried copying the bufferInfo.data into another buffer and then did the image building and display. Then it works .. Don't know wther i should do this or not.

                      As suggested above i must not update UI from call back function . I'll try to use QMetaObject::invokeMethod and come back to you.

                      1 Reply Last reply Reply Quote 0
                      • V
                        vicky_mac @SGaist last edited by

                        @SGaist can you please suggest how i can use QMetaObject::invokeMethod in this scenario. I am not able to figure it out.

                        1 Reply Last reply Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          Do you have a slot on your widget to set the QPixmap on the label ?

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

                          V 1 Reply Last reply Reply Quote 0
                          • V
                            vicky_mac @SGaist last edited by

                            @SGaist No i was directly updating as given above in the coed.

                            I am not able to figure out on which event i should write the slot for label updation.

                            please suggest.

                            1 Reply Last reply Reply Quote 0
                            • SGaist
                              SGaist Lifetime Qt Champion last edited by

                              Which code exactly ?

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

                              V 1 Reply Last reply Reply Quote 0
                              • V
                                vicky_mac @SGaist last edited by

                                @SGaist
                                In the question it self I have given code where I am updating the label.

                                As I receive a frame in gst reamer thread I update it.

                                But how to do it in signal slot mechanism I am not able to feagure out. As you suggested above using qmetaobject how to use it in code.?

                                1 Reply Last reply Reply Quote 0
                                • Christian Ehrlicher
                                  Christian Ehrlicher Lifetime Qt Champion last edited by

                                  @vicky_mac said in Setting QPixmap to Qlabel gives segmentation fault and app crashes:

                                  As I receive a frame in gst reamer thread I update it.

                                  You are not allowed to modify the GUI from any thread except the main thread (better: the thread where QApplication lives in). Use signals/slots to update your ui.

                                  Qt has to stay free or it will die.

                                  1 Reply Last reply Reply Quote 1
                                  • SGaist
                                    SGaist Lifetime Qt Champion last edited by

                                    Something like:

                                    QMetaObject::invokeMethod(ui->label, "setPixmap", Qt::QueuedConnection, Q_ARG(pixmap));
                                    

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

                                    V 1 Reply Last reply Reply Quote 1
                                    • V
                                      vicky_mac @SGaist last edited by

                                      @SGaist hi thanx for advise..
                                      I did Qmetaobject::Invokemethod(self-ui->vdolabel, "setPixel", qtqueuedconnection, Q_Args(Qpixmap, P)) :

                                      And updating qlabel from setPixmap function in mainwidown.cpp its working fine.

                                      Is it ok now or should have to anything else.
                                      Is this OK passing the object reference and derefenrecing it like this?
                                      MainWindow* self = static_cast<MainWindow* >(gSelf);

                                      1 Reply Last reply Reply Quote 0
                                      • SGaist
                                        SGaist Lifetime Qt Champion last edited by

                                        Do you mean pass your MainWindow instance as the opaque data to your callback ?

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

                                        V 1 Reply Last reply Reply Quote 0
                                        • V
                                          vicky_mac @SGaist last edited by

                                          @SGaist yes, because without that we would not Abe able to invoke method.
                                          MainWindow* self = static_cast<MainWindow* >(gSelf);
                                          This line...

                                          Because now by using Invokemethod I am updating qlabel in main thread and not child or gstreamer thread that seems OK.

                                          1 Reply Last reply Reply Quote 0
                                          • SGaist
                                            SGaist Lifetime Qt Champion last edited by

                                            I'd go a step further and pass directly the pointer to the QLabel object, it's even less instructions to use.

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

                                            V 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post