Results 1 to 9 of 9

Thread: QDirModel and QTreeView

  1. #1
    Join Date
    Apr 2006
    Location
    Rheinland-Pfalz (Germany)
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Question QDirModel and QTreeView

    hi guys,

    i have a littel problem with QDirModel.

    Qt Code:
    1. QString workspace = QFileDialog::getExistingDirectory( this, tr("Open worspace"), QDir::homePath() );
    2. if ( workspace.isEmpty() )
    3. return;
    4.  
    5. QDirModel *model = new QDirModel();
    6. model->setFilter( QDir::AllDirs );
    7. model->setLazyChildCount( true );
    8. model->setData( model->index( workspace ), QVariant(workspace), Qt::DisplayRole );
    9. model->setHeaderData(0, Qt::Horizontal, workspace);
    10.  
    11. tvWorkspace->setModel( model );
    12. tvWorkspace->setRootIndex( model->index( workspace ) );
    13. tvWorkspace->header()->setSortIndicatorShown( true );
    14. tvWorkspace->header()->setClickable( true );
    15. tvWorkspace->setColumnHidden( 1, true );
    16. tvWorkspace->setColumnHidden( 2, true );
    17. tvWorkspace->setColumnHidden( 3, true );
    18.  
    19. connect( tvWorkspace, SIGNAL( clicked( const QModelIndex & ) ), this, SLOT( workspaceItemChanged( const QModelIndex & ) ) );
    To copy to clipboard, switch view to plain text mode 

    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? .....
    Last edited by ChMaster; 13th April 2006 at 00:31.
    Best regeards/kinds

    ChMaster

    Projects:
    DBoxFE
    DMS
    First4 (Plugin developer)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel and QTreeView

    You mean you want the whole directory path to be displayed for the root item?

  3. #3
    Join Date
    Apr 2006
    Location
    Rheinland-Pfalz (Germany)
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QDirModel and QTreeView

    yes,

    but i can not find examples/solution for this
    i have all tested ..... or?
    Best regeards/kinds

    ChMaster

    Projects:
    DBoxFE
    DMS
    First4 (Plugin developer)

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel and QTreeView

    Subclass the model and reimplement data() to return an absolute path for items with invalid parents, more or less like so:

    Qt Code:
    1. QVariant MyDirModel::data(const QModelIndex & index, int role) const{
    2. if((role==DisplayRole || role==EditRole)
    3. && index.isValid() && !index.parent().isValid())
    4. return filePath(index);
    5. return QDirModel::data(index, role);
    6. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2006
    Location
    Rheinland-Pfalz (Germany)
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QDirModel and QTreeView

    the target is nearby

    Qt Code:
    1. MTWorkSpaceModel *model = new MTWorkSpaceModel();
    2.  
    3. QDirModel *modelDir = new QDirModel();
    4. modelDir->setFilter( QDir::AllDirs );
    5. modelDir->setLazyChildCount( true );
    6. QVariant data = model->data( modelDir->index( workspace ), Qt::DisplayRole );
    7. qDebug() << data; // data returns QVariant(QString, "/home/alex/test")
    8.  
    9. modelDir->setData( modelDir->index( workspace ), data, Qt::DisplayRole );
    10.  
    11. dirModel = modelDir;
    12.  
    13. tvWorkspace->setModel( modelDir );
    14. tvWorkspace->setRootIndex( modelDir->index( workspace ) );
    15. tvWorkspace->header()->setSortIndicatorShown( true );
    16. tvWorkspace->header()->setClickable( true );
    17. tvWorkspace->setColumnHidden( 1, true );
    18. tvWorkspace->setColumnHidden( 2, true );
    19. tvWorkspace->setColumnHidden( 3, true );
    20.  
    21. connect( tvWorkspace, SIGNAL( clicked( const QModelIndex & ) ), this, SLOT( workspaceItemChanged( const QModelIndex & ) ) );
    To copy to clipboard, switch view to plain text mode 

    MTWorkSpaceModel
    Qt Code:
    1. QVariant MTWorkSpaceModel::data( const QModelIndex & index, int role ) const {
    2. if((role == Qt::DisplayRole || role == Qt::EditRole ) && index.isValid() && index.parent().isValid())
    3. return filePath( index );
    4.  
    5. return QDirModel::data( index, role );
    6. }
    To copy to clipboard, switch view to plain text mode 

    but not realy function .... why? is the error on
    Qt Code:
    1. modelDir->setData( modelDir->index( workspace ), data, Qt::DisplayRole );
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. tvWorkspace->setRootIndex( modelDir->index( workspace ) );
    To copy to clipboard, switch view to plain text mode 



    sorry for my bad english
    Last edited by ChMaster; 13th April 2006 at 20:28.
    Best regeards/kinds

    ChMaster

    Projects:
    DBoxFE
    DMS
    First4 (Plugin developer)

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel and QTreeView

    What exactly doesn't work? Could you describe the problem?

  7. #7
    Join Date
    Apr 2006
    Location
    Rheinland-Pfalz (Germany)
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QDirModel and QTreeView

    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 .....
    Attached Images Attached Images
    Last edited by ChMaster; 13th April 2006 at 20:54.
    Best regeards/kinds

    ChMaster

    Projects:
    DBoxFE
    DMS
    First4 (Plugin developer)

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel and QTreeView

    See the attached file for a working version.
    Attached Files Attached Files

  9. The following 2 users say thank you to wysota for this useful post:

    anca (23rd May 2006), ChMaster (13th April 2006)

  10. #9
    Join Date
    Apr 2006
    Location
    Rheinland-Pfalz (Germany)
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QDirModel and QTreeView

    thank you very much, for this example, i'm so happy and it works *kiss*
    Best regeards/kinds

    ChMaster

    Projects:
    DBoxFE
    DMS
    First4 (Plugin developer)

Similar Threads

  1. QDirModel and QTreeView cut and paste
    By Micawber in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2008, 21:16
  2. Edit items on QTreeView + QDirModel
    By junior0007 in forum Qt Programming
    Replies: 4
    Last Post: 23rd November 2007, 08:16
  3. QTreeView and QDirModel Header Sort Question
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 09:04
  4. Replies: 1
    Last Post: 15th March 2007, 21:45
  5. QDirModel and a QTreeView.
    By jamd in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.