Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. widget doesn't fit inside QGraphicsProxyWidget

widget doesn't fit inside QGraphicsProxyWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • GofferG Offline
    GofferG Offline
    Goffer
    wrote on last edited by
    #1

    Hello community !

    I am trying to create a custom QGraphicsProxtWidget with a predefined size then set its widget. But the widget doesn't match the proxy's size.

    Isn't the widget supposed to inherit from the proxy ? Or I am missing a step ?
    Here is my code so far :

    from PySide import QtGui, QtCore
    
    class GraphicItem(QtGui.QGraphicsItem):
    
        def __init__(self, parent=None):
            super(GraphicItem, self).__init__(parent)
    
            self.setFlag(self.ItemIsMovable)
            self.setFlag(self.ItemIsSelectable)
    
    
        def boundingRect(self):
            rect = QtCore.QRect(0,0,400,400)
            return rect
    
        def shape(self):
            path = QtGui.QPainterPath()
            path.addRect(self.boundingRect())
            return path
    
        def paint(self, painter, option, widget):
            painter.setBrush(QtGui.QColor(255,0,0,255))
            painter.drawRect(self.boundingRect())
    
    
    
    
    
    class ProxyItem(QtGui.QGraphicsProxyWidget):
    
        def __init__(self, parent=None):
            super(ProxyItem, self).__init__(parent)
    
            self.x = 300
            self.y = 300
    
        def boundingRect(self):
            rect = QtCore.QRect(0,0,self.x,self.y)
            return rect
    
        def sizeHint(self, option, index):
            size = QtCore.QSize(self.x,self.y)
            return size
    
        def shape(self):
            path = QtGui.QPainterPath()
            path.addRect(self.boundingRect())
            return path
    
    
    
    
    
    s = QtGui.QGraphicsScene()
    v = QtGui.QGraphicsView(s)
    v.show()
    
    ti = GraphicItem(parent=None)
    s.addItem(ti)
    
    
    pi = ProxyItem(parent=ti)
    pi.setWidget(QtGui.QTextBrowser())
    pi.setPos(5,5)
    

    If somebody has a good example of how to subclass a QGraphicsProxyWidget, I will gladly take it since I can't find one ...

    Thanks !

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why create a subclass of QGraphicsProxyWidget ? From your description you should set a fixed size on your widget and add it to the scene.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • GofferG Offline
        GofferG Offline
        Goffer
        wrote on last edited by
        #3

        I'm not entirely sure that I will need the subclass but I might. And even if I don't, I always like to know how to do it just in case I have to in the future.

        I'm prototyping something and I'm trying different approaches.

        The idea is to have this proxy resized by it's parent. So I wanted my GraphicsItem to resize my proxy thinking that my widget would directly be resized by the proxy.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Subclassing for the sake of subclassing isn't a good idea. The manipulation you plan to do really don't seem to call for any subclassing.

          You can manipulate your QGraphicsProxyWidget geometry directly so you seem to over-engineer things a bit here.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • GofferG Offline
            GofferG Offline
            Goffer
            wrote on last edited by
            #5

            but what if I have to change the events ? or add new things to it. It's not because it's not in the example that I won't have to do it.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Depending on what you want to add, it will likely make more sense to modify the underlying widget. But that's up to you to decide.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • GofferG Offline
                GofferG Offline
                Goffer
                wrote on last edited by
                #7

                So for now I'm using QGraphicsProxyWidget without subclassing and directly setting the geometry of my widget.

                Now I want to move my GraphicsItem by clicking and moving from the widget. So I have to override or ignore them. But the Widget doesn't really have a parent/child relationship with my QGraphicsItem.

                So Can I use the events.ignore() ? Or do I somehow have to get a pointer from my GraphicsItem inside my widget and manually override everything ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then you should take a look at the Pad Navigator example.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved