[SOLVED] QTreeview Double Clicked "The program unexpectedly finished"
-
@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.
-
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 -
Do you mean an example of calling
mapToSource
? -
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.
-
In on_treeView_doubleClicked
-
-
@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); -
hi ```
I get error (QSortFilterProxyModel: index from wrong model passed to mapToSource)
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{if(index.isValid())
{
QModelIndex mapped=filter->mapToSource(model->index(dir));qDebug()<<model->filePath(mapped); }
}
-
oh
maybe its
mapToSource(index)