PDA

View Full Version : finding objects in a QTableWidgetItem



winkle99
16th December 2009, 03:33
I've got a cell in a QTableWidget that holds a number of images. In my delegat for that row I call drawImage.



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();
}


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 )
{
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 );
}



If there's anything I can clarify let me know. All help is appreciated.

spirit
16th December 2009, 06:13
did you see QTableWidget::itemAt?

winkle99
16th December 2009, 19:48
Yes. I've used itemAt. What I'm trying to do now is examine the elements being drawn inside of the item itself.