QTableView::setModel(NULL) crash application ???
hi guys , I have QTableView crashing , the code is like this
- tableView()->setModel( NULL );
when I debug , it crashed when setModel(NULL) runs , and it said
ASSERT:“visualRow != -1” in file itemviews\qtableview.cpp ,
and I found it strange that if I don’t modify the data from tableview , then it won’t crash when I debug here
while if I modify the data from tableview , it will crash here , is this a qt bug or my code’s bug . someone could
give me some good suggestions , thank you very much ~~
EDIT: please use @-tags for code highlighting, Gerolf
10 replies
I cannot reproduce this. Further investigation is not possible with your very tiny code snippets.
The following code works perfectly for me:
————— nullmodeltest.h —————
- #ifndef NULLMODELTEST_H
- #define NULLMODELTEST_H
- #include <QWidget>
- class QTableView;
- class QStandardItemModel;
- class QPushButton;
- {
- Q_OBJECT
- public:
- public slots:
- void changeModel();
- protected:
- };
- #endif // NULLMODELTEST_H
————— nullmodeltest.cpp —————
- #include "nullmodeltest.h"
- #include <QLayout>
- #include <QTableView>
- #include <QStandardItemModel>
- #include <QPushButton>
- #include <QDebug>
- {
- view = new QTableView;
- lay->addWidget(view);
- button = new QPushButton;
- button->setText("change model");
- lay->addWidget(button);
- connect(button, SIGNAL(clicked()), this, SLOT(changeModel()));
- int numRows = 5;
- int numCols = 4;
- for(int r = 0; r < numRows; ++r)
- for(int c = 0; c < numCols; ++c)
- view->setModel(model);
- }
- void NullModelTest::changeModel()
- {
- qDebug() << "NullModelTest::changeModel() ----- triggered -----";
- qDebug() << "NullModelTest::changeModel(): old model=" << view->model();
- if(view->model())
- view->setModel(0);
- else
- view->setModel(model);
- qDebug() << "NullModelTest::changeModel(): new model=" << view->model();
- }
————— main.cpp —————
- #include <QApplication>
- #include "nullmodeltest.h"
- int main(int argc, char *argv[])
- {
- NullModelTest nmt;
- nmt.show();
- return a.exec();
- }
thanks , Volker , I also did the simple testing like you , it won’t crash , in the project , it involves a lot of other code , like delegates , a little complicated , but when I debug the crash , it crash at the setModel(NULL), and I didn’t find where the other code crashed , I will pay more time on it , thanks very much ~~
thanks , Volker , I also did the simple testing like you , it won’t crash , in the project , it involves a lot of other code , like delegates , a little complicated , but when I debug the crash , it crash at the setModel(NULL), and I didn’t find where the other code crashed , I will pay more time on it , thanks very much ~~
So it is time to create a
- short
- complete
- compilable
example for us that demonstrates the problem. Our crystal balls are on summer vacation, so unfortunately, we cannot guess what’s going wrong…
Oh, and experience tells us that your chances are good to find the mistake yourself while preparing the example :-)
I’m having the same issue. setModel(NULL) crashes with:
ASSERT: “visualRow != -1” in file itemviews\qtableview.cpp, line 1568
It seems to crash if setModel(NULL) is called while an item in the table view is being edited.
I don’t know much more about it right now. I found this thread while trying to find info about the problem.
Qt 4.8 on Windows 7
I made a test case and can consistently reproduce the problem. It happens when the following conditions (at least) are true:
1. The table is in edit mode (e.g. you are typing text in a cell and have not accepted the changes yet).
2. setModel(NULL) is called from the mousePressed event of another control.
It does not happen when setModel(NULL) is called from the clicked() signal of a push button.
Please let me know if I should start a new thread for this, and I will repost this there.
Here is a program that shows the problem. Click the table cell to begin editing it, and while it is being edited, click inside the frame on the right to crash the program (start an empty Qt project, create an empty cpp file and paste this all in there):
main.cpp
- #include <QtGui/QApplication>
- #include <QMainWindow>
- #include <QTableView>
- #include <QHBoxLayout>
- #include <QAbstractTableModel>
- public:
- value_("Edit Me")
- {
- }
- return parent.isValid() ? 0 : 1;
- }
- return parent.isValid() ? 0 : 1;
- }
- }
- }
- value_ = value.toString();
- dataChanged(index, index);
- return true;
- } else
- return false;
- }
- private:
- QString value_;
- };
- public:
- view_(view)
- {
- }
- view_->setModel(NULL);
- }
- private:
- };
- int main (int argc, char **argv) {
- table->setModel(new TestModel(table));
- layout->addWidget(table);
- NullModelMousePressedFrame *frame = new NullModelMousePressedFrame(table);
- frame->setMinimumWidth(100);
- frame->setMaximumWidth(100);
- layout->addWidget(frame);
- window->centralWidget()->setLayout(layout);
- window->setWindowTitle("Click blank area on right while editing table.");
- window->show();
- return app.exec();
- }
A workaround is to call setFocus() in the mousePressEvent() before setting the model to NULL. Clearly some kind of issue with QTableView’s assumptions during / just before a focus change. This is also suggested by the fact that the assertion failure is in QTableView::moveCursor (http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/itemviews/qtableview.cpp line 1561, although that’s 4.7 not 4.8).
Anyways this makes the above example not crash:
- setFocus(); // workaround (must be *before* setModel)
- view_->setModel(NULL);
- }
You must log in to post a reply. Not a member yet? Register here!



