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:
Code:
public:
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())
if (role == Qt::DisplayRole && index.column() == 4)
return QString::number(rand() % 10);
// add custom column! at the moment a random number }
Qt
::ItemFlags QFileDialogProxyModel
::flags(const QModelIndex &index
) const { if (!index.isValid())
return 0;
if (index.column() == 4)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
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");
}
int QFileDialogProxyModel
::columnCount ( const QModelIndex & parent
/* = QModelIndex() */){ return (parent.column() > 0) ? 0 : 5; //
}
int main(int argc, char *argv[]){
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.
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,
_
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::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.
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,
_
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.
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,
_
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)
but the debugger doesn´t stop here at no time.
I expected that I get a debugger stop at Line 5 or Line 11.
Re: Cusomize QFileDialog: add new column to the file view of the QFileDialog
Maybe also needs to implement mapFromSource/mapToSource?
Cheers,
_