V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
thedevil7
V2EX  ›  Python

PyQt QGraphicsItem 中既要绘制一个图形(例如一个园), 又要在图形内部有文字的显示, 有些什么样的实现方法?

  •  
  •   thedevil7 ·
    thedevil7 · 2012-04-16 17:09:28 +08:00 · 5806 次点击
    这是一个创建于 4398 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想到了一种方法就是向这个 Item 添加子 TextItem.
    10 条回复    1970-01-01 08:00:00 +08:00
    mlzboy
        1
    mlzboy  
       2012-04-16 19:39:45 +08:00
    我追着问一下,qt和pyqt的差异在哪,刚开始学QT还没有入门呢,求求指点
    thedevil7
        2
    thedevil7  
    OP
       2012-04-16 22:02:59 +08:00
    @mlzboy qt 是 C++ 的库. PyQt 是 python 的绑定库. 和 Qt 内容上好像没有差别. 我现在很多资料也是看 PyQt 自带的 assistant 的文档, 虽然是针对 C++ 写的. 但是 Python 也能用.
    reus
        3
    reus  
       2012-04-17 11:06:55 +08:00
    只是显示可以直接在paint里画,如果需要更多交互就加子item
    thedevil7
        4
    thedevil7  
    OP
       2012-04-17 11:36:19 +08:00
    @reus 多谢, 现在我添加了子 item, 然后设置了字体, 这样就能够在图形内部显示字体, 而且又可以控制大小. 现在又出了一个新问题.. 我有动作需要连接槽, 但是槽时 python 的函数, 需要指定参数. 如果直接写 self.connect(action, SIGNAL(signal), function(arg1,arg2,arg3)), 这个函数就会立即被调用.. 正在找有没有不调用的方法.
    thedevil7
        5
    thedevil7  
    OP
       2012-04-17 19:03:27 +08:00
    问题解决咯~~~ 在 QMenu 里面重新写了一个函数.
    reus
        6
    reus  
       2012-04-17 23:21:19 +08:00
    @thedevil7 可以写成 self.connect(action, SIGNAL(signal), lambda: function(arg1, arg2, arg3))
    thedevil7
        7
    thedevil7  
    OP
       2012-04-17 23:32:48 +08:00
    @reus 多谢, 现在的问题变成了, 我有一个 Item 由菜单中的对 Item 代表的数据有改变, 而且会影响其他的 Item, 现在需要对所有 Item 进行更新重新判断位置并且重画.
    我想到的是, Menu.exec() 之后利用传入的 scene 执行所有 Item 的 update() 函数. 但是没有效果.
    Python 新手... PyQT 新手...
    reus
        8
    reus  
       2012-04-17 23:53:20 +08:00
    @thedevil7 https://gist.github.com/2406975 可以看下这个例子里的GraphWidget里的timerEvent方法,就是定时更新所有item的。这是pyqt源码的examples/graphicsview目录里面的
    其实就是把boundingRect和paint写对就行了,这两个方法需要一些比如位置之类的信息,所以就需要另一些方法来更新这些信息。注意如果item的boundingRect改变了的话,需要调用prepareGeometryChange
    thedevil7
        9
    thedevil7  
    OP
       2012-04-18 00:40:17 +08:00
    @reus 谢谢, 真的很感谢你.帮了我好多问题, 呵呵. 晚安, 明早继续奋战 Qt~
    thedevil7
        10
    thedevil7  
    OP
       2012-04-18 11:26:44 +08:00
    @reus 搞定了, 利用了 timerEvent 的方法重新设置了 item 的位置, 然后再用 scene 的 update() 函数对图形进行了重绘(数据有不同状态, 需要不同颜色表示).
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2226 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:20 · PVG 10:20 · LAX 19:20 · JFK 22:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.