Changing an image using QStyledItemDelegate in TableView
-
wrote on 16 Jan 2017, 08:28 last edited by
I posted the complete code above that takes care of everything, just use that one.
if((index.column () == 3))
The delegate should never have this kind of code inside. The delegate should not depend on the model. use
setItemDelegateForColumn
in the view to obtain that result -
I posted the complete code above that takes care of everything, just use that one.
if((index.column () == 3))
The delegate should never have this kind of code inside. The delegate should not depend on the model. use
setItemDelegateForColumn
in the view to obtain that resultwrote on 17 Jan 2017, 04:14 last edited by gabor53@VRonin
When I try to implement your code I end up with different error messages.
If I leave everything in the .h file like this:virtual void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE { ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); if(imgPick->selectedFile().isEmpty()) { model->setData(index,QVariant(),Qt::UserRole); model->setData(index, QVariant(),Qt::DecorationRole); } else { model->setData(index,imgPick->selectedFile(),Qt::UserRole); model->setData(index, QIcon(imgPick->selectedFile()),Qt::DecorationRole); } } virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE { ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); if(imgPick->selectedFile().isEmpty()) { model->setData(index,QVariant(),Qt::UserRole); model->setData(index, QVariant(),Qt::DecorationRole); } else { model->setData(index,imgPick->selectedFile(),Qt::UserRole); model->setData(index, QIcon(imgPick->selectedFile()),Qt::DecorationRole); } }
I get an error message saying expected token ';' got 'const'. If I move the definition to the cpp file I get an error saying unexpected token ImagePickButton. What am I doing incorrectly? Thank you
-
wrote on 17 Jan 2017, 08:05 last edited by
Looks like you forgot some
#include
s, added them to my original post -
wrote on 19 Jan 2017, 04:16 last edited by
@VRonin
I have in the .h file:virtual void setEditorData(QWidget *editor, const QModelIndex &index);
In the .cpp:
void ImageDelegate::setEditorData(QWidget *editor, const QModelIndex &index) { ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); imgPick->setSelectedFile(index.data(Qt::UserRole)); }
It gives me the following error message:
C:\Programming\Projects\Folkfriends_1_0\imagedelegate.cpp:18: error: no matching function for call to 'ImagePickButton::setSelectedFile(QVariant)'
imgPick->setSelectedFile(index.data(Qt::UserRole));^
C:\Programming\Projects\Folkfriends_1_0\imagedelegate.h:4: In file included from ..\Folkfriends_1_0\imagedelegate.h:4:0,
C:\Programming\Projects\Folkfriends_1_0\imagedelegate.cpp:1: from ..\Folkfriends_1_0\imagedelegate.cpp:1:
C:\Programming\Projects\Folkfriends_1_0\imagepickbutton.h:18: candidate: void ImagePickButton::setSelectedFile(const QString&)
void setSelectedFile(const QString& val);
^
C:\Programming\Projects\Folkfriends_1_0\imagepickbutton.h:18: note: no known conversion for argument 1 from 'QVariant' to 'const QString&'What should I change? Everything else builds correctly.
-
@gabor53
setSelectedFile
expects aQString
parameter so convert theQVariant
returned bydata
toQString
. -
wrote on 19 Jan 2017, 08:01 last edited by
easy fix
imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
-
wrote on 20 Jan 2017, 04:07 last edited by
@VRonin
Thank you. This part works now. I addedImagePickButton *myImagePickButton = new ImagePickButton(this); myImagePickButton->show();
to mydeelgate.cpp createEditor.
When the user clicks on the image now it creates the button saying "Select Image". But this button is not on the view; it is separated from it and appears in the top left corner of the screen. How can I add it to the view so the button is on the actual image the user is trying to change? Thank you.
-
wrote on 20 Jan 2017, 05:52 last edited by
do not call
show()
just return that widget from that method. Again I'd advise you just to use the code I posted above even for the delegate -
do not call
show()
just return that widget from that method. Again I'd advise you just to use the code I posted above even for the delegatewrote on 21 Jan 2017, 03:46 last edited by@VRonin
I definitely want to use the code you posted. Should I call the method returning the widget from myDelegate.cpp or from fixdb.cpp where the model is set up? Currently I have this:ui->tableView_Fix->setItemDelegate (new myDelegate);
This declares that the delegate will be myDelegate from now on for everything. Can this work somehow ?
ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate); ui->tableView_Fix->setItemDelegate (new myDelegate);
Saying that if column 3 is chosen go with ImageDelegate; all other cases myDelegate.
It this is the way, what is missing? Currently it opens a text window in column 4). Thank you. -
wrote on 21 Jan 2017, 15:30 last edited by
do it the other way around and do not leak memory:
ui->tableView_Fix->setItemDelegate (new myDelegate(this)); ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
-
do it the other way around and do not leak memory:
ui->tableView_Fix->setItemDelegate (new myDelegate(this)); ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
wrote on 21 Jan 2017, 18:13 last edited by gabor53This post is deleted! -
do it the other way around and do not leak memory:
ui->tableView_Fix->setItemDelegate (new myDelegate(this)); ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
wrote on 26 Jan 2017, 04:31 last edited by@VRonin
So far I came across 2 problems.- If I add
ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
to fixdb.cpp , nothing happens, because myDelegate.cpp evaluates which cell the user clicked on. I can add
return new ImagePickButton(parent);
to myDelegate createEditor and it returns the button, but I couldn't figure out how to connect imageDelegate to the existing code. Please advise which is the best solution.
- The button is about 10px smaller on the top and on the left than the original cell. How can I make it bigger to cover the size of the original cell (originally its 100 x 100). It doesn't respond to any of the size settings.
Thank you for your help.
19/21