Results 1 to 8 of 8

Thread: Cusomize QFileDialog: add new column to the file view of the QFileDialog

  1. #1
    Join Date
    Mar 2014
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Cusomize QFileDialog: add new column to the file view of the QFileDialog

    I want to add a custom column to the (table view) of the QFileDialog. Therefore I use the method "setProxyModel" of the class QFileDialog.

    Code:
    Qt Code:
    1. class QFileDialogProxyModel : public QSortFilterProxyModel{
    2. public:
    3. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    4. Qt::ItemFlags flags(const QModelIndex &index) const;
    5. virtual int columnCount ( const QModelIndex & parent = QModelIndex() );
    6. virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
    7. };
    8.  
    9. QVariant QFileDialogProxyModel::data(const QModelIndex &index, int role/* = Qt::DisplayRole*/) const {
    10. if (!index.isValid())
    11. return QVariant();
    12. if (role == Qt::DisplayRole && index.column() == 4)
    13. return QString::number(rand() % 10); // add custom column! at the moment a random number
    14. return QSortFilterProxyModel::data(index, role);
    15. }
    16.  
    17. Qt::ItemFlags QFileDialogProxyModel::flags(const QModelIndex &index) const {
    18. if (!index.isValid())
    19. return 0;
    20. if (index.column() == 4)
    21. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    22. return QSortFilterProxyModel::flags(index);
    23. }
    24.  
    25. QVariant QFileDialogProxyModel::headerData ( int section, Qt::Orientation orientation, int role/* = Qt::DisplayRole */) const{
    26. if (section == 4 && orientation == Qt::Horizontal && role == Qt::DisplayRole)
    27. return tr("Notifications");
    28. return QSortFilterProxyModel::headerData(section, orientation, role);
    29. }
    30.  
    31. int QFileDialogProxyModel::columnCount ( const QModelIndex & parent/* = QModelIndex() */){
    32. return (parent.column() > 0) ? 0 : 5; //
    33. }
    34.  
    35. int main(int argc, char *argv[]){
    36. QApplication app(argc, argv);
    37. QFileDialog customDialog;
    38. customDialog.setProxyModel(new QFileDialogProxyModel());
    39. customDialog.exec();
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    Just the filenames/order ("name"), size, type and data modified are displayed.
    Desired is a additional column with the header name "Notifications". The contents should be contain random numbers.
    Hopefully anybody can help me.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog

    Your columnCount() method signature does not match the one from QAbstractItemModel, i.e. it is missing the const.

    As far as C++ goes that is an overload and not invoked when the model's column count is to be evaluated. That still calls the one from QSortFilterProxyModel.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog

    Thank you for helping me.
    Now the new column is displaying. With the name: "Notifications". Perfect.
    But no contents of the new column is displaying. I expects that there are displaying random numbers.
    In the overwritten
    QVariant QFileDialogProxyModel::data(const QModelIndex &index, int role/* = Qt:isplayRole*/) const[LIST]
    method I have added a breakpoint to
    return QString::number(rand() % 10); // add custom column! at the moment a random number
    but the Debugger do not stop here.
    And I cannot choose a file anymore. I can select a file, but no file is displayed in the "File Name" QLineEdit and the "OK" Button is disabled.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog

    Does your data() method get called at all?
    Its signature looks right, so it should be.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2014
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog

    Yes, method:
    QVariant QFileDialogProxyModel::data(const QModelIndex &index, int role/* = Qt:isplayRole*/)
    is called, but I don´t get a index (index.column() == 4) and probably therefore my columns are empty.
    Should I call createIndex and create a new QModelIndex which will have a column==4?
    But I think, that I will modify the main model and not the proxy model.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog

    Yes, try overwriting the index() method and handle creation of indexes for your column yourself.
    Another thing, just to make sure it isn't he cause, would be to always return 5 in the columnCount method,

    Cheers,
    _

  7. #7
    Join Date
    Mar 2014
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog

    I have overwritten the index() method and handle the custom column in the proxy model itself.
    Now a custom column is displayed with a custom header and custom contents. perfect.
    But I have another problem with the selection of a file.
    I cannot choose a file anymore. I can select a file, but no file is displayed in the "File Name" QLineEdit and the "OK" Button is disabled.
    The following methods are also overwritten in the QFileDialogProxyModel class: (the signature i have already checked)
    Qt Code:
    1. virtual QItemSelection mapSelectionToSource ( const QItemSelection & proxySelection ) const;
    2. virtual QItemSelection mapSelectionFromSource ( const QItemSelection & sourceSelection ) const;
    3.  
    4. QItemSelection QFileDialogProxyModel::mapSelectionFromSource ( const QItemSelection & sourceSelection ) const{
    5. return QSortFilterProxyModel::mapSelectionFromSource(sourceSelection);
    6. }
    7.  
    8.  
    9.  
    10. QItemSelection QFileDialogProxyModel::mapSelectionToSource ( const QItemSelection & proxySelection ) const{
    11. return QSortFilterProxyModel::mapSelectionToSource(proxySelection);
    12. }
    To copy to clipboard, switch view to plain text mode 
    but the debugger doesn´t stop here at no time.
    I expected that I get a debugger stop at Line 5 or Line 11.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog

    Maybe also needs to implement mapFromSource/mapToSource?

    Cheers,
    _

Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 11:23
  2. QFileDialog and file extension
    By calhal in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2010, 13:54
  3. QFileDialog and file sequences
    By peterhillman in forum Qt Programming
    Replies: 5
    Last Post: 24th November 2007, 11:16
  4. QFileDialog no thumnail view?
    By sincnarf in forum Qt Programming
    Replies: 8
    Last Post: 14th October 2007, 10:51
  5. copy file/s from QFileDialog
    By raphaelf in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 15:26

Tags for this 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.