+ Reply to Thread
Results 1 to 8 of 8

Thread: QDirModel+QTreeView and checkable items

  1. #1
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDirModel+QTreeView and checkable items

    I'm trying to implement QTreeView + QDirModel with checkable items. I have found topic with similar problem, I've derived new class from QDirModel but I don't get how to reimplement flags() and data() methods to have needed functionality. Can somebody help?

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDirModel+QTreeView and checkable items

    Quote Originally Posted by L.Marvell View Post
    I'm trying to implement QTreeView + QDirModel with checkable items. I have found topic with similar problem, I've derived new class from QDirModel but I don't get how to reimplement flags() and data() methods to have needed functionality. Can somebody help?
    checkable dir & file...

    A Context menu can run ... on file & dir.... like other file explorer ...


    Qt Code:
    1. void User_File::Load_Connector()
    2. {
    3. startnow = QDir::currentPath();
    4. dirsizebite = 0;
    5. dirandfile.clear();
    6. qtcpeed = true;
    7. modeluser = new QDirModel;
    8. modellocal = new QDirModel;
    9. setAcceptDrops(true);
    10. modellocal->setResolveSymlinks(false);
    11. modeluser->setResolveSymlinks(false);
    12. usertree->setModel(modeluser);
    13. mlocal->setModel(modellocal);
    14.  
    15. modeluser->setSupportedDragActions(Qt::CopyAction);
    16. modellocal->setSupportedDragActions(Qt::CopyAction);
    17. usertree->setDragEnabled(true);
    18. mlocal->setDragEnabled(true);
    19.  
    20. mlocal->setRootIndex(modellocal->index(lstart));
    21. usertree->setRootIndex(modeluser->index(ustart));
    22. connect(usertree, SIGNAL(clicked(const QModelIndex &)),this, SLOT(GlobalContextU(const QModelIndex&)));
    23. connect(mlocal, SIGNAL(clicked(const QModelIndex &)),this, SLOT(OpenLocalFile(const QModelIndex&)));
    24. connect(pushButton, SIGNAL(clicked()),this, SLOT(ResetDBDir()));
    25.  
    26.  
    27.  
    28.  
    29.  
    30. usertree->resizeColumnToContents (0);
    31. mlocal->resizeColumnToContents (0);
    32. IndexDir(ustart);
    33. qDebug() << "### dirsizebite " << dirsizebite;
    34.  
    35.  
    36. dirowerflow = false;
    37.  
    38. if (dirsizebite > MAXDIRSIZE) {
    39. dirowerflow = true;
    40. QMessageBox::warning(this, tr("User dir."),tr("The maxiumum capacity of user dir is %1!").arg(BiteorMega(MAXDIRSIZE)));
    41. }
    42. label_2->setText(tr("User Dir size %1 - %2 ")
    43. .arg(BiteorMega(dirsizebite))
    44. .arg(UmanTimeFromUnix(QTime_Null())));
    45. dirandfile.clear();
    46.  
    47.  
    48. Q_ASSERT(qtcpeed);
    49. qtcpeed = false;
    50.  
    51. }
    52.  
    53.  
    54. void User_File::GlobalContextU( const QModelIndex &index )
    55. {
    56. if (!index.isValid()) {
    57. return;
    58. }
    59. ResetDD();
    60. userselect = true;
    61. acc = true;
    62. const QString filepath = modeluser->filePath(index);
    63. fi = QFileInfo(filepath);
    64. last = QCursor::pos();
    65. /* model to fast! */
    66. QTimer::singleShot(200, this, SLOT(User_Context()));
    67. }
    68.  
    69.  
    70. void User_File::User_Context()
    71. {
    72. TContext = new QMenu(this);
    73. QString wat = fi.absoluteFilePath();
    74. QDir checkdir(wat);
    75. dir = checkdir.exists();
    76. if (dir) {
    77. TContext->addAction(tr( "Delete dir \"%1\" " ).arg(fi.fileName()), this , SLOT( DDrm() ) );
    78. TContext->addAction(tr( "Open dir \"%1\" " ).arg(fi.fileName()), this , SLOT( DDop() ) );
    79. } else {
    80. TContext->addAction(tr( "Delete file \"%1\" " ).arg(fi.fileName()), this , SLOT( DDrm() ) );
    81. TContext->addAction(tr( "Open file \"%1\" " ).arg(fi.fileName()), this , SLOT( DDop() ) );
    82. }
    83. TContext->addAction(tr( "New Dir here" ), this , SLOT( DDmkdir() ) );
    84. TContext->addAction(tr( "Reload ..." ), this , SLOT( Load_Connector() ) );
    85. TContext->addAction(tr( "User dir Close contex" ), TContext , SLOT( close() ) );
    86. TContext->exec(last);
    87.  
    88. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDirModel+QTreeView and checkable items

    It seems you didn't understand me (or maybe I don't understand something...). I need QTreeView with tree of directories and each has checkbox near (with flag Qt::ItemIsUserCheckable). I think new class has to be derived from QDirModel with flags(), data() and maybe some other methods reimplemented, but I don't get how to do it. Need some example.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,514 Times in 1,387 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDirModel+QTreeView and checkable items

    Quote Originally Posted by L.Marvell View Post
    I think new class has to be derived from QDirModel with flags(), data() and maybe some other methods reimplemented, but I don't get how to do it. Need some example.
    Yes, that's right. You need to reimplement flags(), data() and setData(). flags() should indeed return Qt::ItemIsUserCheckable amongst the other flags, data() should return a valid value for Qt::CheckStateRole and setData() should store the passed value for Qt::CheckStateRole. The toughest part is to implement a data structure of some kind for storing the check states as QDirModel is not able to store them for you.
    J-P Nurmi

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

    Default Re: QDirModel+QTreeView and checkable items

    Quote Originally Posted by jpn View Post
    The toughest part is to implement a data structure of some kind for storing the check states as QDirModel is not able to store them for you.
    I suggest QSet<QString> where the key is the FilePathRole (defined in QDirModel). The only trick is to update the contents if the dir model changes (i.e. file gets deleted). If a path is in the set, it is checked.

  6. #6
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDirModel+QTreeView and checkable items

    Quote Originally Posted by jpn View Post
    Yes, that's right. You need to reimplement flags(), data() and setData(). flags() should indeed return Qt::ItemIsUserCheckable amongst the other flags, data() should return a valid value for Qt::CheckStateRole and setData() should store the passed value for Qt::CheckStateRole. The toughest part is to implement a data structure of some kind for storing the check states as QDirModel is not able to store them for you.
    That's the point. I can't get how should data() look like and what should it return. It seems I need to read Assistant again and again about Models...

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,514 Times in 1,387 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDirModel+QTreeView and checkable items

    Qt Code:
    1. class MyDirModel : public QDirModel
    2. {
    3. ...
    4.  
    5. private:
    6. QSet<QString> checked; // wysota's suggestion
    7. }
    8.  
    9. Qt::ItemFlags MyDirModel::flags(const QModelIndex& index) const
    10. {
    11. Qt::ItemFlags f = QDirModel::flags(index);
    12. if (index.column() == 0) // make the first column checkable
    13. f |= Qt::ItemIsUserCheckable;
    14. return f;
    15. }
    16.  
    17. QVariant MyDirModel::data(const QModelIndex& index, int role = Qt::DisplayRole) const
    18. {
    19. if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
    20. {
    21. // the item is checked only if we have stored its path
    22. return (checked.contains(filePath(index)) ? Qt::Checked : Qt::Unchecked);
    23. }
    24. return QDirModel::data(index, role);
    25. }
    26.  
    27. bool MyDirModel::setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)
    28. {
    29. if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
    30. {
    31. // store checked paths, remove unchecked paths
    32. if (value.toInt() == Qt::Checked)
    33. checked.insert(filePath(index));
    34. else
    35. checked.remove(filePath(index));
    36. return true;
    37. }
    38. return QDirModel::setData(index, value, role);
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 11th May 2007 at 18:57. Reason: corrected a confusing typo
    J-P Nurmi

  8. The following 2 users say thank you to jpn for this useful post:

    dimixan (1st June 2008), L.Marvell (11th May 2007)

  9. #8
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDirModel+QTreeView and checkable items

    Great thanks man! I'll try it surely.

+ Reply to Thread

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