def __init__(self, servico, parent=None): super().__init__(parent) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setTransformationAnchor(QGraphicsView.AnchorViewCenter) self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate) self.viewport().setAutoFillBackground(False) self.setFrameShape(QFrame.NoFrame) self.setMinimumWidth(150) self._scene = ListaScene(self) self.setScene(self._scene) self._servico = servico self._servico.dadosUsuarioAtualizado.connect(self._atualizarDadosUsuario) self._scene.doubleClickItem.connect(self._emitirUsuarioSelecionado)
class BarraLista(QGraphicsView): usuarioSelecionado = pyqtSignal(Usuario) def __init__(self, servico, parent=None): super().__init__(parent) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setTransformationAnchor(QGraphicsView.AnchorViewCenter) self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate) self.viewport().setAutoFillBackground(False) self.setFrameShape(QFrame.NoFrame) self.setMinimumWidth(150) self._scene = ListaScene(self) self.setScene(self._scene) self._servico = servico self._servico.dadosUsuarioAtualizado.connect(self._atualizarDadosUsuario) self._scene.doubleClickItem.connect(self._emitirUsuarioSelecionado) def _emitirUsuarioSelecionado(self, item): self.usuarioSelecionado.emit(item.getUsuario()) for i in range(self._scene.quantItens()): sceneItem = self._scene.getItem(i) sceneItem.selected = (sceneItem == item) def _atualizarDadosUsuario(self, usuario): us = self.getItemUsuario(usuario) if not us: self.addUsuario(usuario) else: us.setUsuario(usuario) def addUsuario(self, usuario): item = ItemUsuario(usuario) self.sceneRectChanged.connect(item.resize) item.resize(self.sceneRect()) self._scene.addItem(item) def atualizarNumConversasNaoVisualizadas(self, usuario, num): self.getItemUsuario(usuario).setNumConversasNaoVisualizadas(num) def getItemUsuario(self, usuario): for i in range(self._scene.quantItens()): if self._scene.getItem(i).getUsuario().getIP() == usuario.getIP(): return self._scene.getItem(i) return None def resizeEvent(self, resize): size = resize.size() self._scene.setSceneRect(0, 0, size.width(), size.height()) def __getattr__(self, attr): return self._scene.__getattribute__(attr)
#-*- coding: utf-8 -*- from PyQt4.QtGui import QApplication, QGraphicsView from PyQt4.QtCore import QTimer, QRectF from widgets.lista import ListaScene from widgets.itens import ItemMovel app = QApplication([]) w = QGraphicsView() a = ListaScene(w) a.setSceneRect(0,0,300,300) i = ItemMovel() a.addItem(i) i2 = ItemMovel() a.addItem(i2) QTimer.singleShot(2000, lambda: (i.setTamanho(QRectF(0,0,10,10)), i2.setTamanho(QRectF(0,0,40,20)))) QTimer.singleShot(4000, lambda: i.setTamanho(QRectF(0,0,60,50))) q = ItemMovel() QTimer.singleShot(6000, lambda: (a.addItem(q), q.setTamanho(QRectF(0,0,60,60)))) QTimer.singleShot(8000, lambda: (q.setTamanho(QRectF(0,0,30,20)), i.setTamanho(QRectF(0,0,10,50)), i2.setTamanho(QRectF(0,0,30,10)))) a.addItem(ItemMovel()) w.setScene(a) w.show()