Вставленное изображение QPixmap
меняет расстояние между строками в форме и не масштабируется.
Исправить расстояние между строк можно с помощью метода setSizePolicy
и Qsize.Policy
но масштабирование изображения не работает:
self.img_label = QLabel()
img_pixmap = QPixmap("config/img/internet.png")
img_pixmap.scaled(50, 50, Qt.KeepAspectRatio)
self.img_label.setPixmap(img_pixmap)
self.img_label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
также пробовал использовать методы img_pixmap.scaledToWidth(50)
и img_pixmap.scaledToHeight(50)
- безуспешно, метод QLabel().setScaledContents(True)
меняет геометрию изображения растягивая ее по горизонтали, оригинальное изображение имеет размер 200х200 px. Подскажите в чем проблема?
main.py
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
class MainAppInterface(QMainWindow):
MAIN_APP_HEIGHT = 400
MAIN_APP_WIDTH = 700
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.setMinimumHeight(self.MAIN_APP_HEIGHT)
self.setMinimumWidth(self.MAIN_APP_WIDTH)
self.setWindowTitle("Test Forms")
group_box = QGroupBox("Test Forms")
detail_box = DetailBox()
v_spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
g_layout = QGridLayout()
g_layout.addWidget(QLabel("BLA BLA BLA"), 1, 1)
g_layout.addWidget(detail_box, 2, 1)
g_layout.addItem(v_spacer, 3, 1)
group_box.setLayout(g_layout)
h_layout = QHBoxLayout(self.centralWidget)
h_layout.addWidget(group_box)
class DetailBox(QGroupBox):
def __init__(self, detail_dict: dict = None, parent=None):
super(DetailBox, self).__init__(parent)
self.setTitle("Detail")
self.detail_dict = detail_dict
self.name_line = QLineEdit()
self.name_line.setReadOnly(True)
self.description_line = QLineEdit()
self.description_line.setReadOnly(True)
self.img_label = QLabel()
img_pixmap = QPixmap("config/img/internet.png")
img_pixmap.scaled(50, 50, Qt.KeepAspectRatio)
self.img_label.setPixmap(img_pixmap)
self.img_label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.model_line = QLineEdit()
self.model_line.setReadOnly(True)
self.ip_address_line = QLineEdit()
self.ip_address_line.setReadOnly(True)
self.login_datetime_line = QLineEdit()
self.login_datetime_line.setReadOnly(True)
self.login_duration_line = QLineEdit()
self.login_datetime_line.setReadOnly(True)
self.logout_datetime_line = QLineEdit()
self.logout_datetime_line.setReadOnly(True)
self.p_name_line = QLineEdit()
self.p_name_line.setReadOnly(True)
self.p_description_line = QLineEdit()
self.p_description_line.setReadOnly(True)
self.user_id_line = QLineEdit()
self.user_id_line.setReadOnly(True)
self.user_display_name_line = QLineEdit()
self.user_display_name_line.setReadOnly(True)
g_layout = QGridLayout(self)
g_layout.addWidget(QLabel("Name"), 1, 1, 1, 1)
g_layout.addWidget(self.name_line, 1, 2, 1, 1)
g_layout.addWidget(QLabel("Description"), 2, 1, 1, 1)
g_layout.addWidget(self.description_line, 2, 2, 1, 1)
g_layout.addWidget(QLabel("Login Date Time"), 3, 1, 1, 1)
g_layout.addWidget(self.login_datetime_line, 3, 2, 1, 1)
g_layout.addWidget(QLabel("Login Duration"), 4, 1, 1, 1)
g_layout.addWidget(self.login_duration_line, 4, 2, 1, 1)
g_layout.addWidget(QLabel("Logout Date Time"), 5, 1, 1, 1)
g_layout.addWidget(self.logout_datetime_line, 5, 2, 1, 1)
g_layout.addWidget(self.img_label, 1, 3, 5, 1, alignment=Qt.AlignCenter)
g_layout.addWidget(QLabel("Profile Name / Description"), 6, 1, 1, 1)
g_layout.addWidget(self.p_name_line, 6, 2, 1, 1)
g_layout.addWidget(self.p_description_line, 6, 3, 1, 1)
g_layout.addWidget(QLabel("UserId / DisplayName"), 7, 1, 1, 1)
g_layout.addWidget(self.user_id_line, 7, 2, 1, 1)
g_layout.addWidget(self.user_display_name_line, 7, 3, 1, 1)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("Fusion")
ex = MainAppInterface()
ex.show()
sys.exit(app.exec_())