I've got a cell in a QTableWidget that holds a number of images. In my delegat for that row I call drawImage.
{
painter->save();
//
// code to calculate the QRect for drawing the image as well as the code to build the image
//
QFrame *aFrame
= new QFrame();
// qobject_cast wouldn't deal with QImage aFrame->setGeometry( iconRect );
theImage.loadFromData( imageData, "PNG");
painter->drawImage( iconRect, theImage);
painter->restore();
}
void DescriptionDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
{
painter->save();
//
// code to calculate the QRect for drawing the image as well as the code to build the image
//
QFrame *aFrame = new QFrame(); // qobject_cast wouldn't deal with QImage
aFrame->setGeometry( iconRect );
QImage theImage;
theImage.loadFromData( imageData, "PNG");
painter->drawImage( iconRect, theImage);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
This works exactly as I desire. The problem comes when I want to display a different tooltip based on which icon the cursor is currently hovering over. I have a class derived from QTableWidgetItem and in my data method I'm trying to figure out where the cursor is (i.e. which QImage) so that I can select the correct string to return. Below is the current Data method. It's pretty sparce because none of my approaches so far have given me information about what's under the cursor.
Can anyone help? Is this possible?
QVariant ImageItem
::data( int role
) const {
if ( role == Qt::ToolTipRole )
{
//
// according to debug information curWidget is a "qt_scrollarea_viewport"
//
QList<QVariant> myData
= QTableWidgetItem::data( Qt
::ToolTipRole ).
toList();
// the list of strings to choose from//
// note: this method looks at a QFrame because the QImage is not derived from QObject so qobject_cast didn't work
//
QFrame *anImg
= qobject_cast<QFrame
*> curWidget
if ( anImg )
{
qDebug() << "huzzah. Found it.";
}
else
{
qDebug() << "widget At didn't work :-(";
}
return QVariant("Dummy data because I cannott pick string.");
}
}
QVariant ImageItem::data( int role ) const
{
if ( role == Qt::ToolTipRole )
{
QPoint pos = QCursor::pos();
QWidget* curWidget = QApplication::widgetAt( pos );
//
// according to debug information curWidget is a "qt_scrollarea_viewport"
//
QList<QVariant> myData = QTableWidgetItem::data( Qt::ToolTipRole ).toList(); // the list of strings to choose from
//
// note: this method looks at a QFrame because the QImage is not derived from QObject so qobject_cast didn't work
//
QFrame *anImg = qobject_cast<QFrame*> curWidget
if ( anImg )
{
qDebug() << "huzzah. Found it.";
}
else
{
qDebug() << "widget At didn't work :-(";
}
return QVariant("Dummy data because I cannott pick string.");
}
return QTableWidgetItem::data( role );
}
To copy to clipboard, switch view to plain text mode
If there's anything I can clarify let me know. All help is appreciated.
Bookmarks