Results 1 to 1 of 1

Thread: QInputDialog in QGraphicsScene is opened on each mousePressEvent

  1. #1
    Join Date
    May 2015
    Posts
    1
    Qt products
    Platforms
    Unix/X11 Windows

    Default QInputDialog in QGraphicsScene is opened on each mousePressEvent

    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 :
    Qt Code:
    1. #!python
    2. # -*- coding: utf-8 -*-
    3. from PyQt4 import QtCore, QtGui
    4. from PyQt4.QtCore import QRectF, QPoint, QPointF, QRect, Qt
    5.  
    6. class TextItem(QGraphicsItem):
    7.  
    8. def __init__(self, parent=None):
    9. super(TextItem, self).__init__(parent)
    10. self.text_rect = QRectF(10, 10, 100, 20)
    11. self.name = ""
    12. self.brush = QtGui.QBrush(QtCore.Qt.darkBlue)
    13.  
    14. def boundingRect(self):
    15. return self.text_rect
    16.  
    17. def paint(self, painter, option, parent=None):
    18. painter.drawText(self.text_rect, QtCore.Qt.AlignHCenter| QtCore.Qt.AlignTop, self.name);
    19. painter.drawRect(self.text_rect)
    20.  
    21. def setName(self, new_name):
    22. self.name = new_name
    23. self.update()
    24.  
    25.  
    26.  
    27. def mousePressEvent(self, mouseEvent):
    28. text, ok = QtGui.QInputDialog.getText(QtGui.QInputDialog(), "Name",
    29. "Name:", QtGui.QLineEdit.Normal, self.name)
    30. if ok and text != '':
    31. self.setName(text)
    32.  
    33.  
    34. class RectItem(QGraphicsItem):
    35.  
    36. def __init__(self, x, y, parent=None):
    37. super(RectItem, self).__init__(parent)
    38.  
    39. self.rect = QRectF(x,y,10,10)
    40.  
    41. def boundingRect(self):
    42. return self.rect
    43.  
    44. def paint(self, painter, option, parent=None):
    45. painter.drawRect(self.rect)
    46.  
    47.  
    48. def mousePressEvent(self, event):
    49. print "ok"
    50.  
    51. class Scene(QGraphicsScene):
    52.  
    53. def __init__(self, parent=None):
    54. super(Scene, self).__init__(parent)
    55. self.addItem(TextItem())
    56. for i in range(10):
    57. item = RectItem(10 + i*10, 50)
    58. self.addItem(item)
    59.  
    60. if __name__ == '__main__':
    61.  
    62. import sys
    63.  
    64. app = QApplication(sys.argv)
    65. size = 200
    66.  
    67. scene = Scene()
    68. scene.setSceneRect(QRectF(0, 0, size, size))
    69. view = QGraphicsView(scene)
    70. view.show();
    71.  
    72. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for any help.
    Last edited by Davy; 12th May 2015 at 11:22.

Similar Threads

  1. Replies: 3
    Last Post: 13th February 2011, 11:06
  2. Problem in QInputDialog...!
    By hakermania in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2010, 14:58
  3. QInputDialog and OSG
    By Pertur in forum Qt Programming
    Replies: 3
    Last Post: 30th July 2010, 09:34
  4. QGraphicsScene doesn't respond to mousePressEvent
    By Affenbrotbaum in forum Qt Programming
    Replies: 1
    Last Post: 21st January 2010, 19:56
  5. QInputDialog
    By bkv in forum Newbie
    Replies: 2
    Last Post: 6th October 2006, 07:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.