在 pyqt5 中删除用作背景的图像的边距

问题描述 投票:0回答:1

我正在尝试删除在小米应用程序中用作背景图像的图像的边距(或填充?)。我使用 QLabel 将图像放置为主窗口的centralWidget,但是当初始化应用程序时,它看起来像这样:

enter image description here

代码很简单:

from PyQt5.QtWidgets import (
QApplication, 
QMainWindow, 
QToolBar,
QStatusBar,
QAction,
QLabel
)
from PyQt5.QtCore import QSize, Qt, QSettings, pyqtSignal
from PyQt5.QtGui import QIcon
from sqlalchemy import create_engine
from model.models import Base
from view.client_register import ClientRegisterWindow
import resources_rc
import qtstylish
import sys
import os

root = os.path.dirname(__file__)

ICONS_WIDTH = 32
ICONS_HEIGHT = 32

BG_WIDTH = 1920
BG_HEIGHT = 1080

try:
    from ctypes import windll

    myappid = "isDev.sistemaGestorUrimare.administrativo.version1.0" 
    windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
except ImportError:
    pass

if not os.path.isfile(root + "/model/database.db"):
    engine = create_engine("sqlite:///model/database.db", echo=True)
    Base.metadata.create_all(engine)

settings = QSettings("Urimare", "urimareApp")

if not settings.contains("theme"):
    settings.setValue("theme", "/resources/light")

class MainWindow(QMainWindow):

    theme_changed = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Sistema Gestor Urimare")
        self.setWindowIcon(QIcon(":/logo/logo.png"))
        self.setStatusBar(QStatusBar(parent=self))

        # setThemeName must contains the relative path to the diretory where index.theme is
        QIcon.setThemeName(settings.value("theme"))

        self.light_image_pixmap = QIcon(":background/light_bg").pixmap(QSize(BG_WIDTH, BG_HEIGHT)) 
        self.dark_image_pixmap = QIcon(":background/dark_bg").pixmap(QSize(BG_WIDTH, BG_HEIGHT)) 
        self.background = QLabel(parent=self)
        self.set_image_and_theme(settings.value("theme"))
        self.background.setScaledContents(True)
        self.theme_changed.connect(self.set_image_and_theme)

        // some code here
        
        ### SLOTS ###
        def set_image_and_theme(self, theme):
            if theme == "/resources/light":
                self.background.setPixmap(self.light_image_pixmap)
                self.setStyleSheet(qtstylish.light())
           else:
                self.background.setPixmap(self.dark_image_pixmap)
                self.setStyleSheet(qtstylish.dark())

如您所见,图像加载良好,但我想删除一些合并,我试图遵循此解决方案背景图像,但那里使用的方法是使用样式表来完成此操作。我已经在我的应用程序中使用 qtstylish,并且尝试更改其根代码中的一些功能,但没有成功。有更好的解决方案吗?预先感谢!

python pyqt5 margin
1个回答
0
投票
def set_image_and_theme(self, theme):
if theme == "/resources/light":
    self.background.setPixmap(self.light_image_pixmap)
    additional_style = "QLabel { border: none; padding: 0px; }"
    self.setStyleSheet(qtstylish.light() + additional_style)
else:
    self.background.setPixmap(self.dark_image_pixmap)
    additional_style = "QLabel { border: none; padding: 0px; }"
    self.setStyleSheet(qtstylish.dark() + additional_style)

试试这个

self.central_widget_layout = QVBoxLayout()  # Or whichever layout you're using
self.central_widget_layout.setContentsMargins(0, 0, 0, 0)
self.centralWidget().setLayout(self.central_widget_layout)
self.central_widget_layout.addWidget(self.background)
© www.soinside.com 2019 - 2024. All rights reserved.