Solved label->setPixmap causes crash when is used after MyScrollArea calls setWidget(label) inside of constructor
-
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?
-
And what's the backtrace when it crashes?
-
@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?
-
@Rian-Firth Use a debugger, let it run until it crashes, post the backtrace maybe?
-
@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 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?
-
And you're sure you call setWidget() only once? Please remove all other stuff to try to create a small reproducable example.
-
@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
-
Okay, I got it. In
ui_MainWindow.h
there's:imageScrollArea->setWidget(scrollAreaWidgetContents);
Is there a way to get rid of scrollAreaWidgetContents? Am I using QScrollArea in a wrong way?
-
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()
-
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-Ehrlicher, thank you so much for your help.
-
@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.
-
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.