Results 1 to 9 of 9

Thread: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

  1. #1
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    Hello I am new to Qt and can't figure out why I can add a column header, but the data never populates...It always sends ::data indexes 0-3 as if it doesn't know there is an additional column, but I see 0-4 columns:
    Name | Size | Type | Date | [My File Attribute]

    What I am doing wrong? Here is my header and implementation file:

    FileModel.h
    Qt Code:
    1. #ifndef FILEMODEL_H
    2. #define FILEMODEL_H
    3.  
    4. #include <QtGui>
    5.  
    6. class FileModel : public QDirModel
    7. {
    8. QOBJECT
    9.  
    10. public:
    11.  
    12. FileModel(QWidget *parent = 0)
    13.  
    14. ~FileModel();
    15.  
    16. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
    17.  
    18. virtual QModelIndex index(const QString &path, int column = 0) const;
    19.  
    20. virtual QModelIndex parent(const QModelIndex &index) const;
    21.  
    22. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    23.  
    24. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
    25.  
    26. virtual QVariant data(const QModelIndex &index, int role = Qt:DisplayRole) const;
    27.  
    28. virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    29. };
    30. #endif // FILEMODEL_H
    To copy to clipboard, switch view to plain text mode 

    FileModel.cpp
    Qt Code:
    1. #include "FileModel.h"
    2.  
    3. FileModel::FileModel(QWidget *parent) : QDirModel(parent)
    4. {
    5. }
    6.  
    7. FileModel::~FileModel()
    8. {
    9. }
    10.  
    11. QModelIndex FileModel::index(int row, int column, const QModelIndex &parent) const
    12. {
    13. return QDirModel::index(row, column, parent);
    14. }
    15.  
    16. QModelIndex FileModel::index(const QString &path, int column) const
    17. {
    18. return QDirModel::index(path, column);
    19. }
    20.  
    21. QModelIndex FileModel::parent(const QModelIndex &index) const
    22. {
    23. return QDirModel::parent(index);
    24. }
    25.  
    26. int FileModel::rowCount(const QModelIndex &parent) const
    27. {
    28. return QDirModel::rowCount(parent);
    29. }
    30.  
    31. int FileModel::columnCount(const QModelIndex &parent) const
    32. {
    33. if (parent.column() > 0)
    34. {
    35. return 0; // This is a safe return??
    36. }
    37. return QDirModel::columnCount(parent) + 1; // Adding one column to QDirModel
    38. }
    39.  
    40. QVariant FileModel::data(const QModelIndex &index, int role) const
    41. {
    42. if (role == Qt::DisplayRole)
    43. {
    44. if (index.column() == QDirModel::columnCount()) // I never ever see 4 for index.column() count, it's ok for QDirModel::columnCount() to return 4, but this always fails.
    45. return tr("File Attribute");
    46. }
    47. return QDirModel::data(index, role);
    48. }
    49.  
    50.  
    51. QVariant FileModel::headerData(int section, Qt::Orientation orientation, int role) const
    52. {
    53. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    54. {
    55. switch (section)
    56. {
    57. case 4:
    58. return tr("File Attribute Header");
    59. default:
    60. return QDirModel::headerData(section, orientation, role);
    61. }
    62. }
    63. return QVariant();
    64. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    Works for me after fixing all the compilation errors:

    PS. What's the point overriding all those index, parent etc. methods? By the way, one of them is not even virtual.
    Attached Images Attached Images
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    killerwookie99 (13th August 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    Sorry about the compilation errors, the code is on a machine that's hard to get things from. What version of Qt are you using?

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

    Default Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    I'm using Qt/X11 4.4.0.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    killerwookie99 (13th August 2008)

  7. #5
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    I am using 4.3.2 perhaps worth an upgrade?

    I just remember reading to implement all those functions...for now I just put the calls to QDirModel...what are some things you would recommend as I am only just starting with Qt.

    Appreciate your quick responses as well!

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

    Default Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    Hmm, does it change anything if you implement columnCount() like this:
    Qt Code:
    1. int FileModel::columnCount(const QModelIndex &parent) const
    2. {
    3. int count = QDirModel::columnCount(parent);
    4. if (count > 0)
    5. {
    6. count++; // Adding one column to QDirModel
    7. }
    8. return count;
    9. }
    To copy to clipboard, switch view to plain text mode 
    ?
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    killerwookie99 (13th August 2008)

  10. #7
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    No that did not change it, the other curious thing is that it only hits that function maybe a total of 15 times for a listing of 1000 files...

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

    Default Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    How about simply returning QDirModel::columnCount()+1? Or even "5"?

  12. #9
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Subclassing QDirModel to Add Extra Columns Results in Empty Columns!

    Ok, the problem was Qt 4.3.2 and maybe other versions. I am on solaris I upgraded yesterday (on a sparc! ) Anyway, you are correct it works. Should've asked the night before I kept thinking it was something I was doing, but all the reading was probably a good idea anyway. Thanks y'all!

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.