Unsolved [SOLVED] QTreeview Double Clicked "The program unexpectedly finished"
-
I get this error when double clicked of qtreeview item "The program unexpectedly finished"
not get error when I writing "qDebug()<<model.fileName(index);"
I get error when only writting filePath.
How can I solve ?void MainWindow::on_treeView_doubleClicked(const QModelIndex &index) { qDebug()<<model.filePath(index); }
-
@takoo said:
Hi and welcome
Is the model a QDirModel ?
are you doing something like
http://www.bogotobogo.com/Qt/Qt5_QTreeView_QDirModel_ModelView_MVC.php -
my model a QFileSystemModel.
This problem not relevant your example :(
-
@takoo said:
Ok. you didnt tell.
are you using any filters ?
QDir::Filters QFileSystemModel::filter() const -
I am using QSortFilterProxyModel for filtering
-
@takoo
Hi. well since u can see it, then the filter should be ok.
Model lives in mainwindow.h? as member?I cannot reproduce it.'
model = new QFileSystemModel(); model->setRootPath("e:/"); QListView* view = new QListView(); view->setModel(model); view->setRootIndex(model->index("e:/")); view->show(); connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(on_treeView_doubleClicked(QModelIndex))); } void window::on_treeView_doubleClicked(const QModelIndex& index) { qDebug() << model->filePath(index); }
dont crash so you have something in your code.
-
i tryed but was not . I get same error .
-
@takoo
Hi
Im not sure what you mean.
You tried this sample and it still crashes? -
yes unfourtunely
-
@takoo
and you did use something else than "e:/" ?
and else exact code? Model is living in mainwin .h and dont run out of scope.
(ps sample leaks view)Hmm. I cant guess sorry.
What Qt version and platform?
-
QT 5.5 Linux mint
Code is here
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); const QString dir( "/home/tako/cocos2d-x/" ); const QString targetDir( dir + "OpenWorld/Classes" ); const QString targetRes(dir+"OpenWorld/Resources"); const QString targetBin(dir+"OpenWorld/bin"); model = new QFileSystemModel; model->setRootPath( targetDir ); filter = new filtermodel( targetDir,targetRes,targetBin); filter->setSourceModel(model); filter->mapToSource(i); ui->treeView->setModel(filter); ui->treeView->setRootIndex(filter->mapFromSource(model->index(dir))); } void MainWindow::on_treeView_doubleClicked(const QModelIndex &index) { i=index; qDebug()<<model->fileName(i); }
-
Hi,
index comes from
filter
not frommodel
. You have to map the index like you did in your setRootIndex call -
Please give me example code .
I writting at the moment from phone -
Do you mean an example of calling
mapToSource
? -
Codes above
-
In the code above, you are trying to use an index from your proxy on your file system model. That's the problem, you have to use mapToSource in order to get the right index.
-
@SGaist Where will I use mapToSource ?
-
In on_treeView_doubleClicked
-
@SGaist I wrote this code and again I get error("the has unexpectedly finished") ```
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{if(index.isValid())
{
filter->mapToSource(model->index(dir));
qDebug()<<model->filePath(index);
}
} -
@takoo said:
Hi
http://doc.qt.io/qt-5.5/qsortfilterproxymodel.html#mapToSource
it returns a new index. I assume you should use that index.
you still use "old" index filePath(index);QModelIndex mapped=filter->mapToSource(model->index(dir));
model->filePath(mapped);