Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to reimplement sizeHint() that when call adjustSize() can get the size after layout?
Forum Update on Monday, May 27th 2025

How to reimplement sizeHint() that when call adjustSize() can get the size after layout?

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 559 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
    #1

    First If I just use QtWidgets.QGraphicsView, then call adjustSize() can get the size after layout.

    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 = QtWidgets.QGraphicsView(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_())
    
    

    d9179494-1014-41e0-a2ea-309c6b96385a-image.png

    I have a self define QGraphicsView is PhotoViewer
    but I have not reimplemented sizeHint() or adjustSize() yet, I am wondering how to reimplement that I can achieve when call adjustSize() to get the size after layout?

    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.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 = PhotoViewer(self.frame)
            self.graphicsView_2.setObjectName("graphicsView_2")
            self.gridLayout_2.addWidget(self.graphicsView_2, 0, 1, 1, 1)
            self.graphicsView = PhotoViewer(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.gridLayout_2.addWidget(self.graphicsView_3, 1, 2, 1, 1)
            self.graphicsView_3.adjustSize()
            print(self.graphicsView_3.size())
            self.graphicsView_4 = PhotoViewer(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_())
    
    

    This code is showing that I got QtCore.Size() when I call adjustSize()

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

      Hi,

      Until widgets are shown they do not have any "physical" size.

      What is your end goal ?

      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
      • D Offline
        D Offline
        darrenleeleelee1
        wrote on last edited by
        #3

        I want to have the size after layout, if the widget is QtWidgets.QGraphicsView I can call adjustSize() to get it, but when the widget change to PhotoViewer(created by myself, inherit QtWidgets.QGraphicsView) then I don't know how to achieve the size after layout.

        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