PDA

View Full Version : QDirModel and QTreeView



ChMaster
12th April 2006, 23:11
hi guys,

i have a littel problem with QDirModel.



QString workspace = QFileDialog::getExistingDirectory( this, tr("Open worspace"), QDir::homePath() );
if ( workspace.isEmpty() )
return;

QDirModel *model = new QDirModel();
model->setFilter( QDir::AllDirs );
model->setLazyChildCount( true );
model->setData( model->index( workspace ), QVariant(workspace), Qt::DisplayRole );
model->setHeaderData(0, Qt::Horizontal, workspace);

tvWorkspace->setModel( model );
tvWorkspace->setRootIndex( model->index( workspace ) );
tvWorkspace->header()->setSortIndicatorShown( true );
tvWorkspace->header()->setClickable( true );
tvWorkspace->setColumnHidden( 1, true );
tvWorkspace->setColumnHidden( 2, true );
tvWorkspace->setColumnHidden( 3, true );

connect( tvWorkspace, SIGNAL( clicked( const QModelIndex & ) ), this, SLOT( workspaceItemChanged( const QModelIndex & ) ) );


it is work but i see only the folders in this QTreeView:

|-include
|-src
|-ui

but i would like this:

/home/alex/project/test
|-include
|-src
|-ui

i searched on forum index, but can not found items of this ....
but how? can anyone help me? .....

wysota
13th April 2006, 16:44
You mean you want the whole directory path to be displayed for the root item?

ChMaster
13th April 2006, 16:53
yes,

but i can not find examples/solution for this :(
i have all tested ..... or? :confused:

wysota
13th April 2006, 17:05
Subclass the model and reimplement data() to return an absolute path for items with invalid parents, more or less like so:


QVariant MyDirModel::data(const QModelIndex & index, int role) const{
if((role==DisplayRole || role==EditRole)
&& index.isValid() && !index.parent().isValid())
return filePath(index);
return QDirModel::data(index, role);
}

ChMaster
13th April 2006, 19:21
the target is nearby :D



MTWorkSpaceModel *model = new MTWorkSpaceModel();

QDirModel *modelDir = new QDirModel();
modelDir->setFilter( QDir::AllDirs );
modelDir->setLazyChildCount( true );
QVariant data = model->data( modelDir->index( workspace ), Qt::DisplayRole );
qDebug() << data; // data returns QVariant(QString, "/home/alex/test")

modelDir->setData( modelDir->index( workspace ), data, Qt::DisplayRole );

dirModel = modelDir;

tvWorkspace->setModel( modelDir );
tvWorkspace->setRootIndex( modelDir->index( workspace ) );
tvWorkspace->header()->setSortIndicatorShown( true );
tvWorkspace->header()->setClickable( true );
tvWorkspace->setColumnHidden( 1, true );
tvWorkspace->setColumnHidden( 2, true );
tvWorkspace->setColumnHidden( 3, true );

connect( tvWorkspace, SIGNAL( clicked( const QModelIndex & ) ), this, SLOT( workspaceItemChanged( const QModelIndex & ) ) );


MTWorkSpaceModel


QVariant MTWorkSpaceModel::data( const QModelIndex & index, int role ) const {
if((role == Qt::DisplayRole || role == Qt::EditRole ) && index.isValid() && index.parent().isValid())
return filePath( index );

return QDirModel::data( index, role );
}


but not realy function .... why? is the error on

modelDir->setData( modelDir->index( workspace ), data, Qt::DisplayRole );
or

tvWorkspace->setRootIndex( modelDir->index( workspace ) );



sorry for my bad english

wysota
13th April 2006, 19:43
What exactly doesn't work? Could you describe the problem?

ChMaster
13th April 2006, 19:48
i have the subclass created and QDirModel reimplemented with QVariant data(...)

in the code (see above) set i the data of QDirModel with my subclass ....
but it is not function, i see in the treeview only the folder
|-ui
|-src
|-include

but not
/home/alex/test
|-ui
|-src
|-include

in short form: i can not see rootItem .....

wysota
13th April 2006, 20:03
See the attached file for a working version.

ChMaster
13th April 2006, 20:27
thank you very much, for this example, i'm so happy and it works *kiss* :D