PDA

View Full Version : Cusomize QFileDialog: add new column to the file view of the QFileDialog



polynom
5th March 2014, 08:36
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:


class QFileDialogProxyModel : public QSortFilterProxyModel{
public:
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
virtual int columnCount ( const QModelIndex & parent = QModelIndex() );
virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
};

QVariant QFileDialogProxyModel::data(const QModelIndex &index, int role/* = Qt::DisplayRole*/) const {
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole && index.column() == 4)
return QString::number(rand() % 10); // add custom column! at the moment a random number
return QSortFilterProxyModel::data(index, role);
}

Qt::ItemFlags QFileDialogProxyModel::flags(const QModelIndex &index) const {
if (!index.isValid())
return 0;
if (index.column() == 4)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
return QSortFilterProxyModel::flags(index);
}

QVariant QFileDialogProxyModel::headerData ( int section, Qt::Orientation orientation, int role/* = Qt::DisplayRole */) const{
if (section == 4 && orientation == Qt::Horizontal && role == Qt::DisplayRole)
return tr("Notifications");
return QSortFilterProxyModel::headerData(section, orientation, role);
}

int QFileDialogProxyModel::columnCount ( const QModelIndex & parent/* = QModelIndex() */){
return (parent.column() > 0) ? 0 : 5; //
}

int main(int argc, char *argv[]){
QApplication app(argc, argv);
QFileDialog customDialog;
customDialog.setProxyModel(new QFileDialogProxyModel());
customDialog.exec();
return 0;
}


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.

anda_skoa
5th March 2014, 09:31
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,
_

polynom
5th March 2014, 10:53
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::DisplayRole*/) 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.

anda_skoa
5th March 2014, 11:21
Does your data() method get called at all?
Its signature looks right, so it should be.

Cheers,
_

polynom
5th March 2014, 11:42
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.

anda_skoa
5th March 2014, 12:21
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,
_

polynom
5th March 2014, 16:47
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)


virtual QItemSelection mapSelectionToSource ( const QItemSelection & proxySelection ) const;
virtual QItemSelection mapSelectionFromSource ( const QItemSelection & sourceSelection ) const;

QItemSelection QFileDialogProxyModel::mapSelectionFromSource ( const QItemSelection & sourceSelection ) const{
return QSortFilterProxyModel::mapSelectionFromSource(sour ceSelection);
}



QItemSelection QFileDialogProxyModel::mapSelectionToSource ( const QItemSelection & proxySelection ) const{
return QSortFilterProxyModel::mapSelectionToSource(proxyS election);
}

but the debugger doesn´t stop here at no time.
I expected that I get a debugger stop at Line 5 or Line 11.

anda_skoa
5th March 2014, 17:36
Maybe also needs to implement mapFromSource/mapToSource?

Cheers,
_