Issue
I am creating a PyQt5 QMainWindow window, which has a image set as the centralLayout. I want to overlay the QMainWindow with a QWidget to show some QLabels inside a QVBoxLayout. But I always get this console warning "QLayout: Attempting to add QLayout '' to QWidget '' which already has a layout".
Here is my code
from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QFont, QPixmap
class GameIdle(QMainWindow):
def __init__(self):
super().__init__()
self.screen_width = QApplication.desktop().width()
self.screen_height = QApplication.desktop().height()
self.font = QFont("Ubuntu")
self.font.setWordSpacing(2)
self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)
self.master_background = QLabel(self)
self.setCentralWidget(self.master_background)
self.font.setWordSpacing(2)
self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)
self.setStyleSheet("background-color:#191F26;")
self.move(0, 0)
self.setFixedSize(self.screen_width, self.screen_height)
self.showFullScreen()
self.master_identify_device_container = QWidget()
self.master_identify_device_container.setParent(self)
self.identify_device_audio_player = QMediaPlayer(self)
self.frontend()
self.identify_device()
def frontend(self):
self.master_background.setPixmap(QPixmap("picture.jpg").scaled(self.screen_width, self.screen_height))
def identify_device(self):
self.master_identify_device_container.setStyleSheet("background-color:black;")
self.master_identify_device_container.resize(self.screen_width, self.screen_height)
self.font.setPointSize(40)
self.device_name = QLabel(self.master_identify_device_container)
self.device_name.setFont(self.font)
self.device_name.setText(f"DEVICE NAME : Test Device")
self.device_name.setStyleSheet("color:white; font-weight:bold;")
self.ip_address = QLabel(self.master_identify_device_container)
self.ip_address.setFont(self.font)
self.ip_address.setText(f"IP ADDRESS : 127.0.0.1")
self.ip_address.setStyleSheet("color:white; font-weight:bold;")
self.device_key = QLabel(self.master_identify_device_container)
self.device_key.setFont(self.font)
self.device_key.setText(f"DEVICE KEY : 123456678")
self.device_key.setStyleSheet("color:white; font-weight:bold;")
self.api_status = QLabel(self.master_identify_device_container)
self.api_status.setFont(self.font)
self.api_status.setText(f"API STATUS : Up")
self.api_status.setStyleSheet("color:white; font-weight:bold;")
self.last_api_response_time = QLabel(self.master_identify_device_container)
self.last_api_response_time.setFont(self.font)
self.last_api_response_time.setText("LAST API RESPONSE : 5 seconds ago")
self.last_api_response_time.setStyleSheet("color:white; font-weight:bold;")
self.master_identify_device_container_layout = QVBoxLayout(self.master_identify_device_container)
self.master_identify_device_container_layout.setSpacing(0)
self.master_identify_device_container_layout.setContentsMargins(self.screen_width / 10,
self.screen_height / 15,
self.screen_width / 10,
self.screen_height / 15)
self.master_identify_device_container_layout.addWidget(self.device_name, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.ip_address, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.device_key, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.api_status, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.last_api_response_time, alignment=Qt.AlignLeft)
self.master_identify_device_container.show()
Thanks in advance
Solution
You need to set the layout for the master_background
widget:
self.master_identify_device_container_layout = QVBoxLayout(self.master_background)
from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QLabel, QApplication
from PyQt5.QtGui import QFont, QPixmap
from PyQt5.Qt import *
class GameIdle(QMainWindow):
def __init__(self):
super().__init__()
self.screen_width = QApplication.desktop().width()
self.screen_height = QApplication.desktop().height()
self.font = QFont("Ubuntu")
self.font.setWordSpacing(2)
self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)
self.master_background = QLabel(self)
self.setCentralWidget(self.master_background)
self.font.setWordSpacing(2)
self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)
self.setStyleSheet("background-color:#191F26;")
self.move(0, 0)
self.setFixedSize(self.screen_width, self.screen_height)
self.showFullScreen()
self.master_identify_device_container = QWidget(self)
# self.master_identify_device_container.setParent(self)
self.identify_device_audio_player = QMediaPlayer(self)
self.frontend()
self.identify_device()
def frontend(self):
self.master_background.setPixmap(QPixmap("opencv_color.jpg").scaled(self.screen_width, self.screen_height))
def identify_device(self):
self.master_identify_device_container.setStyleSheet("background-color:black;")
self.master_identify_device_container.resize(self.screen_width, self.screen_height)
self.font.setPointSize(40)
self.device_name = QLabel(self.master_identify_device_container)
self.device_name.setFont(self.font)
self.device_name.setText(f"DEVICE NAME : Test Device")
# +++ ----> vvvvvvvvvvvvvvvvvvvvvvvv
self.device_name.setStyleSheet("color:white; font-weight:bold; background: transparent;")
self.ip_address = QLabel(self.master_identify_device_container)
self.ip_address.setFont(self.font)
self.ip_address.setText(f"IP ADDRESS : 127.0.0.1")
self.ip_address.setStyleSheet("color:white; font-weight:bold; background: transparent;")
self.device_key = QLabel(self.master_identify_device_container)
self.device_key.setFont(self.font)
self.device_key.setText(f"DEVICE KEY : 123456678")
self.device_key.setStyleSheet("color:white; font-weight:bold; background: transparent;")
self.api_status = QLabel(self.master_identify_device_container)
self.api_status.setFont(self.font)
self.api_status.setText(f"API STATUS : Up")
self.api_status.setStyleSheet("color:white; font-weight:bold; background: transparent;")
self.last_api_response_time = QLabel(self.master_identify_device_container)
self.last_api_response_time.setFont(self.font)
self.last_api_response_time.setText("LAST API RESPONSE : 5 seconds ago")
self.last_api_response_time.setStyleSheet("color:white; font-weight:bold; background: transparent;")
# self.master_identify_device_container_layout = QVBoxLayout(self.master_identify_device_container)
self.master_identify_device_container_layout = QVBoxLayout(self.master_background)
self.master_identify_device_container_layout.setSpacing(0)
self.master_identify_device_container_layout.setContentsMargins(self.screen_width / 10,
self.screen_height / 15,
self.screen_width / 10,
self.screen_height / 15)
self.master_identify_device_container_layout.addWidget(self.device_name, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.ip_address, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.device_key, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.api_status, alignment=Qt.AlignLeft)
self.master_identify_device_container_layout.addWidget(self.last_api_response_time, alignment=Qt.AlignLeft)
# self.master_identify_device_container.show()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = GameIdle()
w.show()
sys.exit(app.exec_())
Answered By - S. Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.