C ++ - Qt奇怪的QList :: at()错误

时间:2015-12-18 14:36:49

标签: c++ qt

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本身已初始化但为空。但是,当我使用wishLists.push_back( wishlist );或QList的任何其他功能时,我会收到此错误:

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。所以一旦我在列表中至少有一个项目,它就不会抛出这个错误。

任何想法都错了吗?

更新

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>              

1 个答案:

答案 0 :(得分:0)

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

            wishlistBox->addItem( text );

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

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

因为我不希望简单地修改QList来给出这样的调用堆栈。