Results 1 to 3 of 3

Thread: [Resolved] How can I add a column with Image into the model

  1. #1
    Join Date
    Feb 2009
    Location
    Chengdu, China
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question [Resolved] How can I add a column with Image into the model

    Hi everyone,
    I have rewrite the QDirModel by myslef. I want to add a column to show some images.

    In the QDirModel, there is only 'Name', 'Size', 'Type', 'Date Modified'. And all of them are QString type.
    Qt Code:
    1. QString name(const QModelIndex &index) const;
    2. QString size(const QModelIndex &index) const;
    3. QString type(const QModelIndex &index) const;
    4. QString time(const QModelIndex &index) const;
    5.  
    6. case 0: return d->name(index);
    7. case 1: return d->size(index);
    8. case 2: return d->type(index);
    9. case 3: return d->time(index);
    To copy to clipboard, switch view to plain text mode 

    So, I want to know what type can I set that it can show the images in the additional column. I have tried 'QPixmap' and 'QIcon', but they are not work.

    What can I do? I need your help. Thanks.
    Last edited by lovehouye; 12th February 2009 at 04:58. Reason: Resolved

  2. #2
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I add a column with Image into the model

    Qt Code:
    1. QVariant QDirModel::data(const QModelIndex &index, int role) const
    2. {
    3. Q_D(const QDirModel);
    4. if (!d->indexValid(index))
    5. return QVariant();
    6.  
    7. if (role == Qt::DisplayRole || role == Qt::EditRole) {
    8. switch (index.column()) {
    9. case 0: return d->name(index);
    10. case 1: return d->size(index);
    11. case 2: return d->type(index);
    12. case 3: return d->time(index);
    13. // you can return icons if the role == Qt::DecorationRole
    14. //or else you can return icons if the role == Qt::DecorationRole and the index is in the column you want to present icons
    15. default:
    16. qWarning("data: invalid display value column %d", index.column());
    17. return QVariant();
    18. }
    19. }
    20.  
    21. if (index.column() == 0) {
    22. if (role == FileIconRole)
    23. return fileIcon(index);
    24. if (role == FilePathRole)
    25. return filePath(index);
    26. if (role == FileNameRole)
    27. return fileName(index);
    28. }
    29.  
    30. if (index.column() == 1 && Qt::TextAlignmentRole == role) {
    31. return Qt::AlignRight;
    32. }
    33. return QVariant();
    34. }
    To copy to clipboard, switch view to plain text mode 

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

    talk2amulya (12th February 2009)

  4. #3
    Join Date
    Feb 2009
    Location
    Chengdu, China
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I add a column with Image into the model

    Quote Originally Posted by nifei View Post
    Qt Code:
    1. QVariant QDirModel::data(const QModelIndex &index, int role) const
    2. {
    3. Q_D(const QDirModel);
    4. if (!d->indexValid(index))
    5. return QVariant();
    6.  
    7. if (role == Qt::DisplayRole || role == Qt::EditRole) {
    8. switch (index.column()) {
    9. case 0: return d->name(index);
    10. case 1: return d->size(index);
    11. case 2: return d->type(index);
    12. case 3: return d->time(index);
    13. // you can return icons if the role == Qt::DecorationRole
    14. //or else you can return icons if the role == Qt::DecorationRole and the index is in the column you want to present icons
    15. default:
    16. qWarning("data: invalid display value column %d", index.column());
    17. return QVariant();
    18. }
    19. }
    20.  
    21. if (index.column() == 0) {
    22. if (role == FileIconRole)
    23. return fileIcon(index);
    24. if (role == FilePathRole)
    25. return filePath(index);
    26. if (role == FileNameRole)
    27. return fileName(index);
    28. }
    29.  
    30. if (index.column() == 1 && Qt::TextAlignmentRole == role) {
    31. return Qt::AlignRight;
    32. }
    33. return QVariant();
    34. }
    To copy to clipboard, switch view to plain text mode 
    Oh, you're so kind and it works perfect.
    Thanks very much.

Similar Threads

  1. Replies: 1
    Last Post: 23rd November 2008, 14:11
  2. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15:36
  3. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  4. Replies: 0
    Last Post: 10th November 2006, 13:46
  5. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 08:00

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.