Davy
12th May 2015, 10:56
Good Morning,
I have a QGraphicsScene with two kinds of items : rectangle items and text Items
I use a QInputDialog window to get the user's text and insert it in the text items. However, once the QInputDialog is closed, it opens again whenever I click on the scene. Normally the dialog should open only when I click on the text items but not when I click on rectangle items.
I have this behaviour under Windows but not under Linux.
Here is the code :
#!python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication, QGraphicsItem, QGraphicsScene, QGraphicsView, QPainter
from PyQt4.QtCore import QRectF, QPoint, QPointF, QRect, Qt
class TextItem(QGraphicsItem):
def __init__(self, parent=None):
super(TextItem, self).__init__(parent)
self.text_rect = QRectF(10, 10, 100, 20)
self.name = ""
self.brush = QtGui.QBrush(QtCore.Qt.darkBlue)
def boundingRect(self):
return self.text_rect
def paint(self, painter, option, parent=None):
painter.drawText(self.text_rect, QtCore.Qt.AlignHCenter| QtCore.Qt.AlignTop, self.name);
painter.drawRect(self.text_rect)
def setName(self, new_name):
self.name = new_name
self.update()
def mousePressEvent(self, mouseEvent):
text, ok = QtGui.QInputDialog.getText(QtGui.QInputDialog(), "Name",
"Name:", QtGui.QLineEdit.Normal, self.name)
if ok and text != '':
self.setName(text)
class RectItem(QGraphicsItem):
def __init__(self, x, y, parent=None):
super(RectItem, self).__init__(parent)
self.rect = QRectF(x,y,10,10)
def boundingRect(self):
return self.rect
def paint(self, painter, option, parent=None):
painter.drawRect(self.rect)
def mousePressEvent(self, event):
print "ok"
class Scene(QGraphicsScene):
def __init__(self, parent=None):
super(Scene, self).__init__(parent)
self.addItem(TextItem())
for i in range(10):
item = RectItem(10 + i*10, 50)
self.addItem(item)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
size = 200
scene = Scene()
scene.setSceneRect(QRectF(0, 0, size, size))
view = QGraphicsView(scene)
view.show();
sys.exit(app.exec_())
Thanks in advance for any help.
I have a QGraphicsScene with two kinds of items : rectangle items and text Items
I use a QInputDialog window to get the user's text and insert it in the text items. However, once the QInputDialog is closed, it opens again whenever I click on the scene. Normally the dialog should open only when I click on the text items but not when I click on rectangle items.
I have this behaviour under Windows but not under Linux.
Here is the code :
#!python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication, QGraphicsItem, QGraphicsScene, QGraphicsView, QPainter
from PyQt4.QtCore import QRectF, QPoint, QPointF, QRect, Qt
class TextItem(QGraphicsItem):
def __init__(self, parent=None):
super(TextItem, self).__init__(parent)
self.text_rect = QRectF(10, 10, 100, 20)
self.name = ""
self.brush = QtGui.QBrush(QtCore.Qt.darkBlue)
def boundingRect(self):
return self.text_rect
def paint(self, painter, option, parent=None):
painter.drawText(self.text_rect, QtCore.Qt.AlignHCenter| QtCore.Qt.AlignTop, self.name);
painter.drawRect(self.text_rect)
def setName(self, new_name):
self.name = new_name
self.update()
def mousePressEvent(self, mouseEvent):
text, ok = QtGui.QInputDialog.getText(QtGui.QInputDialog(), "Name",
"Name:", QtGui.QLineEdit.Normal, self.name)
if ok and text != '':
self.setName(text)
class RectItem(QGraphicsItem):
def __init__(self, x, y, parent=None):
super(RectItem, self).__init__(parent)
self.rect = QRectF(x,y,10,10)
def boundingRect(self):
return self.rect
def paint(self, painter, option, parent=None):
painter.drawRect(self.rect)
def mousePressEvent(self, event):
print "ok"
class Scene(QGraphicsScene):
def __init__(self, parent=None):
super(Scene, self).__init__(parent)
self.addItem(TextItem())
for i in range(10):
item = RectItem(10 + i*10, 50)
self.addItem(item)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
size = 200
scene = Scene()
scene.setSceneRect(QRectF(0, 0, size, size))
view = QGraphicsView(scene)
view.show();
sys.exit(app.exec_())
Thanks in advance for any help.