简体   繁体   English

如何在 Pyqt5 的 QMainWindow 中将小部件放在背景图像上?

[英]How to put a widget on a background image in QMainWindow in Pyqt5?

I have a custom LED indicator widget(from github), which looks like this on QMainWindow:我有一个自定义 LED 指示灯小部件(来自 github),它在 QMainWindow 上看起来像这样:

在此处输入图像描述

I have an image which I can put in the backgroud of the QMainWindow like this:我有一张图片,可以像这样放在 QMainWindow 的背景中:

在此处输入图像描述

Now my quesion is, How do I put the LED indicator widget OVER the background image(I want to put them in all the boxes)?现在我的问题是,如何将 LED 指示灯小部件放在背景图像上(我想把它们放在所有的盒子里)? The LED indicator widget is not showing up at all when I put a background image in the program.当我在程序中放置背景图像时,LED 指示灯小部件根本没有显示。

This is the code:这是代码:

import sys
from LedIndicatorWidget import *
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QCheckBox, QWidget
from PyQt5.QtCore import QSize    

class ExampleWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setMinimumSize(QSize(300, 300))    
        self.setWindowTitle("Checkbox")

        oImage = QImage("map.png")
        sImage = oImage.scaled(QSize(800,277))                   
        palette = QPalette()
        palette.setBrush(QPalette.Window, QBrush(sImage))                        
        self.setPalette(palette)

        self.show()

    
    

        self.led = LedIndicator(self)
        self.led.setDisabled(True)  
    
    
        self.led.move(10,20)
        self.led.resize(100,100)

        self.led1 = LedIndicator(self)
        self.led1.setDisabled(True)  
    
    
        self.led1.move(150,20)
        self.led1.resize(100,100)

        self.led2 = LedIndicator(self)
        self.led2.setDisabled(True)  
    
    
        self.led2.move(300,20)
        self.led2.resize(100,100)
    
    
    
        self.timer = QtCore.QTimer()
    
    
        self.timer.timeout.connect(self.onPressButton)
        self.timer.start()
    


    def onPressButton(self):
    
    
        self.led.setChecked(not self.led.isChecked())
        self.timer.stop()
     
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = ExampleWindow()
    mainWin.show()
    sys.exit( app.exec_() )

I've used Qpalette for the background image, if you have a better idea to add the image and make it work, feel free to do so, because I couldn't find one.我使用 Qpalette 作为背景图像,如果您有更好的想法来添加图像并使其工作,请随意这样做,因为我找不到。

To use the LED indicator widget, make a file "LedIndicatorWidget.py" and copy this code:要使用 LED 指示灯小部件,请创建一个文件“LedIndicatorWidget.py”并复制以下代码:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class LedIndicator(QAbstractButton):
    scaledSize = 1000.0

    def __init__(self, parent=None):
        QAbstractButton.__init__(self, parent)

        self.setMinimumSize(24, 24)
        self.setCheckable(True)

        # Green
        self.on_color_1 = QColor(0, 255, 0)
        self.on_color_2 = QColor(0, 192, 0)
        self.off_color_1 = QColor(0, 28, 0)
        self.off_color_2 = QColor(0, 128, 0)

    def resizeEvent(self, QResizeEvent):
        self.update()

    def paintEvent(self, QPaintEvent):
        realSize = min(self.width(), self.height())

        painter = QPainter(self)
        pen = QPen(Qt.black)
        pen.setWidth(1)

        painter.setRenderHint(QPainter.Antialiasing)
        painter.translate(self.width() / 2, self.height() / 2)
        painter.scale(realSize / self.scaledSize, realSize / self.scaledSize)

        gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500))
        gradient.setColorAt(0, QColor(224, 224, 224))
        gradient.setColorAt(1, QColor(28, 28, 28))
        painter.setPen(pen)
        painter.setBrush(QBrush(gradient))
        painter.drawEllipse(QPointF(0, 0), 500, 500)

        gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500))
        gradient.setColorAt(0, QColor(224, 224, 224))
        gradient.setColorAt(1, QColor(28, 28, 28))
        painter.setPen(pen)
        painter.setBrush(QBrush(gradient))
        painter.drawEllipse(QPointF(0, 0), 450, 450)

        painter.setPen(pen)
        if self.isChecked():
            gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500))
            gradient.setColorAt(0, self.on_color_1)
            gradient.setColorAt(1, self.on_color_2)
        else:
            gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500))
            gradient.setColorAt(0, self.off_color_1)
            gradient.setColorAt(1, self.off_color_2)

        painter.setBrush(gradient)
        painter.drawEllipse(QPointF(0, 0), 400, 400)

    @pyqtProperty(QColor)
    def onColor1(self):
        return self.on_color_1

    @onColor1.setter
    def onColor1(self, color):
        self.on_color_1 = color

    @pyqtProperty(QColor)
    def onColor2(self):
        return self.on_color_2

    @onColor2.setter
    def onColor2(self, color):
        self.on_color_2 = color

    @pyqtProperty(QColor)
    def offColor1(self):
        return self.off_color_1

    @offColor1.setter
    def offColor1(self, color):
        self.off_color_1 = color

    @pyqtProperty(QColor)
    def offColor2(self):
        return self.off_color_2

    @offColor2.setter
    def offColor2(self, color):
        self.off_color_2 = color

The issue is not related to the background: the LED widgets are there , the problem is that widgets added to a parent that is already shown (and without using a layout manager) does not make them visible, and they must be explicitly shown by calling show() or setVisible(True) .问题与背景无关:LED小部件在那里,问题是添加到已经显示的父级的小部件(并且使用布局管理器)不会使它们可见,它们必须通过调用显式显示show()setVisible(True)

You can see the difference if you remove the self.show() line after setting the palette (but leaving the mainWin.show() at the end): in that case, the leds become automatically visible.如果在设置调色板后删除self.show()行(但将mainWin.show()留在最后),您可以看到差异:在这种情况下,LED 将自动可见。

The solution is to either show the child widgets explicitly, or call show() / setVisible(True) on the parent after adding them.解决方案是显式显示子小部件,或者在添加它们在父小部件上调用show() / setVisible(True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM