Search code examples
qtsegmentation-faultqt-creatorqwidgetqlayout

Using "QWidget *QLayoutItem::widget()" throws a segment-fault (into qobjectdefs_impl.h)


I'm using QTabWidget(s) inside QTabWidget tabs and when I try to get a "child" QTabWidget which is stored in the layout of a tab of the "parent" QTabWidget, I have an segment-fault error.

PS : I'm using Qt Creator v4.8.1, Qtv5.6.3 and Windows 7 64-bits OS (VM under VBox v6.0.4)

Initially, the following line throws the error

dbTablesTabWidget = static_cast<QTabWidget*>(ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget());

I have tried without the "static_cast" with the following line but the error is always here

QWidget *widget = ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget();

My last try (without return the QWidget pointer) with always the same "segment-fault" error.

ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget();

That I don't understand is that the following line (first line in the first for loop)

QString itemClass = ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget()->metaObject()->className();

works fine althought it includes the same "base code".

The segment-fault error appears in the "qobjectdefs_impl.h" file :

1   MainWindow::addRow                                                                                                                                        mainwindow.cpp          298  0x403de6   
2   QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, void (MainWindow:: *)()>::call(void (MainWindow:: *)(), MainWindow *, void * *) qobjectdefs_impl.h      501  0x410175   
3   QtPrivate::FunctionPointer<void (MainWindow:: *)()>::call<QtPrivate::List<>, void>(void (MainWindow:: *)(), MainWindow *, void * *)                       qobjectdefs_impl.h      520  0x4103e2   
4   QtPrivate::QSlotObject<void (MainWindow:: *)(), QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase *, QObject *, void * *, bool *)            qobject_impl.h          143  0x410243   
5   QtPrivate::QSlotObjectBase::call                                                                                                                          qobject_impl.h          124  0x6ba5d241 
6   QMetaObject::activate                                                                                                                                     qobject.cpp             3715 0x6b8fc213 
7   QMetaObject::activate                                                                                                                                     qobject.cpp             3595 0x6b8fbc3e 
8   QAbstractButton::clicked                                                                                                                                  moc_qabstractbutton.cpp 307  0x9c0685   
9   QAbstractButtonPrivate::emitClicked                                                                                                                       qabstractbutton.cpp     404  0x9be459   
10  QAbstractButtonPrivate::click                                                                                                                             qabstractbutton.cpp     397  0x9be3f4   
11  QAbstractButton::mouseReleaseEvent                                                                                                                        qabstractbutton.cpp     1002 0x9bf6f7   
12  QWidget::event                                                                                                                                            qwidget.cpp             8757 0x8f6881   
13  QAbstractButton::event                                                                                                                                    qabstractbutton.cpp     959  0x9bf56c   
14  QPushButton::event                                                                                                                                        qpushbutton.cpp         673  0xa55168   
15  QApplicationPrivate::notify_helper                                                                                                                        qapplication.cpp        3804 0x8bf880   
16  QApplication::notify                                                                                                                                      qapplication.cpp        3277 0x8bd5f1   
17  QCoreApplication::notifyInternal2                                                                                                                         qcoreapplication.cpp    1015 0x6b8d37dd 
18  QCoreApplication::sendSpontaneousEvent                                                                                                                    qcoreapplication.h      228  0xc2fb5d   
19  QApplicationPrivate::sendMouseEvent                                                                                                                       qapplication.cpp        2773 0x8bc2cb   
20  QWidgetWindow::handleMouseEvent                                                                                                                           qwidgetwindow.cpp       607  0x90f314   
... <plus>                                                                                                                                                                                            

Below, an extract of the addRow Method ("custom" private slot called after the click on a QPushButton)

// var
    int dbIndex = ui->tabWidget->currentIndex();
    int tableIndex = 0;
    QTabWidget *dbTablesTabWidget;

    // Détection de l'index de l'item du layout de l'onglet de la BDD actuellement sélectionné
    // -> Détection de la position du widget (jeu d'onglets des tables) dans le layout pour récupérer l'index de la table actuelle.
    for (int i=0; i<ui->tabWidget->currentWidget()->layout()->count(); i++)
    {
        QString itemClass = ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget()->metaObject()->className();
        qDebug(itemClass.toLatin1());
        if (itemClass == "QTabWidget")
        {
            qDebug("Cet élément est un QTabWidget !!!");

            //*** The following line throws a segment fault ***

            //dbTablesTabWidget = static_cast<QTabWidget*>(ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget());
            //QWidget *widget = ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget();
            ui->tabWidget->currentWidget()->layout()->takeAt(i)->widget();

            tableIndex = dbTablesTabWidget->currentIndex();
        }
    }

I'm a newbie with Qt and I don't see where the segment-fault error could come from. The compilation process is OK, please see below (french language version)

15:53:12: Configuration inchangée, étape qmake sautée.
15:53:12: Débute : "C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe" -j1
C:/Qt/Tools/mingw492_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'E:/Projects_Info_Slink/_Training/C++/QTCreatorProject/DataBaseViewerGUI/build-DataBaseViewerGUI-Desktop_Qt_5_6_3_MinGW_32bit-Debug'
mingw32-make[1]: Nothing to be done for 'first'.
mingw32-make[1]: Leaving directory 'E:/Projects_Info_Slink/_Training/C++/QTCreatorProject/DataBaseViewerGUI/build-DataBaseViewerGUI-Desktop_Qt_5_6_3_MinGW_32bit-Debug'
15:53:13: Le processus "C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe" s'est terminé normalement.
15:53:13: Temps écoulé : 00:01.

Solution

  • I have finally found the error : I should use layout()->itemAt(i) instead layout()->takeAt(i). It explains why the error occurs at the second time. takeAt remove the item from the list, so during the next call, the item doesn't exist in the list.

    PS : for QList object, this is at instead itemAt to get the element from the list of element.