Why is origin of drawing not top left corner ?
-
I am using PyQt5. I want to make a QMainWIndow with a central widget containing a horizontal row of buttons. Beneath the buttons I want a framed panel with a grid background. The lines of the grid are drawing in the frame, but the origin is offset. How can I get the grid to be relative to (0,0) in the coordinates of the frame ?
Below I have given the relevant bits of code. It produces the buttons and frame, but the vertical grid lines are in the lower right of the frame.
class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) vbl = QVBoxLayout() hbl = QHBoxLayout() hbl.addWidget(QPushButton('Start')) hbl.addWidget(QPushButton('End')) vbl.addLayout(hbl) fl = QVBoxLayout() s = Simulation() v = s.display() fl.addWidget(v) frame = QFrame() frame.setFrameStyle(QFrame.Panel) frame.setLayout(fl) vbl.addWidget(frame) w = QWidget() w.setLayout(vbl) self.setCentralWidget(w) self.show() class Simulation: def __init__(self): _width = 20 _height = 20 class SimScene(QGraphicsScene): def __init__(self, sim, parent=None): QGraphicsScene.__init__(self, parent) self._sim = sim def drawBackground(self, painter, rect): grid = 10.0 lines = [] for x in range(self._sim._width): lines.append(QLineF(x * grid, 0, x * grid, grid * self._sim._height)) painter.drawLines(lines);
-
-
I found the answer:-
I am only drawing the background of QGraphicsScene. I still have not put any graphics items in the scene, so the system has no idea what size it should be. I added a call to v.setSceneRect(0,0,200,200) to specify the size of the grid and everything works.
-
@elpidiovaldez5 please don't forget to mark your post as solved. Thanks.