Results 1 to 18 of 18

Thread: How to find out the type of the clicked item..

  1. #1
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to find out the type of the clicked item..

    Hi..

    I have mutiple items drawn on the canvas... they are either line or rectangles..
    I want to find out which exact item i have clicked on... ie i item on which i have clicked is line or rect.. how do i find that out...

    Thanking you,
    Kapil

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    The following functions will help you with your task

    Q3CanvasItemList collisions ( const QPoint & p ) const (in Q3Canvas)
    virtual int rtti () const (in Q3CanvasItem)

  3. #3
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Quote Originally Posted by munna
    The following functions will help you with your task

    Q3CanvasItemList collisions ( const QPoint & p ) const (in Q3Canvas)
    virtual int rtti () const (in Q3CanvasItem)
    Hi..

    Thanks..

    Yaa true.. they will help me in finding out the item i have clicked on...

    But i have 10 Line objects and 20 rectangle object.. Now of them whcih Line object has been clicked or which of the Rectangle object has been clicked is my question...

    ie is it the 3rd line or the 5th.. or it is the 3rd rect or the 8th... How to retrieve this specific info.. do i have to write my own functions that would reduce to find out the information..

    Kapil

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    In that case you can maintain a list of items for comparision. You can have a list of all the lines, a list of all the rectangles and so on... Once you get the item that was selected or clicked, first find out if it was a line or rectangle.. and then compare the selected item with the appropriate list items to find out which line or rectangle was selected.

    Hope you understood!!

  5. #5
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Quote Originally Posted by munna
    In that case you can maintain a list of items for comparision. You can have a list of all the lines, a list of all the rectangles and so on... Once you get the item that was selected or clicked, first find out if it was a line or rectangle.. and then compare the selected item with the appropriate list items to find out which line or rectangle was selected.

    Hope you understood!!
    Thanks a lot...

    About my understanding i would just let you know what i deduced ->

    I would have to maintain a list wherein i would have to keep the entire information of lines and rectangles and their exact locations... and whereever i have clicked, i would have to find the position and compare it with the line or rect location..

    Is it what u wanted to say..

    what are the other ways in which comparisons can be made ????

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    Quote Originally Posted by kapil
    I would have to maintain a list wherein i would have to keep the entire information of lines and rectangles and their exact locations...
    you just have to maintain a list of pointers of your items. Their locations are not needed.

  7. #7
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Quote Originally Posted by munna
    you just have to maintain a list of pointers of your items. Their locations are not needed.
    Hi...

    Yaa i have done that... i have the list of all the line and rect pointers with me...
    But how will i know that the item i have clicked on is the "nth" line or "nth" rectangle....

    can u give me some example or few lines of code that explains the thing...

    it would be really very helpful for me...

    Thanking you...

    Kapil
    Last edited by Kapil; 30th March 2006 at 12:22.

  8. #8
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    ok here it is

    Qt Code:
    1. QList<Q3CanvasItem *> lineList;
    2. QList<Q3CanvasItem *> rectList;
    3.  
    4. Q3CanvasItem *l = new Q3CanvasLine(yourCanvas);
    5. lineList.append(l);
    6.  
    7. //Similarly Build your rectangle list;
    To copy to clipboard, switch view to plain text mode 

    Now to find which line was clicked

    Qt Code:
    1. Q3CanvasItem* item;
    2. // Find an item, e.g. with Q3CanvasItem::collisions().
    3. ...
    4. if (item->rtti() == Rtti_Line) {
    5. for(int i = 0; i< lineList.size(); ++i){
    6. if(lineList.at(i) == item){
    7. //Here you know that you have clicked the line which in the list is at i th location
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Hope this helps

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

    Kapil (30th March 2006)

  10. #9
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Hi...

    yaa i got the basic idea..
    what i have done is this now:

    First the creation of the Line List:
    Qt Code:
    1. QList<Q3CanvasItem *> lineList;
    2.  
    3. Q3CanvasItem **l;
    4. l = new Q3CanvasLine*[numLines]; //numLines is the number of lines on Canvas.
    5. for(i=0;i<numLines;i++)
    6. {
    7. l[i] = new Q3CanvasLine(yourCanvas);
    8. l[i] = canvasLine[i];
    9. lineList.append(l[i]);
    10. }
    11.  
    12. Same for the Rectangle;
    To copy to clipboard, switch view to plain text mode 

    Is it correct...

  11. #10
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    In your code you are actually having two list. One in the form of QList and the other as array. This is not required. One is enough to perform the task.

    Cheers

  12. #11
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Quote Originally Posted by munna
    In your code you are actually having two list. One in the form of QList and the other as array. This is not required. One is enough to perform the task.

    Cheers
    which means this:

    Qt Code:
    1. QList<Q3CanvasItem *> lineList;
    2.  
    3. Q3CanvasItem *l = new Q3CanvasLine(yourCanvas);
    4. l[i] = canvasLine[i];
    5. lineList.append(l[i]);
    To copy to clipboard, switch view to plain text mode 

    is this what you were saying

    Also the line lineList.at(i) == m_itemLine is giving error...

    Its giving the comparison error as m_itemList is not a pointer object whereas the other one is... creating m_itemLine pointer creates problem with the collision statement as it return a non-pointer object value...
    Last edited by Kapil; 30th March 2006 at 14:34.

  13. #12
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    Qt Code:
    1. QList<Q3CanvasItem *> lineList;
    2. Q3CanvasItem *l = new Q3CanvasLine(yourCanvas);
    3. l[i] = canvasLine[i];//WHAT IS THIS LINE FOR ????
    4. lineList.append(l[i]); //This should be lineList.append(l);
    To copy to clipboard, switch view to plain text mode 

    collisions() returns a list. This means that to compare you need to do something like

    Qt Code:
    1. lineList.at(i) == colList.at(j)
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    still gives error and this is what it says..

    [HTML]

    error: no match for 'operator==' in '(((QList<Q3CanvasItem*>*)
    ((ImageDraw*)this)) + 56u)->QList<T>::at [with T = Q3CanvasItem*](i) == Q3ValueL
    ist<T>::at(typename QLinkedList<T>::size_type) [with T = Q3CanvasItem*](i)'

    [/HTML]

  15. #14
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    Can you paste your code ?

  16. #15
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Yaa..

    Here is the code:

    CanvasMouse.cpp This file handles the mouse click events and then finds the point through the collision method and sends the info to the ImageDraw class..
    Qt Code:
    1. #include "canvasmouse.h"
    2. CanvasMouse::CanvasMouse(Q3Canvas *pCanvas, QFrame *frame): Q3CanvasView(frame)
    3. {
    4. m_pCanvas = pCanvas;
    5. Q3CanvasView::setCanvas(m_pCanvas);
    6. m_pCanvas->setBackgroundColor(Qt::white);
    7. m_pCanvas->resize(400, 400);
    8. imgdraw = new ImageDraw(m_pCanvas);
    9. setDraw = false;
    10. startDraw = false;
    11.  
    12. }
    13.  
    14. CanvasMouse::~CanvasMouse()
    15. {
    16. }
    17.  
    18. void CanvasMouse::contentsMousePressEvent(QMouseEvent* e)
    19. {
    20. if(setDraw)
    21. {
    22. if(e->button() == Qt::LeftButton)
    23. {
    24. pPress = inverseWorldMatrix().map(e->pos());
    25. startDraw = true;
    26. }
    27. }
    28.  
    29. }
    30. void CanvasMouse::contentsMouseReleaseEvent(QMouseEvent* e)
    31. {
    32.  
    33. if(setDraw)
    34. {
    35. if((e->button() == Qt::LeftButton) && startDraw)
    36. {
    37. pRelease = inverseWorldMatrix().map(e->pos());
    38. Q3CanvasItemList l = m_pCanvas->collisions(pRelease);;
    39.  
    40. imgdraw->selectLine(l);
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    [B]ImageDraw.cpp [B]: This class does the finding of the line number and rect number. Its the selectLine function that finds the line number.

    Qt Code:
    1. #include "imagedraw.h"
    2. #include <Q3ValueList>
    3.  
    4. ImageDraw::ImageDraw(Q3Canvas *pCanvas): m_pCanvas(pCanvas)
    5. {
    6. drawLine();
    7. drawRectangle();
    8. }
    9. ImageDraw::~ImageDraw()
    10. {
    11. }
    12. void ImageDraw::drawLine()
    13. {
    14. int i;
    15. int xxPoint = 2,xyPoint = 2,yxPoint = 202,yyPoint = 2;
    16. lineNum = 20;
    17. m_pCanvasLine = new Q3CanvasLine*[lineNum];
    18. for(i=0;i<lineNum;i++)
    19. {
    20. xyPoint = (i+1)*4;
    21. yyPoint = xyPoint;
    22. m_pCanvasLine[i] = new Q3CanvasLine(m_pCanvas);
    23. m_pCanvasLine[i]->setPoints(xxPoint,xyPoint,yxPoint,yyPoint);
    24. m_pCanvasLine[i]->show();
    25.  
    26. }
    27.  
    28. m_pLineItem = new Q3CanvasLine(m_pCanvas);
    29. lineList.append(m_pLineItem);
    30.  
    31. }
    32.  
    33. void ImageDraw::drawRectangle()
    34. {
    35. int xPoint = 4, yPoint = 100;
    36. int height = 50, width = 80;
    37. int i;
    38. recNum = 5;
    39. m_pCanvasRectangle = new Q3CanvasRectangle*[recNum];
    40. for(i=0;i<recNum;i++)
    41. {
    42. m_pCanvasRectangle[i] = new Q3CanvasRectangle(xPoint, yPoint, width, height, m_pCanvas);
    43. m_pCanvasRectangle[i]->show();
    44. yPoint += 75;
    45. }
    46.  
    47. }
    48.  
    49. void ImageDraw::selectLine(Q3CanvasItemList item)
    50. {
    51. int i=0;
    52. m_itemLine = item;
    53. for (Q3CanvasItemList::Iterator it= m_itemLine.begin(); it!= m_itemLine.end(); ++it)
    54. {
    55. if ( (*it)->rtti() == 7 )
    56. {
    57. for(i=0;i<lineList.size();i++)
    58. {
    59. if(lineList.at(i) == m_itemLine.at(i))
    60. {
    61.  
    62. QMessageBox::information(this, tr("Mouse Event"),tr("Line %1 Mouse Event Handled ").arg(i));
    63.  
    64. }
    65.  
    66. }
    67. }
    68. else if ( (*it)->rtti() == 5 )
    69. {
    70. QMessageBox::information(this, tr("Mouse Event"),tr("Rectangle Mouse Event Handled "));
    71. }
    72. }
    73. }
    To copy to clipboard, switch view to plain text mode 

    ImageDraw.h: The header file where i have made the dec.
    Qt Code:
    1. #ifndef IMAGEDRAW_H
    2. #define IMAGEDRAW_H
    3.  
    4. #include <QtGui>
    5. #include <Q3Canvas>
    6. #include <qdir.h>
    7. #include <QColor>
    8. #include <Q3CanvasLine>
    9. #include <qevent.h>
    10. #include <QPoint>
    11. #include <Q3CanvasView>
    12. #include <Q3ScrollView>
    13. #include <Q3CanvasItem>
    14. #include <Q3CanvasItemList>
    15. #include <QPen>
    16. #include <QList>
    17.  
    18. class ImageDraw : public Q3CanvasView
    19. {
    20. public:
    21. ImageDraw(Q3Canvas *pCanvas);
    22. ~ImageDraw();
    23. void drawLine();
    24. void drawRectangle();
    25. void selectLine(Q3CanvasItemList item);
    26. //void selectRect(Q3CanvasItemList item);
    27.  
    28. private:
    29. Q3Canvas *m_pCanvas;
    30. Q3CanvasLine **m_pCanvasLine;
    31. Q3CanvasRectangle **m_pCanvasRectangle;
    32. Q3CanvasItemList m_itemLine;
    33. Q3CanvasItemList m_itemRect;
    34. QList<Q3CanvasItem *> lineList;
    35. Q3CanvasItem *m_pLineItem;
    36. //const QPoint m_pointLine,m_pointRect;
    37. int lineNum;
    38. int recNum;
    39. bool lineDraw,rectDraw;
    40. };
    41.  
    42. #endif
    To copy to clipboard, switch view to plain text mode 

    Please tell me what can be the problem...

  17. #16
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Hi munna...

    Thanks a lot for ur suggestions..

    I used the function collidesWith(lineList.at(i)) and it gave me the value of the line i have clicked on...

    Also i was able to create the lineList ..

    One doubt... How can I colour the created line with some other colour..

    Using the pen, we create a line with another coloue but is there any function like stColour for the line which could set the lines colour to the one i need..

    Thanking you,
    Kapil

  18. #17
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out the type of the clicked item..

    Please Explain the following lines.

    Qt Code:
    1. m_pCanvasLine = new Q3CanvasLine*[lineNum];
    2. for(i=0;i<lineNum;i++) {
    3. xyPoint = (i+1)*4;
    4. yyPoint = xyPoint;
    5. m_pCanvasLine[i] = new Q3CanvasLine(m_pCanvas);
    6. m_pCanvasLine[i]->setPoints(xxPoint,xyPoint,yxPoint,yyPoint);
    7. m_pCanvasLine[i]->show();
    8. }
    9. m_pLineItem = new Q3CanvasLine(m_pCanvas);
    10. lineList.append(m_pLineItem);
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. m_pCanvasRectangle = new Q3CanvasRectangle*[recNum];
    2. for(i=0;i<recNum;i++){
    3. m_pCanvasRectangle[i] = new Q3CanvasRectangle(xPoint, yPoint, width, height, _pCanvas);
    4. m_pCanvasRectangle[i]->show();
    5. yPoint += 75;
    6. }
    To copy to clipboard, switch view to plain text mode 

    As i said earlier you dont need to maintain two lists. One list should be more than enough to achieve your task. Please try to find out what exactly you need to achieve and what you are doing in your code.

  19. The following user says thank you to munna for this useful post:

    Kapil (31st March 2006)

  20. #18
    Join Date
    Feb 2006
    Location
    New Delhi,India
    Posts
    226
    Thanks
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find out the type of the clicked item..

    Quote Originally Posted by munna
    Please Explain the following lines.

    Qt Code:
    1. m_pCanvasLine = new Q3CanvasLine*[lineNum];
    2. for(i=0;i<lineNum;i++) {
    3. xyPoint = (i+1)*4;
    4. yyPoint = xyPoint;
    5. m_pCanvasLine[i] = new Q3CanvasLine(m_pCanvas);
    6. m_pCanvasLine[i]->setPoints(xxPoint,xyPoint,yxPoint,yyPoint);
    7. m_pCanvasLine[i]->show();
    8. }
    9. m_pLineItem = new Q3CanvasLine(m_pCanvas);
    10. lineList.append(m_pLineItem);
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. m_pCanvasRectangle = new Q3CanvasRectangle*[recNum];
    2. for(i=0;i<recNum;i++){
    3. m_pCanvasRectangle[i] = new Q3CanvasRectangle(xPoint, yPoint, width, height, _pCanvas);
    4. m_pCanvasRectangle[i]->show();
    5. yPoint += 75;
    6. }
    To copy to clipboard, switch view to plain text mode 

    As i said earlier you dont need to maintain two lists. One list should be more than enough to achieve your task. Please try to find out what exactly you need to achieve and what you are doing in your code.

    Hi.. yaa i understood it.. i was doing this mistake.. i after that removed the array and just maintained the QList as you had told me too.. also i was able to view the line number as i told in the reply just above ur last reply...

    Thanks a lot for ur help..

    Please tell me that is there any setColour sort of function for chaging the line or rectangle colour on click.. or i have to set a pen and recreate the line or rect over the created line...

    Thanks again...

    Kapil

Similar Threads

  1. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  2. problems installing Qt opensource with msvc2008 support
    By odin1985 in forum Installation and Deployment
    Replies: 6
    Last Post: 24th May 2008, 09:06
  3. How to know wich item is clicked QTreeWidget
    By ^NyAw^ in forum Qt Programming
    Replies: 13
    Last Post: 6th November 2007, 23:47
  4. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  5. Getting item from QTableView when clicked on
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 11:58

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.