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. label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor
Qt 6.11 is out! See what's new in the release blog

label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 1.3k 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.
  • R Offline
    R Offline
    Rian Firth
    wrote on last edited by Rian Firth
    #1

    .h file:

    class ImageScrollArea : public QScrollArea
    {
    	Q_OBJECT
    
    public:
    	explicit ImageScrollArea(QWidget *parent = nullptr);
    	~ImageScrollArea() = default;
    
    public:
    	void SetImage(const QPixmap& pixmap);
    
    private:
    	QLabel* m_imageLabel;
    };
    

    .cpp file:

    ImageScrollArea::ImageScrollArea(QWidget* parent)
    	: QScrollArea(parent)
    	, m_imageLabel(new QLabel)
    {
    	setWidget(m_imageLabel);
    }
    
    void ImageScrollArea::SetImage(const QPixmap& pixmap)
    {
    	if (pixmap.isNull())
    		return;
    
    	m_imageLabel->setPixmap(pixmap);
    }
    
    The inferior stopped because it received a signal from the operating system.
    
    Signal name :  SIGSEGV
    Signal meaning :  Segmentation fault
    

    But if calling setWidget inside of SetImage everything is fine:

    void ImageScrollArea::SetImage(const QPixmap& pixmap)
    {
    	if (pixmap.isNull())
    		return;
    
    	setWidget(m_imageLabel); // <-- calling here instead of constructor
    	m_imageLabel->setPixmap(pixmap);
    }
    

    Any idea why?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Rian-Firth said in label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor:

      Any idea why?

      From where do you call SetImage? Please also show the backtrace.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Rian-Firth said in label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor:

        Any idea why?

        From where do you call SetImage? Please also show the backtrace.

        R Offline
        R Offline
        Rian Firth
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        MainWindow.cpp

        MainWindow::MainWindow(QWidget* parent)
        	: QMainWindow(parent)
        	, m_ui(std::make_unique<Ui::MainWindow>())
        {
        	m_ui->setupUi(this);
        
        	connect(m_ui->actionOpen, &QAction::triggered, this, [this]() {
        		auto filename = QFileDialog::getOpenFileName(this, "Open", "", "Images (*.png *.jpg)");
        		m_ui->imageScrollArea->SetImage(QPixmap(filename)); // <-- SetImage call is here
        	});
        }
        
        MainWindow::~MainWindow()
        {
        }
        

        Also what exactly do you mean by backtrace?
        Call stack is fine?
        6f51d512-094c-48b5-a31a-8b918c5b8e04-image.png

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          And what's the backtrace when it crashes?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          R 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            And what's the backtrace when it crashes?

            R Offline
            R Offline
            Rian Firth
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor:

            And what's the backtrace when it crashes?

            How do I check that?

            Christian EhrlicherC 1 Reply Last reply
            0
            • R Rian Firth

              @Christian-Ehrlicher said in label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor:

              And what's the backtrace when it crashes?

              How do I check that?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Rian-Firth Use a debugger, let it run until it crashes, post the backtrace maybe?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              R 2 Replies Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Rian-Firth Use a debugger, let it run until it crashes, post the backtrace maybe?

                R Offline
                R Offline
                Rian Firth
                wrote on last edited by
                #7

                @Christian-Ehrlicher said in label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor:

                post the backtrace maybe?

                I'm sorry, that's the part I don't understand the most. I don't get what you mean by backtrace and where I can see it in qtc.
                Is call stack (the screen I sent before) not the same as backtrace?

                1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Rian-Firth Use a debugger, let it run until it crashes, post the backtrace maybe?

                  R Offline
                  R Offline
                  Rian Firth
                  wrote on last edited by Rian Firth
                  #8

                  @Christian-Ehrlicher

                  1  QLabel::setPixmap(QPixmap const&)                                                                                                                                                                      0x10d252c  
                  2  ImageScrollArea::SetImage                                                                                                                                                      ImageScrollArea.cpp 18  0x401668   
                  3  MainWindow::<lambda()>::operator()(void) const                                                                                                                                 MainWindow.cpp      14  0x401850   
                  4  QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, MainWindow::MainWindow(QWidget *)::<lambda()>>::call(MainWindow::<lambda()> &, void * *)             qobjectdefs_impl.h  146 0x401cb8   
                  5  QtPrivate::Functor<MainWindow::MainWindow(QWidget *)::<lambda()>, 0>::call<QtPrivate::List<>, void>(MainWindow::<lambda()> &, void *, void * *)                                qobjectdefs_impl.h  256 0x401c98   
                  6  QtPrivate::QFunctorSlotObject<MainWindow::MainWindow(QWidget *)::<lambda()>, 0, QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase *, QObject *, void * *, bool *) qobjectdefs_impl.h  443 0x401c69   
                  7  void doActivate<false>(QObject *, int, void * *)                                                                                                                                                       0x68b99915 
                  8  QAction::activate(QAction::ActionEvent)                                                                                                                                                                0xfb3df3   
                  9  QMenuPrivate::activateCausedStack(QVector<QPointer<QWidget>> const&, QAction *, QAction::ActionEvent, bool)                                                                                            0x110c4eb  
                  10 QMenuPrivate::activateAction(QAction *, QAction::ActionEvent, bool)                                                                                                                                    0x1112572  
                  11 QMenu::mouseReleaseEvent(QMouseEvent *)                                                                                                                                                                0x1113459  
                  12 QWidget::event(QEvent *)                                                                                                                                                                               0xff3d78   
                  13 QMenu::event(QEvent *)                                                                                                                                                                                 0x111564e  
                  14 QApplicationPrivate::notify_helper(QObject *, QEvent *)                                                                                                                                                0xfb790e   
                  15 QApplication::notify(QObject *, QEvent *)                                                                                                                                                              0xfbf1ea   
                  16 QCoreApplication::sendSpontaneousEvent(QObject *, QEvent *)                                                                                                                                            0x68a65588 
                  17 QApplicationPrivate::sendMouseEvent(QWidget *, QMouseEvent *, QWidget *, QWidget *, QWidget * *, QPointer<QWidget>&, bool, bool)                                                                       0xfbd83b   
                  18 QWidgetWindow::handleMouseEvent(QMouseEvent *)                                                                                                                                                         0x100a37e  
                  19 QWidgetWindow::event(QEvent *)                                                                                                                                                                         0x100cc06  
                  20 QApplicationPrivate::notify_helper(QObject *, QEvent *)                                                                                                                                                0xfb790e   
                  21 QApplication::notify(QObject *, QEvent *)                                                                                                                                                              0xfbe3e3   
                  22 QCoreApplication::sendSpontaneousEvent(QObject *, QEvent *)                                                                                                                                            0x68a65588 
                  23 QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent *)                                                                                                                 0x61979ff4 
                  24 QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *)                                                                                                   0x6197b315 
                  25 QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>)                                                                                                                  0x6195726c 
                  26 QEventDispatcherWin32::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)                                                                                                                            0x68abb6c0 
                  27 QWindowsGuiEventDispatcher::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)                                                                                                                       0x6a903e85 
                  28 QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>)                                                                                                                                                0x68a63a15 
                  29 QCoreApplication::exec()                                                                                                                                                                               0x68a6cd75 
                  30 qMain                                                                                                                                                                          main.cpp            10  0x4016e0   
                  31 WinMain                                                                                                                                                                        qtmain_win.cpp      97  0x40390d   
                  32 __tmainCRTStartup                                                                                                                                                                                      0x4013c7   
                  33 WinMainCRTStartup                                                                                                                                                                                      0x4014cb   
                  

                  Also I found "Create Full Backtrace" option. Is it needed?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    And you're sure you call setWidget() only once? Please remove all other stuff to try to create a small reproducable example.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    R 2 Replies Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      And you're sure you call setWidget() only once? Please remove all other stuff to try to create a small reproducable example.

                      R Offline
                      R Offline
                      Rian Firth
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher said in label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor:

                      And you're sure you call setWidget() only once?

                      Yup, calling setWidget() only in my custom ScrollArea's constructor.

                      Please remove all other stuff to try to create a small reproducable example.

                      https://mega.nz/file/owtigShT#1M63GDHVPDqzct7i47UV5y1lRxJWjGe-lfGJwYqqgw4

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        And you're sure you call setWidget() only once? Please remove all other stuff to try to create a small reproducable example.

                        R Offline
                        R Offline
                        Rian Firth
                        wrote on last edited by Rian Firth
                        #11

                        @Christian-Ehrlicher

                        Okay, I got it. In ui_MainWindow.h there's:

                        imageScrollArea->setWidget(scrollAreaWidgetContents);
                        

                        fb2ab2c2-7313-4f32-9fe2-a931908b2779-image.png

                        Is there a way to get rid of scrollAreaWidgetContents? Am I using QScrollArea in a wrong way?

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          As I already guessed. setWidget() is called two times

                          imageScrollArea->setWidget(scrollAreaWidgetContents);
                          

                          When creating it via designer a default widget is already said to the scroll area. Add you label to this instead calling setWidget()

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          1
                          • R Offline
                            R Offline
                            Rian Firth
                            wrote on last edited by
                            #13

                            I'm not sure if it's a proper way but removing scrollAreaWidgetContents directly from .ui file helps as well.

                                <item>
                                 <widget class="ImageScrollArea" name="imageScrollArea">
                                  <property name="widgetResizable">
                                   <bool>true</bool>
                                  </property>
                                  <widget class="QWidget" name="scrollAreaWidgetContents">
                                   <property name="geometry">
                                    <rect>
                                     <x>0</x>
                                     <y>0</y>
                                     <width>780</width>
                                     <height>538</height>
                                    </rect>
                                   </property>
                                  </widget>
                                 </widget>
                                </item>
                            
                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              Rian Firth
                              wrote on last edited by
                              #14

                              @Christian-Ehrlicher, thank you so much for your help.

                              1 Reply Last reply
                              0
                              • R Rian Firth

                                I'm not sure if it's a proper way but removing scrollAreaWidgetContents directly from .ui file helps as well.

                                    <item>
                                     <widget class="ImageScrollArea" name="imageScrollArea">
                                      <property name="widgetResizable">
                                       <bool>true</bool>
                                      </property>
                                      <widget class="QWidget" name="scrollAreaWidgetContents">
                                       <property name="geometry">
                                        <rect>
                                         <x>0</x>
                                         <y>0</y>
                                         <width>780</width>
                                         <height>538</height>
                                        </rect>
                                       </property>
                                      </widget>
                                     </widget>
                                    </item>
                                
                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @Rian-Firth said in label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor:

                                I'm not sure if it's a proper way but removing scrollAreaWidgetContents directly from .ui file helps as well.

                                Modifying an ui file manually is never a good idea and for sure no solution.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                2
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Hi
                                  If you dont need to set scrollarea properties in Designer you can also just
                                  promote a plain QWidget and hence don't get this issue.

                                  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