Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Quit application crashes when using QHeaderView::setSectionResizeMode



  • I have a QTreeView (salesView) and in order to adapt the column width to its contents, I added this to my code:

    ui_.salesView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    

    But now, when I quit the application via the menu, using

    connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit);
    

    the application crashes. If I do quit the application by just closing the window, everything is fine.

    What am I doing wrong?

    The backtrace is:

    Thread 1 (Thread 0x7f625eb21d00 (LWP 32045)):
    [KCrash Handler]
    #6 __cxxabiv1::__dynamic_cast (src_ptr=0x564348be4bd0, src_type=0x5643483da2d8 <typeinfo for Entity>, dst_type=0x5643483d9ae8 <typeinfo for Sale>, src2dst=0) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/dyncast.cc:68
    #7 0x00005643483044b2 in SaleModel::parent(QModelIndex const&) const ()
    #8 0x00005643482bea43 in QModelIndex::parent() const ()
    #9 0x0000564348304812 in SaleModel::data(QModelIndex const&, int) const ()
    #10 0x00007f6262b8c361 in QStyledItemDelegate::initStyleOption(QStyleOptionViewItem*, QModelIndex const&) const () from /usr/lib/libQt5Widgets.so.5
    #11 0x00007f6262b8b951 in QStyledItemDelegate::sizeHint(QStyleOptionViewItem const&, QModelIndex const&) const () from /usr/lib/libQt5Widgets.so.5
    #12 0x00007f6262bd091d in QTreeViewPrivate::widthHintForIndex(QModelIndex const&, int, QStyleOptionViewItem const&, int) const () from /usr/lib/libQt5Widgets.so.5
    #13 0x00007f6262bd34b4 in QTreeView::sizeHintForColumn(int) const () from /usr/lib/libQt5Widgets.so.5
    #14 0x00007f6262b785bb in ?? () from /usr/lib/libQt5Widgets.so.5
    #15 0x00007f6262b78da7 in QHeaderView::sectionSize(int) const () from /usr/lib/libQt5Widgets.so.5
    #16 0x00007f6262bd3afa in QTreeViewPrivate::updateScrollBars() () from /usr/lib/libQt5Widgets.so.5
    #17 0x00007f6262bdaf42 in QTreeView::updateGeometries() () from /usr/lib/libQt5Widgets.so.5
    #18 0x00007f6262b67bc9 in ?? () from /usr/lib/libQt5Widgets.so.5
    #19 0x00007f6261f6b82c in QMetaObject::activate(QObject*, int, int, void**) () from /usr/lib/libQt5Core.so.5
    #20 0x00007f6262b7e923 in QHeaderView::viewportEvent(QEvent*) () from /usr/lib/libQt5Widgets.so.5
    #21 0x00007f6261f40b0b in QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject*, QEvent*) () from /usr/lib/libQt5Core.so.5
    #22 0x00007f626290be14 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQt5Widgets.so.5
    #23 0x00007f62629135f1 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/libQt5Widgets.so.5
    #24 0x00007f6261f40df9 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/libQt5Core.so.5
    #25 0x00007f62629478fe in QWidgetPrivate::hideChildren(bool) () from /usr/lib/libQt5Widgets.so.5
    #26 0x00007f62629478e9 in QWidgetPrivate::hideChildren(bool) () from /usr/lib/libQt5Widgets.so.5
    #27 0x00007f62629478e9 in QWidgetPrivate::hideChildren(bool) () from /usr/lib/libQt5Widgets.so.5
    #28 0x00007f62629478e9 in QWidgetPrivate::hideChildren(bool) () from /usr/lib/libQt5Widgets.so.5
    #29 0x00007f6262947b7b in QWidgetPrivate::hide_helper() () from /usr/lib/libQt5Widgets.so.5
    #30 0x00007f626294c951 in QWidgetPrivate::setVisible(bool) () from /usr/lib/libQt5Widgets.so.5
    #31 0x00007f6262947d8b in QWidgetPrivate::close_helper(QWidgetPrivate::CloseMode) () from /usr/lib/libQt5Widgets.so.5
    #32 0x00007f626294851e in QWidget::~QWidget() () from /usr/lib/libQt5Widgets.so.5
    #33 0x00005643482b52ca in MainWindow::~MainWindow() ()
    #34 0x00005643482b52ec in MainWindow::~MainWindow() ()
    #35 0x00005643482b69be in std::default_delete<MainWindow>::operator()(MainWindow*) const ()
    #36 0x00005643482b68b7 in std::unique_ptr<MainWindow, std::default_delete<MainWindow> >::~unique_ptr() ()
    #37 0x00005643482b6201 in main ()
    [Inferior 1 (process 32045) detached]


  • Lifetime Qt Champion

    Hi
    Question.
    you did not put MainWindow in std::unique_ptr ?
    (meaning its internal Qt code)

    Also, I think it's not related as such to setSectionResizeMode.

    Try to make a small example where you do the same and see if that crashes.
    It seems related to SaleModel so might be something in the model but best
    to test with a small sample first that it's really not setSectionResizeMode which
    is gulity.



  • @mrjj said in Quit application crashes when using QHeaderView::setSectionResizeMode:

    you did not put MainWindow in std::unique_ptr ?

    No, I didn't. :)

    Also, I think it's not related as such to setSectionResizeMode.

    Of course, It's possible that it lies in the SaleModel. A simple example might not be so easy, but I'll give it a try...

    Btw, is there somewhere a hint about what's important for the model to work flawlessly with setSectionResizeMode(QHeaderView::ResizeToContents)?

    Thanks a lot.


  • Lifetime Qt Champion

    @beedaddy
    Hi
    I would start by setting a break point in
    SaleModel::parent(QModelIndex const&) const ()
    and see if anything goes wrong there.
    My best guess is something that already deleted is being accessed.
    Do you have an abstract model (SaleModel) that uses some external list or similar?

    As far as i know, there are no special requirements for setSectionResizeMode.
    Also, it first when you close app, something happens, if it was due to something missing, one should think it would happen as soon as setSectionResizeMode was called.



  • What I noticed is that using

    connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit);
    

    does not trigger QWidget::closeEvent(QCloseEvent *event) (which I use for storing the window size, for example). So my wild guess is that QApplication::quit() doesn't properly quit the application. When I however use

    connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::closeAllWindows);
    

    everything works as expected and nothing crashes. I don't understand this behavour, because I thought QApplication::quit() is the right way to quit the application.


  • Moderators

    @beedaddy the QGuiApplication::lastWindowClosed() signal itself calls quit as well.

    My guess is you're doing something strange in one of your destructors.

    btw, is your form class instance created on the heap or the stack?



  • @J-Hilk I have no clue what that strange thing could be since everything seems to be ok when using QApplication::closeAllWindows().

    My form class instance is created on the stack.


Log in to reply