Coder Social home page Coder Social logo

QGrapicsItems Interactions about qt HOT 7 CLOSED

therecipe avatar therecipe commented on September 24, 2024
QGrapicsItems Interactions

from qt.

Comments (7)

therecipe avatar therecipe commented on September 24, 2024

Which functions do you need exactly?

from qt.

5k3105 avatar 5k3105 commented on September 24, 2024

Hi!,

From your post in (#12) :

if you would want to be able to override virtual functions in QGraphicsLineItem, you would need to create a new QGraphicsLineItem with widgets.NewQGraphicsLineItem and then add it to the scene with scene.AddItem.

I'm trying to use the mouse hover:

hoverEnterEvent(QGraphicsSceneHoverEvent *)
hoverLeaveEvent(QGraphicsSceneHoverEvent *)
hoverMoveEvent(QGraphicsSceneHoverEvent *)

It's possible I'm not going about it right. So for each line or rectangle or circle I plot on the scene I want it to trigger it's hover events so I know which item it's over.

I tried this last week and it's didn't seem to work (I used AddRect, setAcceptHoverEvents and ConnectHoverEnterEvent I believe) then I remembered your post about that.

Thanks

from qt.

therecipe avatar therecipe commented on September 24, 2024

Hey

Yes, you need to create the items directly with NewQ* instead of add*.
(Because you want to override virtual functions and not signals)

You might want to also call the default virtual implementation (which always ends with *Default)

This works for me:

    var text = widgets.NewQGraphicsSimpleTextItem2("Hello 世界", nil)
    text.SetFont(gui.NewQFont2("Meiryo", 20, 2, false))
    text.SetAcceptHoverEvents(true)

    text.ConnectHoverEnterEvent(func(e *widgets.QGraphicsSceneHoverEvent) {
        text.SetText("entered")
        text.HoverEnterEventDefault(e)
    })

    text.ConnectHoverMoveEvent(func(e *widgets.QGraphicsSceneHoverEvent) {
        text.SetText(fmt.Sprintf("moved %v %v", e.ScenePos().X(), e.ScenePos().Y()))
        text.HoverMoveEventDefault(e)
    })

    text.ConnectHoverLeaveEvent(func(e *widgets.QGraphicsSceneHoverEvent) {
        text.SetText("leaved")
        text.HoverLeaveEventDefault(e)
    })

    scene.AddItem(text)

from qt.

5k3105 avatar 5k3105 commented on September 24, 2024

I wonder what I was looking at. They are all implemented :/

Sorry! Thanks!

from qt.

5k3105 avatar 5k3105 commented on September 24, 2024

With text.SetPos2(50, 50) this modification gives "moved 0 0" :

    text.ConnectHoverMoveEvent(func(e *widgets.QGraphicsSceneHoverEvent) {
        var x, y = e.Pos().X(), e.Pos().Y()
        var i = scene.ItemAt3(x, y, nil)
        x, y = i.X(), i.Y()
        text.SetText(fmt.Sprintf("moved %v %v", x, y))
        text.HoverMoveEventDefault(e)
    })

Same with this:

    text.ConnectHoverMoveEvent(func(e *widgets.QGraphicsSceneHoverEvent) {
        var x, y = e.Pos().X(), e.Pos().Y()
        var i = scene.ItemAt3(x, y, nil)
        var p = core.NewQPointF()
        p = i.ScenePos()
        x, y = p.X(), p.Y()
        text.SetText(fmt.Sprintf("moved %v %v", x, y))
        text.HoverMoveEventDefault(e)
    })

I would have thought x,y would be the coords of where the item is in the scene - 50 50.

My application draws rectangles on a grid. I am trying to retrieve the xy of the hovered item. Instead it always returns 0,0.

from qt.

5k3105 avatar 5k3105 commented on September 24, 2024

It's strange that didn't work - but I found a better solution by subclassing QGaphicsItem. Thanks.

from qt.

therecipe avatar therecipe commented on September 24, 2024

Hey

This gives me moved 50 50

    text.SetPos2(50, 50)

    text.ConnectHoverMoveEvent(func(e *widgets.QGraphicsSceneHoverEvent) {
        var x, y = e.ScenePos().X(), e.ScenePos().Y()
        var i = scene.ItemAt3(x, y, nil)
        x, y = i.X(), i.Y()

        text.SetText(fmt.Sprintf("moved %v %v", x, y))
        text.HoverMoveEventDefault(e)
    })

    text.ConnectHoverMoveEvent(func(e *widgets.QGraphicsSceneHoverEvent) {
        var x, y = e.ScenePos().X(), e.ScenePos().Y()
        var i = scene.ItemAt3(x, y, nil)
        var p = i.ScenePos()
        x, y = p.X(), p.Y()

        text.SetText(fmt.Sprintf("moved %v %v", x, y))
        text.HoverMoveEventDefault(e)
    })

I just needed to replace e.Pos() with e.ScenePos().

There are 3 different current positions you can get from the event e.Pos(), e.ScenePos() and e.ScreenPos() and you probably want to use e.ScenePos() most of the time.

Maybe take a look here: http://doc.qt.io/qt-5/qgraphicsscenehoverevent.html#pos

from qt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.