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:
expected result (which happens if i implement the Qgraphicsview from main.py):
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