Seite 1 von 1

GraphicsItem "blinken" lassen

Verfasst: 17. März 2009 11:20
von otternase
Hallo

folgendes Problem:
ich möchte ein GraphicsItem in einem View "blinken" lassen, wenn es selektiert wurde. Also abwechseldn sichtbar und nicht sichtbar.

Ein möglicher Lösungsweg scheint mir einen QTimer zu starten und dann an das TimerEvent ein Auslesen der selektierten Items zu hängen mit einem setVisible(!isVisible()) für jedes Item.

Gibt es für diese Aufgabe noch eine bessere Lösung, insbesondere auch eine weniger Rechnerlast erzeugende?

Danke
Markus

Verfasst: 17. März 2009 11:30
von franzf
QGraphicsItemAnimation sollte für dich passen. Evtl. ableiten.
Ich hab da mal rumgespielt mit Python. Vielleicht bringts dir ja was ;)

Code: Alles auswählen

# -*- coding: iso-8859-15 -*-
from PyQt4 import Qt
import types
import sys


class Gradient:
  def __init__(self, start=Qt.QColor(Qt.Qt.black), stop=Qt.QColor(Qt.Qt.white), alpha=255):
    self.start = start
    self.stop = stop
    self.alpha = alpha

  def colorAt(self, step):
    if (step < 0.0) | (step > 1.0):
      print "Warning, step = " + str(step) + " out of range, accepted values are 0.0 <= x <= 1.0"
      if step < 0.0:
        return start
      else:
        return stop
    color = Qt.QColor()
    color.setAlpha(self.alpha)
    color.setRedF((self.start.red() + (float(self.stop.red())-float(self.start.red()))*step)/255.0)
    color.setGreenF((self.start.green() + (float(self.stop.green())-float(self.start.green()))*step)/255.0)
    color.setBlueF((self.start.blue() + (float(self.stop.blue())-float(self.start.blue()))*step)/255.0)
    return color


class HighlightAnimation (Qt.QGraphicsItemAnimation):
  def __init__(self, gradient=Gradient(), parent=None):
    Qt.QGraphicsItemAnimation.__init__(self, parent)
    self.gradient = gradient

  def setGradient(self, gradient):
    if gradient:
      self.gradient = gradient

  def beforeAnimationStep(self, step):
    self.item().setBrush(Qt.QBrush(self.gradient.colorAt(step)))



if __name__ == "__main__":
  app = Qt.QApplication(sys.argv)
  
  text = Qt.QGraphicsSimpleTextItem("B")
  text.setFont(Qt.QFont("Sans", 300))
  
  timer = Qt.QTimeLine(2500)
  timer.setFrameRange(0, 100)
  Qt.QObject.connect(timer, Qt.SIGNAL('finished()'), timer, Qt.SLOT('toggleDirection()'))
  Qt.QObject.connect(timer, Qt.SIGNAL('finished()'), timer, Qt.SLOT('start()'))
  
  animation = HighlightAnimation()
  animation.setItem(text)
  animation.setTimeLine(timer)
  animation.setGradient(Gradient(start=Qt.QColor(Qt.Qt.blue), stop=Qt.QColor(Qt.Qt.red)))
  
  for i in range(0, 100):
    animation.setTranslationAt(i/100.0, -text.boundingRect().width()/2.0, -text.boundingRect().height()/2.0)
    animation.setScaleAt(i/100.0, 1+i/100.0, 1+i/100.0)
    animation.setRotationAt(i/100.0, i*3.6)
  
  scene = Qt.QGraphicsScene()
  side = text.boundingRect().width()*2.0
  scene.setSceneRect(Qt.QRectF(-side, -side, side*2.0, side*2.0))
  scene.addItem(text)
  
  view = Qt.QGraphicsView()
  view.setScene(scene)
  view.setViewport(Qt.QGLWidget(Qt.QGLFormat(Qt.QGL.SampleBuffers)))
  view.setRenderHint(Qt.QPainter.Antialiasing)
  view.show()
  
  timer.start()
  
  sys.exit(app.exec_())

Verfasst: 17. März 2009 12:22
von upsala
Jedes ein und ausblenden der Items erzeugt einen Refresh und dieser kostet nun mal Rechenzeit...