Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

pyqt - Why the central widget(QWidget) doesn't get sized properly when its imported from another file?

when i tried to split the code in another file to be more manageable a undesirable behavior took place. My Qwidget when imported from another file gets small on the screen. if we were to replace the line to set CentralWidget from:

self.setCentralWidget(self.view)

to:

self.setCentralWidget(QTextEdit())

the textedit widget would show with no problems, fully sized. Is that a QgraphicsView Problem or layout problem i am dealing with?

Part relevant of code in main.py

import sys
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from viewport import ViewPort

class dockdemo(QMainWindow):
    def __init__(self, parent=None):
        super(dockdemo, self).__init__(parent)
        self.view:QWidget = ViewPort()

    bar = self.menuBar()
    file = bar.addMenu("File")
    file.addAction("New")
    file.addAction("save")
    file.addAction("quit")

    # buttons layout
    buttons = QWidget()
    vert_layout = QVBoxLayout()
    button = QPushButton("Rotate - ", self)
    button.setGeometry(200, 450, 100, 50)
    button.clicked.connect(self.view.rotate_minus)
    button2 = QPushButton("Rotate + ", self)
    button2.setGeometry(320, 450, 100, 50)
    button2.clicked.connect(self.view.rotate_plus)
    button3 = QPushButton("zoom-in ", self)
    button3.clicked.connect(self.view.zoom_in)
    button4 = QPushButton("zoom-out ", self)
    button4.clicked.connect(self.view.zoom_out)
    button5 = QPushButton("reset zoom ", self)
    button5.clicked.connect(self.view.reset_zoom)
    vert_layout.addWidget(button)
    vert_layout.addWidget(button2)
    vert_layout.addWidget(button3)
    vert_layout.addWidget(button4)
    vert_layout.addWidget(button5)
    buttons.setLayout(vert_layout)

    self.Mydock = QDockWidget("Dockable", self)
    self.Mydock.setWidget(buttons)
    self.Mydock.setFloating(False)

    self.addDockWidget(Qt.LeftDockWidgetArea, self.Mydock)

    self.setCentralWidget(self.view)

    self.setWindowTitle("Dock demo")

my qwidget in viewport.py

import sys
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class ViewPort(QWidget):
    def __init__(self, parent=None):
        super(ViewPort, self).__init__(parent)

    self.scene = QGraphicsScene(self)
    greenBrush = QBrush(Qt.green)
    blueBrush = QBrush(Qt.blue)

    blackPen = QPen(Qt.black)
    blackPen.setWidth(5)

    ellipse = self.scene.addEllipse(10, 10, 11, 11, blackPen, greenBrush)
    ellipse.moveBy(100, 0)
    rect = self.scene.addRect(-100, -100, 200, 200, blackPen, blueBrush)

    self.scene.addText("Codeloop.org", QFont("Sanserif", 15))

    ellipse.setFlag(QGraphicsItem.ItemIsMovable)
    rect.setFlag(QGraphicsItem.ItemIsMovable)

    self.view = QGraphicsView(self.scene, self)
    setattr(self.view, 'scale_of_viewport', 0)

    # filter event so mouse wheel wont scroll. see def event filter
    self.view.viewport().installEventFilter(self)

result: result

expected result (which happens if i implement the Qgraphicsview from main.py):

expected

so because i wanted to implement the viewport on a diferent file (viewport.py) the widget doesn't take over the screen anymore like it was supposed to. i've tried setGeometry(), setminimumsize(), and changeging layouts without any success. i just wanna be able to implement the qwidgets in different files before they get gigantic. I couldn't find the answer online. Thanks for any help.

question from:https://stackoverflow.com/questions/65892368/why-the-central-widgetqwidget-doesnt-get-sized-properly-when-its-imported-fro

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem

As you guessed, is a problem with the layout. Your ViewpPort Widget doesn't have an organized structure. That means the size of the QGraphicsView (inside ViewPort) doesn′t have any relation with the size of the ViewPort widget. And when you try to adjust the size of ViewPort inside dockdemo, the QGraphicsView won't be affected.

Solution

Just add a layout inside your ViewPort widget, and then add the QGraphicsView inside it. Here is your ViewPort class changed (With the changes commented):

    class ViewPort(QWidget):
        def __init__(self, parent=None):
            super(ViewPort, self).__init__(parent)
            ## Create a Layout and set it in the widget, 
            ## in this case I created a Vertical Layout
            central_layout = QVBoxLayout()
            self.setLayout(central_layout)
            ## (end)
            self.scene = QGraphicsScene(self)
            greenBrush = QBrush(Qt.green)
            blueBrush = QBrush(Qt.blue)
            blackPen = QPen(Qt.black)
            blackPen.setWidth(5)
            ellipse = self.scene.addEllipse(10, 10, 11, 11, blackPen, greenBrush)
            ellipse.moveBy(100, 0)
            rect = self.scene.addRect(-100, -100, 200, 200, blackPen, blueBrush)
            self.scene.addText("Codeloop.org", QFont("Sanserif", 15))
            ellipse.setFlag(QGraphicsItem.ItemIsMovable)
            rect.setFlag(QGraphicsItem.ItemIsMovable)
            self.view = QGraphicsView(self.scene, self)
            setattr(self.view, 'scale_of_viewport', 0)
            self.view.viewport().installEventFilter(self)
            ## Add your QGraphicsView inside the layout
            central_layout.addWidget(self.view)
            ## (end)

And here is the result:

enter image description here

(I used PyQt5, but I think this works fine in PySide2, tell if it works)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...