Results 1 to 9 of 9

Thread: showing the name of object +mouse

  1. #1
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default showing the name of object +mouse

    Hi
    i am beginer in Qt programming

    i want to whe i put my mose on an object in the screen it show me his name
    for example : i have two shepe in my scene an each object has name
    object1.name
    object2.name
    or has index(num)
    object1.index
    object2.index

    i want when i put my mouse on the object1 i see his name (in label or in onother thing) , is it possible to do tat in qt ? and how i do that ?

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: showing the name of object +mouse

    Hi,

    Lets say that your application is based on qwidget. So you can re-implement moveEvent:

    Qt Code:
    1. void myQWidgetClass::moveEvent ( QMoveEvent * event )
    2. {
    3. QWidget *child;
    4. child = childAt (event->pos.x(), event->pos.y());
    5. if (child)
    6. {
    7. qDebug() << child.objectName();
    8. }
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    Carlos.

  3. #3
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: showing the name of object +mouse

    hi ,
    i t dos not work
    it make to me error in this line
    Qt Code:
    1. child = childAt (e->pos.x(), e->pos.y());
    To copy to clipboard, switch view to plain text mode 
    left of '->pos' must point to class/struct/union/generic type
    there is another thing
    Qt Code:
    1. qDebug() << child.objectName();
    To copy to clipboard, switch view to plain text mode 
    how it now the name of object (there are many object and my object name are like this object[i].name ?
    for exemple
    object[1].name="A1"
    object[2].name="C"
    .
    .
    .
    .
    i can use index too
    object[1].num=2
    object[2].num=5
    .
    .

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: showing the name of object +mouse

    This is the code:

    Qt Code:
    1. void Mainwidget::mouseMoveEvent(QMouseEvent * e)
    2. {
    3. QWidget *child;
    4. child = this->childAt(e->pos().x(),e->pos().y());
    5. if (child)
    6. {
    7. qDebug() << child->objectName();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    You need to activate setMouseTracking(true) to all objects for it to work.

    how it now the name of object (there are many object and my object name are like this object[i].name ?
    for exemple
    object[1].name="A1"
    object[2].name="C"
    .
    .
    .
    .
    i can use index too
    object[1].num=2
    object[2].num=5
    .
    .
    Its is not clear on what you want to do.

  5. #5
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: showing the name of object +mouse

    hi ,
    yhats what i see
    aff.jpg
    i have many spheres
    i draw this spheres like that
    Qt Code:
    1. for(i=0,i<sphere.size(),i++)
    2. {
    3. int posx=sphere[i].x
    4. int posy=sphere[i].y
    5. int posz=sphere[i].z
    6. ..................
    7. }
    To copy to clipboard, switch view to plain text mode 
    and for each sphre[i] there is name
    Qt Code:
    1. char name=sphere[i].name
    To copy to clipboard, switch view to plain text mode 
    i want when i put (don't clic) on sphere i can see her name
    (each red sphere has name deferent to the others)

    you understand me now?
    Last edited by rimie23; 26th April 2012 at 22:49.

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: showing the name of object +mouse

    If your sphere is a class deriving from graphics item (and it should be), and it lives in a scene (from what you said, so I assume it's QGraphicsScene) then in the constructor of the sphere set setAcceptHoverEvents( true ), and reimplement hoverEnterEvent and hoverLeaveEvent.

    Something like that:
    Qt Code:
    1. class MySphere : public QGraphicsItem
    2. {
    3. public:
    4. MySphere( QGraphicsItem* parent = 0 ) : QGraphicsItem( parent ), myName( "Not named!" )
    5. {
    6. this->setAcceptHoverEvents( true );
    7. }
    8.  
    9. void setValues( int x, int y, int z )
    10. {
    11. // do something
    12. }
    13.  
    14. void setName( const QString& name )
    15. {
    16. this->myName = name;
    17. }
    18.  
    19. protected:
    20. // add painting and all else you need
    21.  
    22. void hoverEnterEvent( QGraphicsSceneHoverEvent* event )
    23. {
    24. QToolTip::showText( event->screenPos(), this->myName );
    25. }
    26.  
    27. void hoverLeaveEvent( QGraphicsSceneHoverEvent* event )
    28. {
    29. Q_UNUSED( event );
    30.  
    31. QToolTip::hideText();
    32. }
    33.  
    34. private:
    35. QString myName;
    36. };
    To copy to clipboard, switch view to plain text mode 

    Edit:
    btw - dust on my screen is bigger than spheres on this black image

  7. #7
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: showing the name of object +mouse

    If your sphere is a class deriving from graphics item (and it should be), and it lives in a scene (from what you said, so I assume it's QGraphicsScene) then in the constructor of the sphere set setAcceptHoverEvents( true ), and reimplement hoverEnterEvent and hoverLeaveEvent.
    but it's not
    i draw my spheres in the window of qt but i using ogre
    and i store my spheres in vector to draw it i use
    Qt Code:
    1. void OgreWidget::createScene()
    2. {
    3. ogreSceneManager->setAmbientLight(Ogre::ColourValue(1,1,1));
    4.  
    5. for(i=0,i<sphere.size(),i++)
    6. {
    7. int posx=sphere[i].x
    8. int posy=sphere[i].y
    9. int posz=sphere[i].z
    10. ent_sphere = ogreSceneManager->createEntity( sphere[i].name,Ogre::SceneManager:: PT_SPHERE);
    11.  
    12.  
    13. sphereNode= ogreSceneManager->getRootSceneNode()->createChildSceneNode(sphere[i].name, Ogre::Vector3(kk, kkk, kkkk));
    14.  
    15. sphereNode->setScale(0.1f, 0.1f, 0.1f);
    16. sphereNode->attachObject(sphere_marker);
    17. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: showing the name of object +mouse

    Then use Ogre means of finding objects in the scene and retreiving their name.

    Alternative would be to test all elements in the vector on every mouse move, but that is espensive and impractical.

    Besides, if you're using Ogre to display the spheres, why not to use Ogre to display the name as well?
    I'm pretty sure it can be done entirely inside Ogre.

  9. #9
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: showing the name of object +mouse

    thank you
    i just find how i do it in ogre thanks a lot

Similar Threads

  1. Ogre+qt mouse event (add object with mouse problem)
    By rimie23 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2012, 10:49
  2. mouse tracking showing float numbers
    By fatecasino in forum Qwt
    Replies: 17
    Last Post: 2nd February 2011, 15:40
  3. Child object not showing
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2009, 14:46
  4. showing menu mouseclick event with opengl object
    By validator in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2008, 23:25
  5. Replies: 4
    Last Post: 10th May 2006, 22:37

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.