PDA

View Full Version : showing the name of object +mouse



rimie23
24th April 2012, 17:40
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 ?

qlands
25th April 2012, 10:52
Hi,

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



void myQWidgetClass::moveEvent ( QMoveEvent * event )
{
QWidget *child;
child = childAt (event->pos.x(), event->pos.y());
if (child)
{
qDebug() << child.objectName();
}

}


Carlos.

rimie23
25th April 2012, 13:05
hi ,
i t dos not work
it make to me error in this line

child = childAt (e->pos.x(), e->pos.y());
left of '->pos' must point to class/struct/union/generic type

there is another thing

qDebug() << child.objectName();
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
.
.

qlands
26th April 2012, 14:50
This is the code:



void Mainwidget::mouseMoveEvent(QMouseEvent * e)
{
QWidget *child;
child = this->childAt(e->pos().x(),e->pos().y());
if (child)
{
qDebug() << child->objectName();
}
}


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.

rimie23
26th April 2012, 23:44
hi ,
yhats what i see
7635
i have many spheres
i draw this spheres like that

for(i=0,i<sphere.size(),i++)
{
int posx=sphere[i].x
int posy=sphere[i].y
int posz=sphere[i].z
..................
}
and for each sphre[i] there is name

char name=sphere[i].name
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?

Spitfire
27th April 2012, 16:21
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:


class MySphere : public QGraphicsItem
{
public:
MySphere( QGraphicsItem* parent = 0 ) : QGraphicsItem( parent ), myName( "Not named!" )
{
this->setAcceptHoverEvents( true );
}

void setValues( int x, int y, int z )
{
// do something
}

void setName( const QString& name )
{
this->myName = name;
}

protected:
// add painting and all else you need

void hoverEnterEvent( QGraphicsSceneHoverEvent* event )
{
QToolTip::showText( event->screenPos(), this->myName );
}

void hoverLeaveEvent( QGraphicsSceneHoverEvent* event )
{
Q_UNUSED( event );

QToolTip::hideText();
}

private:
QString myName;
};


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

rimie23
27th April 2012, 19:39
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


void OgreWidget::createScene()
{
ogreSceneManager->setAmbientLight(Ogre::ColourValue(1,1,1));

for(i=0,i<sphere.size(),i++)
{
int posx=sphere[i].x
int posy=sphere[i].y
int posz=sphere[i].z
ent_sphere = ogreSceneManager->createEntity( sphere[i].name,Ogre::SceneManager:: PT_SPHERE);


sphereNode= ogreSceneManager->getRootSceneNode()->createChildSceneNode(sphere[i].name, Ogre::Vector3(kk, kkk, kkkk));

sphereNode->setScale(0.1f, 0.1f, 0.1f);
sphereNode->attachObject(sphere_marker);
}

Spitfire
30th April 2012, 13:33
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.

rimie23
30th April 2012, 16:39
thank you
i just find how i do it in ogre thanks a lot