Reputation: 21
The task is to write a robot emulator. I have three classes in my code: ControlWidget, BoardWidget and Emulator (the main widget that should combine Control and Board in one window). I'm going to draw some pictures on the BoardWidget with the use of QPainter.
class ControlWidget(QFrame):
def __init__(self):
super().__init__()
self._vbox_main = QVBoxLayout()
self.initUI()
def initUI(self):
# ... adding some buttons
self.setLayout(self._vbox_main)
self.setGeometry(50, 50, 600, 600)
self.setWindowTitle('Robot Controller')
class BoardWidget(QWidget):
def __init__(self):
super().__init__()
self._robot_pixmap = QPixmap("robo.png")
self.initUI()
def initUI(self):
self.setStyleSheet("QWidget { background: #123456 }")
self.setFixedSize(300, 300)
self.setWindowTitle("Robot Emulator")
Both of them appear nicely if shown in different windows:
class Emulator(QWidget):
def __init__(self):
super().__init__()
self._control = ControlWidget()
self._board = BoardWidget()
self._board.show()
self._control.show()
But the magic comes here. I want my Emulator show both the board and control:
class Emulator(QWidget):
def __init__(self):
super().__init__()
self._control = ControlWidget()
self._board = BoardWidget()
self.initUI()
self.show()
def initUI(self):
layout = QBoxLayout(QBoxLayout.RightToLeft, self)
layout.addWidget(self._control)
layout.addStretch(1)
layout.addWidget(self._board)
self.setLayout(layout)
self.setWindowTitle('Robot Emulator')
self.setWindowIcon(QIcon("./assets/robo.png"))
# self._board.update()
I've killed three hours trying to fix it. I've tried to present my board as a QPixmap over the QLabel inside the QBoxLayout. I tried to replace QBoxLayout with the QHBoxLayout. Nothing makes any difference.
Upvotes: 2
Views: 1357
Reputation: 1310
As @ekhumoro stated in the comments, it is necessary to add the QPixmap
to a QLabel
and then set it on the BoardWidget
layout manager with setLayout()
function.
One solution could be the next reimplementation of BoardWidget
class:
class BoardWidget(QWidget):
def __init__(self):
super().__init__()
self._robot_pixmap = QPixmap("robo.png")
self.label = QLabel()
self.label.setPixmap(self._robot_pixmap)
self._vbox_board = QVBoxLayout()
self.initUI()
def initUI(self):
self._vbox_board.addWidget(self.label)
self.setLayout(self._vbox_board)
self.setStyleSheet("QWidget { background: #123456 }")
self.setFixedSize(300, 300)
self.setWindowTitle("Robot Emulator")
The result is shown here:
Upvotes: 1