Coder Social home page Coder Social logo

QGraphicsPathItem about qt HOT 3 CLOSED

therecipe avatar therecipe commented on September 24, 2024
QGraphicsPathItem

from qt.

Comments (3)

therecipe avatar therecipe commented on September 24, 2024

Hey

It seems like accessing scene.Width() or scene.Height() in line.boundingRect() causes a infinite recursion and crashes the program. (The scene probably checks all added objects to determine its own size)
But you could use the QGraphicsView size (which is different from the QGraphicsScene size).

The scene.RemoveItem function should work, just click inside the scene and the line should disappear.

package main

import (
    "os"

    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/gui"
    "github.com/therecipe/qt/widgets"
)

var (
    canvas *widgets.QWidget
    scene  *widgets.QGraphicsScene
    view   *widgets.QGraphicsView
)

type line struct{ *widgets.QGraphicsPathItem }

func main() {
    widgets.NewQApplication(len(os.Args), os.Args)

    NewCanvas().Show()

    widgets.QApplication_Exec()
}

func NewCanvas() *widgets.QWidget {
    canvas = widgets.NewQWidget(nil, 0)
    scene = widgets.NewQGraphicsScene(nil)
    view = widgets.NewQGraphicsView(nil)

    var l = &line{QGraphicsPathItem: widgets.NewQGraphicsPathItem(nil)}
    l.ConnectBoundingRect(l.boundingRect)
    l.ConnectPaint(l.paint)

    scene.AddItem(l)

    //test scene.RemoveItem
    scene.ConnectMousePressEvent(func(e *widgets.QGraphicsSceneMouseEvent) { scene.RemoveItem(l) })

    view.SetScene(scene)

    var layout = widgets.NewQVBoxLayout()
    layout.AddWidget(view, 0, 0)

    canvas.SetLayout(layout)

    return canvas
}

func (l *line) paint(p *gui.QPainter, o *widgets.QStyleOptionGraphicsItem, w *widgets.QWidget) {
    var color = gui.NewQColor3(0, 0, 0, 255)
    var brush = gui.NewQBrush3(color, 0) // solid = 1, nobrush = 0
    var pen = gui.NewQPen3(color)
    pen.SetWidth(1)

    p.SetRenderHint(1, true) // Antiailiasing
    p.SetPen(pen)
    p.SetBrush(brush)

    path := gui.NewQPainterPath()

    sx, sy, tx, ty := 0.0, 0.0, scene.Width(), scene.Height()/2

    path.MoveTo2(sx, sy)
    path.LineTo2(tx, ty)
    p.DrawPath(path)
}

func (l *line) boundingRect() *core.QRectF {
    /*
        //accessing scene.Width() and scene.Height() here causes infinite recursion
        println("called boundingRect")
        return core.NewQRectF4(0.0, 0.0, scene.Width(), scene.Height()/2)
    */
    return core.NewQRectF4(0.0, 0.0, float64(view.Width()), float64(view.Height())/2)
}

from qt.

5k3105 avatar 5k3105 commented on September 24, 2024

Thanks. Confirmed my error.

Btw, I am having a difficult time keeping the canvas updated. I use these two methods:

c.View.Viewport().Repaint()

and

c.Scene.Update(c.Scene.SceneRect())

Neither seem to be working like I want. When I draw lines and remove them sometimes they stay on the scene until I draw several more lines and they eventually disappear. Sometimes when I erase a box on the canvas a line underneath the box will not redraw itself unless I zoom in and out.

from qt.

5k3105 avatar 5k3105 commented on September 24, 2024

QGraphicsItem.PrepareGeometryChange() AND View.Viewport().Repaint() getting me most of the way there :/

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.