Unsolved QTableView and Image
-
thank you @VRonin , It was a mistake of mine ,that I didn't provide the complete information.
I'm subclassing QItemDelegate....on this note how its different from subclassing QStyledItemDelegate?? I mean Qt document says thatQStyledItemDelegate uses the current style to paint its items
whats the current style here !!
QSize myDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{ return QSize newSize(70,30); }
QImage m_image(":/new/prefix1/d.jpg");
m_image is in private of myDelegate.
-
So
m_image
does not depend at all on what's in the model?@MokJ said in QTableView and Image:
whats the current style here !!
http://doc.qt.io/qt-5/style-reference.html
Let's go step by step. The below should be an improvement already, correct?
void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ (option.widget ? option.widget.style() : QApplication::style())->drawItemPixmap(painter,option.rect(),QPixmap::fromImage(m_image.scaled(option.rect().size()))); }
-
@VRonin Thanks
yes, m_image doesn't depend on model at all.
I'm clear about current style now.Let's go step by step. The below should be an improvement already, correct?
yes , after a small change It has improved.
So whats the next step !
What method should be used to get desired output !@MokJ said in QTableView and Image:
Another thing is , is it possible to create something like, whenever a block from QTableView is selected ,the image on that particular block should get changed?
-
@MokJ said in QTableView and Image:
So whats the next step !
We make the image depend on the model.
In the constructor add:connect(view->selectionModel(),&QItemSelectionModel::selectionChanged,[view](const QItemSelection &selected, const QItemSelection &deselected)->void{ QModelIndexList indexList = selected.indexes(); for(auto& index : qAsConst(indexList)) view->model()->setData(index,true,Qt::UserRole); indexList = deselected.indexes(); for(auto& index : qAsConst(indexList)) view->model()->setData(index,QVariant(),Qt::UserRole); });
now in
myDelegate::paint
you can addif(index.data(Qt::UserRole).toBool())
to check if an item is selected or not -
@VRonin Thank you
I have added that snippet too, now question is , How is it making m_image depend on model ? -
@MokJ I made a mistake,
if(index.data().toBool())
should have beenif(index.data(Qt::UserRole).toBool())
fixed above nowP.S.
if(index.data(Qt::UserRole).isValid())
does the same and it's probably more efficient -
@VRonin
I'm a bit confused about how its supposed to work. I mean once I select an Index it goes insideif(index.data(Qt::UserRole).isValid()) {}
What should get implemented in If?
I appreciate your help greatly.
-
@VRonin
can we take the Image as string ? -
-