C++ - Qt Strange QList::at() error

C++ - Qt Strange QList::at() error

本文关键字:at error QList Qt Strange C++      更新时间:2023-10-16
void MainWindow::onWishlistEditButtonClicked()
{
    QInputDialog *inputDialog = new QInputDialog();
    inputDialog->setOptions( QInputDialog::NoButtons );
    static QString defaultText = "Default Wishlist";
    //No Error here, prints 0 as expected
    qDebug() << wishLists.size();

    bool isOk = false;
    QString text = inputDialog->getText( NULL, "Create a Wishlist", "Wishlist Name: ", QLineEdit::Normal, defaultText, &isOk );
    if( isOk && !text.isEmpty() ) {
        if( text.length() >= 100 ) {
            defaultText = text; //on next pop up this will be the text
            //let user know that the maximum number can only be 100
            QMessageBox::critical( this, "Error", "The maximum number of characters is 100.", QMessageBox::Ok );
        } else {
            defaultText = "Default Wishlist";
            WishList wishlist( text );
            if( wishlist.save() ) {
                wishlistBox->addItem( text );
                wishLists.push_back( wishlist ); //any method invoked by this object will cause the same error even .size()
            } else {
                QMessageBox::critical( this, "Error", "Could not create wishlist.", QMessageBox::Ok );
            }
        }
    }
    inputDialog->deleteLater();
}

我使用qDebug() << wishLists.size();的行非常好。QList 本身已初始化,但为空。但是,当我使用 QList 的wishLists.push_back( wishlist );或任何其他功能时,我会得到此错误:

ASSERT failure in QList<T>::at: "index out of range", file ../../../Qt5.5.1/5.5/gcc_64/include/QtCore/qlist.h, line 510
The program has unexpectedly finished.

这只是最近的错误。昨天完全没问题,没有对程序进行任何更改!每次我保存愿望清单并重新启动程序时,它都会将所有愿望清单读取到 qlist 中。因此,一旦列表中至少有 1 个项目,它就不会抛出此错误。

任何想法有什么问题?

更新:

0   __GI_raise  /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so    56  0x7ffff5d5acc9  
1   __GI_abort  /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so    89  0x7ffff5d5e0d8  
2   QMessageLogger::fatal(const char *, ...) const          0x7ffff669513e  
3   qt_assert_x(const char *, const char *, const char *, int)          0x7ffff6690701  
4   QList<WishList>::at qlist.h 510 0x40c725    
5   MainWindow::updateListView  mainwindow.cpp  204 0x408539    
6   MainWindow::onWishlistChanged   mainwindow.cpp  99  0x407c2e    
7   MainWindow::qt_static_metacall  moc_mainwindow.cpp  102 0x413520    
8   QMetaObject::activate(QObject *, int, int, void * *)            0x7ffff68c335e  
9   QComboBox::currentIndexChanged(int)         0x7ffff77be121  
10  ??          0x7ffff77c0451  
11  ??          0x7ffff77c26dd  
12  QComboBox::setCurrentIndex(int)         0x7ffff77c294f  
13  ??          0x7ffff77c79d9  
14  QMetaObject::activate(QObject *, int, int, void * *)            0x7ffff68c335e  
15  QAbstractItemModel::rowsInserted(QModelIndex const&, int, int, QAbstractItemModel::QPrivateSignal)          0x7ffff693fd84  
16  QAbstractItemModel::endInsertRows()         0x7ffff683fea2  
17  ??          0x7ffff71cd5bb  
18  QComboBox::insertItem(int, QIcon const&, QString const&, QVariant const&)           0x7ffff77c3f18  
19  QComboBox::insertItem   qcombobox.h 268 0x4092a8    
... <More>              

看起来崩溃更有可能实际发生在:

            wishlistBox->addItem( text );

由于调用堆栈位于某些与模型相关的更新代码中,因此您可能希望先添加到列表中:

            wishLists.push_back( wishlist );
            wishlistBox->addItem( text );

因为我没想到简单地修改 QList 会给出这样的调用堆栈。