# Import required package libraries from qgis.PyQt.QtWidgets import QLabel # Create a QLabel object label = QLabel('Hello, World!') # Apply CSS to the label to set the font size and color label.setStyleSheet('font-size: 20px; color: red;') # Display the label label.show()
# Import required package libraries from qgis.PyQt.QtWidgets import QMainWindow, QLabel, QVBoxLayout, QWidget # Create a QMainWindow object main_window = QMainWindow() # Create a QWidget object to hold the labels widget = QWidget() # Create two QLabel objects with different text label1 = QLabel('This is label 1.') label2 = QLabel('This is label 2.') # Create a QVBoxLayout to organize the labels vertically layout = QVBoxLayout() layout.addWidget(label1) layout.addWidget(label2) # Set the layout of the widget widget.setLayout(layout) # Set the widget as the central widget of the main window main_window.setCentralWidget(widget) # Apply CSS to the labels to set their background color label1.setStyleSheet('background-color: yellow;') label2.setStyleSheet('background-color: green;') # Show the main window main_window.show()In this example, we create a QMainWindow object and a QWidget object to hold two QLabel objects. We use a QVBoxLayout to organize the labels vertically and set the layout of the widget. Finally, we use the setStyleSheet method to apply CSS to the labels, setting their background color. We then display the main window with the widget as its central widget. Package/library: PyQt.QtWidgets.