PDA

View Full Version : Call QFileDialog when trying to edit cell of QTableView, put file name after return.



toglia3d
12th October 2010, 18:44
Hi, In my QTableView one of my columns refers to a file name, so when editing a cell of this column I would like to call a QFileDialog to make the user choose the file. And I know this can be done with a QItemDelegate.

So, this is my createEditor attempt:

QWidget *createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index
) const {
Q_UNUSED(option);
Q_UNUSED(index);

QFileDialog* editor = new QFileDialog(parent);
editor->setFilter("*.png");
editor->setDirectory(mResources);

return editor;
};

And actually this does work. But I still wish I could use getOpenFileName to get the native dialog which looks cooler and also shows a preview of the images.

So is there a better way of doing this that I can use getOpenFileName method, maybe attaching some signals or slots?

Thanks for any help.

toglia3d
12th October 2010, 19:31
I can't even resize the dialog window. Something weird is happening when I return the editor that's changing the geometry of the QFileDialog.



QWidget *createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index
) const {
Q_UNUSED(option);
Q_UNUSED(index);

QFileDialog* editor = new QFileDialog(parent);
editor->setFilter("*.png");
editor->setDirectory(mResources);
editor->setGeometry(0,0,1000,500);
editor->exec() // <--- big file dialog;

return editor; // <--- tiny file dialog;
};

toglia3d
12th October 2010, 20:50
OK so the editor->setGeometry method has to go in the override method setEditorData of the QItemDelegate.

Does anyone know of an example code where the setItemDelegate is used to paint the thumbnail preview of the images in the QFileDialog?