Results 1 to 18 of 18

Thread: what item QCanvasItemList is holding..

  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 what item QCanvasItemList is holding..

    Hi..

    Is their any way to find out what item the QCanvasItemList is holding..
    i read th class well but couldn't find any function supporting it..
    with rtti() i can find out what item type it is holding but not exactly which item..
    i want to extract the exact item which it is holding...
    Is it possible????

    Thank you...

    with regards,
    Kapil
    All you have to decide is what to do with the time that is given to you

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    Quote Originally Posted by Kapil
    with rtti() i can find out what item type it is holding but not exactly which item..
    Sure, compare pointers from the list and from the set of items you have stored in the canvas (QCanvas::allItems()).

  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: what item QCanvasItemList is holding..

    Quote Originally Posted by wysota
    Sure, compare pointers from the list and from the set of items you have stored in the canvas (QCanvas::allItems()).

    Are you talking about the collideswith() function of the QCanvasItem to find out the item clicked..
    If yes, then i tried it out but then it is not returning me the item which i have clicked but the first item on the canvas.. dont know why ?????

    Kapil
    All you have to decide is what to do with the time that is given to you

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    I don't quite understand your problem. If you want an item clicked, just call QCanvas::collisions(const QPoint &p) with p set to the coordinates of the click (e->pos() if in mouse press event) and it returns a list of pointers to items. Then just iterate the list and do with the items whatever you want. You can take a look at wwWidgets widget set, it contains a canvasview with dragging capabilities (among other things it contains a handler for clicking items).

  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: what item QCanvasItemList is holding..

    This is the code of what i have done and is exactly what u have asked me to...

    Qt Code:
    1. void ImageDraw::selectLine(QPoint pRelease)
    2. {
    3. int i=0;
    4. QPoint xPoint,yPoint;
    5. Q3CanvasItemList item = m_pCanvas->collisions(pRelease);;
    6. m_itemLine = item;
    7. for (Q3CanvasItemList::Iterator it= m_itemLine.begin(); it!= m_itemLine.end(); ++it)
    8. {
    9. if ( (*it)->rtti() == 7 )
    10. {
    11. for(i=0;i<20;i++)
    12. {
    13. if((i<lineList.size())&&((*it)->collidesWith(lineList.at(i))))
    14. {
    15. QMessageBox::information(this, tr("Mouse Event"),tr("Line %1 Mouse Event Handled ").arg(i));
    16. m_pCanvasLine[i]->setPen(QPen(Qt::red));
    17. xPoint = m_pCanvasLine[i]->startPoint();
    18. yPoint = m_pCanvasLine[i]->endPoint();
    19. m_pCanvasLine[i]->setPoints(xPoint.x(),xPoint.y(),yPoint.x(),yPoint.y());
    20. m_pCanvasLine[i]->show();
    21.  
    22. break;
    23. }
    24. else if((i<lineList2.size())&&((*it)->collidesWith(lineList2.at(i))))
    25. {
    26. QMessageBox::information(this, tr("Mouse Event"),tr("Line %1 Mouse Event Handled ").arg(i));
    27.  
    28. m_pCanvasLine2[i]->setPen(QPen(Qt::red));
    29. xPoint = m_pCanvasLine2[i]->startPoint();
    30. yPoint = m_pCanvasLine2[i]->endPoint();
    31. m_pCanvasLine2[i]->setPoints(xPoint.x(),xPoint.y(),yPoint.x(),yPoint.y());
    32. m_pCanvasLine2[i]->show();
    33.  
    34. break;
    35. }
    36.  
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    Note : sorry as i have pasted the same code in some other thread but the doubt was a bit different... so if u think that this is turning out to be a double thread then you can close it.. sorry again.. the thread is in the Qt Programming section with the name Highlighting the CanvasItem....

    Thank you...

    Kapil
    All you have to decide is what to do with the time that is given to you

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    What for are you using this collidesWith?

    Try something like this:

    Qt Code:
    1. void ImageDraw::selectLine(QPoint pRelease){
    2. Q3CanvasItemList itemlist = m_pCanvas->collisions(pRelease);
    3. for (Q3CanvasItemList::Iterator it= itemlist.begin(); it!= itemlist.end(); ++it){
    4. Q3CanvasItem *item = *it;
    5. if(item->rtti()!=7) continue;
    6. Q3CanvasLine *line = (Q3CanvasLine*)item;
    7. line->setActive(true);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 21st April 2006 at 12:25.

  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: what item QCanvasItemList is holding..

    hi..

    thanks a lot for the code..
    yes is helps me selecting the exact line but then there is a small problem again...

    why is so that after i click on any item it is getting disappeared... the moment i click on the line it goes off...
    i tried setting the line color to some other color other than white but still the line is gone the moment i click on it... why is it so????

    Kapil
    All you have to decide is what to do with the time that is given to you

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    Where do you "set" the colour? AFAIR canvas items don't have a colour associated with them. I think you need to subclass the item and reimplement its painting routine to be able to change the colour of an item.

    For example:

    Qt Code:
    1. void MyCanvasLine::draw ( QPainter & painter ){
    2. if(isActive()) painter.setPen(Qt::red);
    3. QCanvasLine::draw(painter);
    4. }
    To copy to clipboard, switch view to plain text mode 

  9. #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: what item QCanvasItemList is holding..

    hi..

    to set the colour i did something like this and its working
    Qt Code:
    1. Q3CanvasLine *m_pCanline = (Q3CanvasLine*)itemCan;
    2. m_pCanline->setPen(QPen(Qt::red));
    3. xPoint = m_pCanline->startPoint();
    4. yPoint = m_pCanline->endPoint();
    5. m_pCanline->setPoints(xPoint.x(),xPoint.y(),yPoint.x(),yPoint.y());
    6. m_pCanline->show();
    To copy to clipboard, switch view to plain text mode 

    The drawback which i found with this approach was that i have to redraw the line...

    There is a small bug which is appearing while highlighting the lines.. at a point whenever there are more than 2 lines intersecting i.e. when it tells that canvasitemlist size is 3+, it does not color the line on which i have clicked but colours any of the lines surrounding the point...
    The reason what i thought could be the settings of the z-coordinate... it fills the canvasitemlist in the form of a stack and colours the topmost elemnt of the stack whenever they are more than 1...
    the problem is that how do i figure it out that at what index does my item (one i have clicked) lie in the canvasitemlist so that i can colour that particular item...
    I tried to put on what not mathematical formualtions but few seem to work for a particular set of points and not for the general standards...
    How do i get rid of this problem...

    Thank you...

    with regards,
    Kapil
    All you have to decide is what to do with the time that is given to you

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    Maybe something like this would be easier:

    Qt Code:
    1. Q3CanvasLine *m_pCanline = (Q3CanvasLine*)itemCan;
    2. m_pCanline->setPen(QPen(Qt::red));
    3. m_pCanline->invalidate();
    4. m_pCanline->update();
    To copy to clipboard, switch view to plain text mode 

    the problem is that how do i figure it out that at what index does my item (one i have clicked) lie in the canvasitemlist so that i can colour that particular item
    If you click on a canvas, you get a list of all items which collide with that point. So you click on each of the lines returned.
    Last edited by wysota; 24th April 2006 at 12:12.

  11. #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: what item QCanvasItemList is holding..

    Quote Originally Posted by wysota
    click on each of the lines returned.
    Sorry but i could not understand by the statement... you want to say that highlight all the items that collide with the point...

    when i click on the canvas it returns me the point with the of which i extract all the canvas items coliiding with the point...
    now i need to highlight only one particular item of all that collide with the point.. if i highlight all of the items then it doesn't solve the purpose..
    its like user clicks on the line, chooses the color and the line is coloured that ways... this is what i am not able to do.. was still working out with it....????????
    is their any way to sort it out...

    thank you,

    with regards,
    Kapil
    All you have to decide is what to do with the time that is given to you

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    I don't exactly understand your problem... This is how I do it in wwWidgets:

    Qt Code:
    1. void wwCanvasView::contentsMousePressEvent( QMouseEvent * e ) {
    2. if(e->button()!=LeftButton || !canvas())
    3. return QCanvasView::contentsMousePressEvent(e);
    4. QWMatrix wm = inverseWorldMatrix();
    5. QPoint pt = wm.map(e->pos());
    6. QCanvasItemList l = canvas()->collisions(pt);
    7. if(l.empty()) {
    8. emit clicked(0);
    9. return QCanvasView::contentsMousePressEvent(e);
    10. }
    11. if(!m_dragsEnabled) {
    12. emit clicked(l.first());
    13. return QCanvasView::contentsMousePressEvent(e);
    14. }
    15. QCanvasItem *clicked_item = l.first();
    16. m_dragged = clicked_item;
    17. m_dragorig = pt;
    18. emit clicked(clicked_item);
    19. }
    To copy to clipboard, switch view to plain text mode 

    In your case I would use something like this:

    Qt Code:
    1. void someClass::contentsMousePressEvent( QMouseEvent * e ) {
    2. if(e->button()!=LeftButton || !canvas()) // if not left button or no canvas assigned
    3. return QCanvasView::contentsMousePressEvent(e); // ignore the event
    4. QWMatrix wm = inverseWorldMatrix(); // inverse the matrix to allow zooming
    5. QPoint pt = wm.map(e->pos()); // map the point according to the matrix
    6. QCanvasItemList l = canvas()->collisions(pt); // get list of items clicked
    7. if(l.empty()) { // if none clicked
    8. return QCanvasView::contentsMousePressEvent(e); // ignore event
    9. // in your case you could "deselect" all items, if you want
    10. }
    11. QCanvasItem *clicked_item = l.first(); // get the top most clicked item
    12. clicked_item->setPen(Qt::red); // change it to be red
    13. clicked_item->invalidate(); // invalidate it so that it gets redrawn
    14. clicked_item->update(); // update the canvas to reflect changes
    15. }
    To copy to clipboard, switch view to plain text mode 

    In my opinion this is all that should be done to highlight a "clicked" item.

  13. The following user says thank you to wysota for this useful post:

    Kapil (24th April 2006)

  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: what item QCanvasItemList is holding..

    Hi..

    A minor doubt about the use of the invalidate and the update functions of the Q3CanvasPolyganItem..
    i tried using them but the error was thrown that they are the protected members hence cannot be used directly.. so this means that i need to inherit the Q3CanvasPolyganItem class in order to use these functions... I am already inhereting the Q3CanvasView class.. Wont it create any errors
    All you have to decide is what to do with the time that is given to you

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    You have to update the canvas, so from within the view you should call canvas()->update(). You can use Q3Canvas::setAllChanged() instead of invalidate() if setPen doesn't invalidate the item by itself.

  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: what item QCanvasItemList is holding..

    Quote Originally Posted by wysota
    QCanvasItem *clicked_item = l.first(); // get the top most clicked item
    I don't know why it is not colouring the item i clicked but colouring some other item on the canvas...
    I would put the image here.. i hope this would help you to understand what exactly is happening.. i have pointed out the linei have clicked and u can see the line which it has coloured....
    Attached Images Attached Images
    All you have to decide is what to do with the time that is given to you

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    Maybe you're mixing the coordinates somewhere? It looks like your horizontal and vertical coords are inverted -- (y,x) instead of (x,y).

  18. #17
    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: what item QCanvasItemList is holding..

    Quote Originally Posted by wysota
    Maybe you're mixing the coordinates somewhere? It looks like your horizontal and vertical coords are inverted -- (y,x) instead of (x,y).
    Hi..

    its not inverted... i have created them seperately, though it gives the look that ways as the symmetry is maintained... dont even know which part of the code would give you the idea of how to sort it out....

    i am totally confused... would try it out in a different manner by giving it some break.. i hope it sorts out the things...

    Thanks a lot for all the help and sample code you provided.. It really helped a lot...

    take care...

    with regards,
    Kapil
    All you have to decide is what to do with the time that is given to you

  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: what item QCanvasItemList is holding..

    Try with a single line. If it works, add another and try again. Report coordinates of every click through qDebug or QMessageBox, etc.

Similar Threads

  1. ListWidget positions item
    By talex in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2008, 15:16
  2. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:21
  3. Item in a DLL not getting events
    By Benne Gesserit in forum Qt Programming
    Replies: 11
    Last Post: 16th August 2008, 22:30
  4. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  5. Replies: 1
    Last Post: 19th April 2007, 22:23

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.