Results 1 to 8 of 8

Thread: Identify QImage

  1. #1
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Identify QImage

    Hi,
    I am creating mutliple QImages in a reSizeEvent which is same as Scribble Example. In Scribble example, they create only one QImage.
    On MousePress, how to identify which QImage it is? Is it possible to assign any object name while creating like setObjectName. Please help me.
    Thank you.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Identify QImage

    how to identify which QImage it is?
    Can't you use QImage variable itself to identify it.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Identify QImage

    My QImage variable is same for all images.
    Can i get which image it is on mousepress??
    Thank you

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Identify QImage

    My QImage variable is same for all images.
    Can i get which image it is on mousepress??
    Well you can know where the mouse was clicked on the widget, and based on this you need to back-calculate the image which was drawn at that position/region.

    If you need to handle mouse click for individual images, then QWidget's paint() function not a good way, rather you should be using QGraphicsView framework.


    BTW
    I am creating mutliple QImages in a reSizeEvent which is same as Scribble Example
    Scribble example does not create multiple QImages in resizeEvent().
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Identify QImage

    I am able to get the object name of widget. But how to get the image details.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Identify QImage

    You cannot get the image name (like widget). You will need to implement a tracking mecahnism where the image was drawn, and with what dimention and back-calculate from the mouse click position. There are no direct Qt classes / functions to do so.

    In simple words it is possble but is not stright forward and may end by being complex logic. It is better to use QGraphcisView framework to keep things simple.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Mar 2013
    Posts
    43
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Identify QImage

    can u give me any sample using QGraaphicsView framework.

    Thank you

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Identify QImage

    With the available information this the example I can give quickly.

    Qt Code:
    1. #include <QtGui>
    2. #include <QtWidgets> // Comment this if using Qt4
    3. #include <QApplication>
    4.  
    5. class GraphicsPixmapItem : public QGraphicsPixmapItem
    6. {
    7. public:
    8. explicit GraphicsPixmapItem(const QPixmap & pixmap, QGraphicsItem * parent = 0)
    9. : QGraphicsPixmapItem(pixmap, parent)
    10. , mName("GraphicsPixmapItem") { }
    11.  
    12. enum { Type = UserType + 1 };
    13.  
    14. int type(void) const { return Type; }
    15.  
    16. QString name(void) const { return mName; }
    17.  
    18. void setName(const QString & name) { mName = name; }
    19.  
    20. protected:
    21. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    22. {
    23. painter->drawRect(option->rect);
    24. return QGraphicsPixmapItem::paint(painter, option, widget);
    25. }
    26.  
    27. private:
    28. QString mName;
    29. };
    30.  
    31. class GraphicsScene : public QGraphicsScene
    32. {
    33. public:
    34. explicit GraphicsScene(QObject * parent = 0)
    35. : QGraphicsScene(parent)
    36. , mGraphicsTextItem(addText("No Selection")) { }
    37. protected:
    38. void mousePressEvent(QGraphicsSceneMouseEvent * event)
    39. {
    40. GraphicsPixmapItem * item = qgraphicsitem_cast<GraphicsPixmapItem *>(itemAt(event->scenePos(), QTransform()));
    41.  
    42. if(item)
    43. mGraphicsTextItem->setPlainText(item->name());
    44. else
    45. mGraphicsTextItem->setPlainText("Click on GraphicsPixmapItem");
    46. }
    47.  
    48. private:
    49. QGraphicsTextItem * mGraphicsTextItem;
    50. };
    51.  
    52. int main(int argc, char *argv[])
    53. {
    54. QApplication app(argc, argv);
    55.  
    56. QGraphicsView graphicsView;
    57. graphicsView.showMaximized();
    58.  
    59. GraphicsScene graphicsScene;
    60. graphicsView.setScene(&graphicsScene);
    61.  
    62. GraphicsPixmapItem * pix1 = new GraphicsPixmapItem(QPixmap("logo1.png"));
    63. GraphicsPixmapItem * pix2 = new GraphicsPixmapItem(QPixmap("logo2.png"));
    64.  
    65. pix1->setPos(50,50);
    66. pix2->setPos(200,50);
    67.  
    68. pix1->setName("Pixmap - 1");
    69. pix2->setName("Pixmap - 2");
    70.  
    71. graphicsScene.addItem(pix1);
    72. graphicsScene.addItem(pix2);
    73.  
    74. return app.exec();
    75. }
    To copy to clipboard, switch view to plain text mode 

    As an alternate to custom GraphicsPixmapItem, you can also use QGraphicsItem::setData(int key, const QVariant & value), in fact it will be more simple to use QGraphicsItem::setData() and QGraphicsItem::data().
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. The following user says thank you to Santosh Reddy for this useful post:

    mythili (24th May 2013)

Similar Threads

  1. Identify mimetype
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 7th March 2011, 06:16
  2. How to identify the control
    By chandru@080 in forum Newbie
    Replies: 0
    Last Post: 30th November 2010, 10:28
  3. How to identify Num pad keys
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2008, 17:43
  4. identify QGraphicItem
    By avis_phoenix in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2007, 06:16
  5. How to identify the process
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2007, 16:18

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
  •  
Qt is a trademark of The Qt Company.