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. Self Define QGraphicsView not constrained by QGridLayout

Self Define QGraphicsView not constrained by QGridLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
pyqt5qgraphicsview
8 Posts 3 Posters 837 Views
  • 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.
  • D Offline
    D Offline
    darrenleeleelee1
    wrote on last edited by darrenleeleelee1
    #1

    I want four of my QGraphicsView to be same size, but when I change one to self define QGraphicsView instance, the layout would be broken.

    from PhotoViewer import PhotoViewer
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
            self.gridLayout.setObjectName("gridLayout")
            self.frame = QtWidgets.QFrame(self.centralwidget)
            self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
            self.frame.setObjectName("frame")
            self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
            self.gridLayout_2.setObjectName("gridLayout_2")
            self.graphicsView_2 = QtWidgets.QGraphicsView(self.frame)
            self.graphicsView_2.setObjectName("graphicsView_2")
            self.gridLayout_2.addWidget(self.graphicsView_2, 0, 1, 1, 1)
            self.graphicsView = QtWidgets.QGraphicsView(self.frame)
            self.graphicsView.setObjectName("graphicsView")
            self.gridLayout_2.addWidget(self.graphicsView, 0, 0, 1, 1)
            self.graphicsView_3 = PhotoViewer(self.frame)
            self.graphicsView_3.setObjectName("graphicsView_3")
            # self.graphicsView_3.adjustSize()
            print(self.graphicsView_3.size())
            self.gridLayout_2.addWidget(self.graphicsView_3, 1, 0, 1, 1)
            self.graphicsView_4 = QtWidgets.QGraphicsView(self.frame)
            self.graphicsView_4.setObjectName("graphicsView_4")
            self.gridLayout_2.addWidget(self.graphicsView_4, 1, 1, 1, 1)
            self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    

    and the self define is a class inherit QGraphicsView and just override mousePressedEvent, mouseReleasedEvent and mouseMoveEvent.

    class PhotoViewer(QtWidgets.QGraphicsView):
        pass
    

    a9c43866-0bfb-4cfc-a0b6-9c239c3235b3-image.png
    this is my outcome, black one is which I used PhotoViewer.
    and if I don't use QGraphicsView instead of PhotoViewer would be like
    5ccc337f-9cda-44fe-a6dd-5885e46bc1ba-image.png
    I am wondering If I override sizeHint(), or adjustSize(), to let my object to fit the layout.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AlexMaly
      wrote on last edited by
      #2

      Layout is not broken, if you mean that size of the is different when you use different widgets in the layouts, it is probably because of different size policies or for example minimumSize set to them. Check for example if minimum size is specified for your PhotoViewer

      1 Reply Last reply
      0
      • D Offline
        D Offline
        darrenleeleelee1
        wrote on last edited by
        #3

        c0bd67b4-dc70-4f7a-bfae-aa43c0cd5d46-image.png
        I print them out, and minimumSize, and sizePolicy are both same.

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

          Hi,

          Why do you use two layouts ?

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

          D 2 Replies Last reply
          0
          • SGaistS SGaist

            Hi,

            Why do you use two layouts ?

            D Offline
            D Offline
            darrenleeleelee1
            wrote on last edited by
            #5

            @SGaist one is for centralWidget, but may not need it.

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Why do you use two layouts ?

              D Offline
              D Offline
              darrenleeleelee1
              wrote on last edited by
              #6

              @SGaist dd23e435-c72b-43f7-a069-22f4533e3c8e-image.png
              3 is the black one.

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

                But why not set your frame as central widget ?

                You are using one widget and one layout for nothing.

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

                D 1 Reply Last reply
                0
                • SGaistS SGaist

                  But why not set your frame as central widget ?

                  You are using one widget and one layout for nothing.

                  D Offline
                  D Offline
                  darrenleeleelee1
                  wrote on last edited by
                  #8

                  @SGaist ok I see, but I still can't get the size after layout(this return QtCore.QSize())

                  self.graphicsView_3.adjustSize()
                  print(self.graphicsView_3.size())
                  

                  full code

                  from PyQt5 import QtCore, QtGui, QtWidgets
                  from PhotoViewer import PhotoViewer
                  
                  class Ui_MainWindow(object):
                      def setupUi(self, MainWindow):
                          MainWindow.setObjectName("MainWindow")
                          MainWindow.resize(800, 600)
                          self.centralwidget = QtWidgets.QWidget(MainWindow)
                          self.centralwidget.setObjectName("centralwidget")
                          self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
                          self.gridLayout.setObjectName("gridLayout")
                          self.graphicsView_2 = QtWidgets.QGraphicsView(self.centralwidget)
                          self.graphicsView_2.setObjectName("graphicsView_2")
                          self.gridLayout.addWidget(self.graphicsView_2, 2, 0, 1, 1)
                          self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
                          self.graphicsView.setObjectName("graphicsView")
                          self.gridLayout.addWidget(self.graphicsView, 1, 0, 1, 1)
                          self.graphicsView_3 = PhotoViewer(self.centralwidget)
                          self.graphicsView_3.adjustSize()
                          print(self.graphicsView_3.size())
                          self.graphicsView_3.setObjectName("graphicsView_3")
                          self.gridLayout.addWidget(self.graphicsView_3, 1, 1, 1, 1)
                          self.graphicsView_4 = QtWidgets.QGraphicsView(self.centralwidget)
                          self.graphicsView_4.setObjectName("graphicsView_4")
                          self.gridLayout.addWidget(self.graphicsView_4, 2, 1, 1, 1)
                          MainWindow.setCentralWidget(self.centralwidget)
                          self.menubar = QtWidgets.QMenuBar(MainWindow)
                          self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
                          self.menubar.setObjectName("menubar")
                          MainWindow.setMenuBar(self.menubar)
                          self.statusbar = QtWidgets.QStatusBar(MainWindow)
                          self.statusbar.setObjectName("statusbar")
                          MainWindow.setStatusBar(self.statusbar)
                  
                          self.retranslateUi(MainWindow)
                          QtCore.QMetaObject.connectSlotsByName(MainWindow)
                  
                      def retranslateUi(self, MainWindow):
                          _translate = QtCore.QCoreApplication.translate
                          MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
                  
                  
                  if __name__ == "__main__":
                      import sys
                      app = QtWidgets.QApplication(sys.argv)
                      MainWindow = QtWidgets.QMainWindow()
                      ui = Ui_MainWindow()
                      ui.setupUi(MainWindow)
                      MainWindow.show()
                      sys.exit(app.exec_())
                  
                  
                  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