Results 1 to 3 of 3

Thread: finding objects in a QTableWidgetItem

  1. #1
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question finding objects in a QTableWidgetItem

    I've got a cell in a QTableWidget that holds a number of images. In my delegat for that row I call drawImage.

    Qt Code:
    1. void DescriptionDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
    2. {
    3. painter->save();
    4. //
    5. // code to calculate the QRect for drawing the image as well as the code to build the image
    6. //
    7. QFrame *aFrame = new QFrame(); // qobject_cast wouldn't deal with QImage
    8. aFrame->setGeometry( iconRect );
    9.  
    10. QImage theImage;
    11. theImage.loadFromData( imageData, "PNG");
    12. painter->drawImage( iconRect, theImage);
    13. painter->restore();
    14. }
    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?

    Qt Code:
    1. QVariant ImageItem::data( int role ) const
    2. {
    3. if ( role == Qt::ToolTipRole )
    4. {
    5. QPoint pos = QCursor::pos();
    6. QWidget* curWidget = QApplication::widgetAt( pos );
    7. //
    8. // according to debug information curWidget is a "qt_scrollarea_viewport"
    9. //
    10. QList<QVariant> myData = QTableWidgetItem::data( Qt::ToolTipRole ).toList(); // the list of strings to choose from
    11. //
    12. // note: this method looks at a QFrame because the QImage is not derived from QObject so qobject_cast didn't work
    13. //
    14. QFrame *anImg = qobject_cast<QFrame*> curWidget
    15. if ( anImg )
    16. {
    17. qDebug() << "huzzah. Found it.";
    18. }
    19. else
    20. {
    21. qDebug() << "widget At didn't work :-(";
    22. }
    23. return QVariant("Dummy data because I cannott pick string.");
    24. }
    25.  
    26. return QTableWidgetItem::data( role );
    27. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: finding objects in a QTableWidgetItem

    did you see QTableWidget::itemAt?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: finding objects in a QTableWidgetItem

    Yes. I've used itemAt. What I'm trying to do now is examine the elements being drawn inside of the item itself.

Similar Threads

  1. QWizard creating objects dynamically
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 15:43
  2. objects modelling
    By mickey in forum General Discussion
    Replies: 1
    Last Post: 12th April 2008, 08:14
  3. Ole objects in Qt(MS OFFice)
    By mchara in forum Qt Programming
    Replies: 9
    Last Post: 26th September 2007, 07:07
  4. Replies: 1
    Last Post: 21st August 2007, 16:25
  5. Custom objects in a QGraphicScene
    By draand in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 11:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.