Dunkles Systemlayout - Widgets nicht sichtbar

Python und das Qt-Toolkit, erstellen von GUIs mittels des Qt-Designers.
Antworten
Nobuddy
User
Beiträge: 997
Registriert: Montag 30. Januar 2012, 16:38

Hallo zusammen,
nach längerem, möchte ich wieder mich mit pyqt auseinandersetzen.
Ich verwende Kubuntu 20.04 und als Theme 'Breeze Dunkel'.
Durch dies, habe ich das Problem, dass die qt-Widgets nicht erkennbar sind.
Wie ich beim Googeln gelesen habe, unterstützt qt nicht dunkle Themen und man muss über stylesheet, das Layout anpassen.
Dazu habe ich schon ein paar codes ausprobiert, aber es verändert sich so gut wie nichts.

Ich stelle jetzt hier mal ein Beispiel rein und hoffe, Ihr könnt mir anhand dieses Beispiels eine Lösung aufzeigen.

Code: Alles auswählen

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPalette, QColor
from PyQt5.QtWidgets import (QMainWindow, QApplication, QVBoxLayout,
	QAction)


class tooldemo(QMainWindow):
	def __init__(self, parent = None):
		super(tooldemo, self).__init__(parent)
		layout = QVBoxLayout()
		tb = self.addToolBar("File")

		new = QAction(QIcon("new.bmp"),"new",self)
		tb.addAction(new)

		open = QAction(QIcon("open.bmp"),"open",self)
		tb.addAction(open)
		save = QAction(QIcon("save.bmp"),"save",self)
		tb.addAction(save)
		tb.actionTriggered[QAction].connect(self.toolbtnpressed)
		self.setLayout(layout)
		self.setWindowTitle("toolbar demo")

	def toolbtnpressed(self, a):
		print("pressed tool button is", a.text())

def main():
	app = QApplication(sys.argv)
	ex = tooldemo()
	ex.show()
	sys.exit(app.exec_())

if __name__ == '__main__':
	main()
Nobuddy
User
Beiträge: 997
Registriert: Montag 30. Januar 2012, 16:38

Hallo, bin beim Googeln auf dieses Beispiel gestoßen, was optisch erstmal gut erkennbar ist.
Hier werde ich erstmal den Code studieren, dann wird mir wahrscheinlich das Verständnis dafür kommen. :wink:

Code: Alles auswählen

from PyQt5 import QtWidgets, QtCore, QtGui

class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)

        mainMenu = self.menuBar()
        mainMenu.setNativeMenuBar(False)
        self.fileMenu = mainMenu.addMenu('Menu1')

        action1     = QtWidgets.QWidgetAction(self)
        self.label1 = QtWidgets.QLabel("action1")
        action1.setDefaultWidget(self.label1);
        action1.setText('action1')

        action2      = QtWidgets.QWidgetAction(self)
        self.label2  = QtWidgets.QLabel("action2")
        action2.setDefaultWidget(self.label2);
        action2.setText('action2')

        action3      = QtWidgets.QWidgetAction(self)
        self.label3  = QtWidgets.QLabel("action3")
        action3.setDefaultWidget(self.label3);
        action3.setText('action3')

        self.fileMenu.addAction(action1)
        self.fileMenu.addAction(action2)
        self.fileMenu.addAction(action3)

        self.fileMenu.triggered.connect(self.print_stuff)        # +++

    def print_stuff(self, q):
        print('whatever->', q.text() )
        self.label1.setStyleSheet("""
            QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
            QLabel:hover { background-color: #654321;}
            """)
        self.label2.setStyleSheet("""
            QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
            QLabel:hover { background-color: #654321;}
            """)
        self.label3.setStyleSheet("""
            QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
            QLabel:hover { background-color: #654321;}
            """)

        if q.text() == 'action1':
            self.label1.setStyleSheet("""
                QLabel { background-color : red; padding: 10px 12px 10px 12px;}
                QLabel:hover { background-color: #C10000;}
                """)
        elif q.text() == 'action2':
            self.label2.setStyleSheet("""
                QLabel { background-color : red; padding: 10px 12px 10px 12px;}
                QLabel:hover { background-color: #C10000;}
                """)
        elif q.text() == 'action3':
            self.label3.setStyleSheet("""
                QLabel { background-color : red; padding: 10px 12px 10px 12px;}
                QLabel:hover { background-color: #C10000;}
                """)


qss = """
QMenuBar {
    background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                      stop:0 lightgray, stop:1 darkgray);
}
QMenuBar::item {
    spacing: 3px;
    padding: 2px 10px;
    background-color: rgb(210,105,30);
    color: rgb(255,255,255);
    border-radius: 5px;
}
QMenuBar::item:selected {
    background-color: rgb(244,164,96);
}
QMenuBar::item:pressed {
    background: rgb(128,0,0);
}

QLabel {
    background-color: #ABABAB;
    color: rgb(255,255,255);
    font: 12px;
    padding: 10px 12px 10px 12px;
}
QLabel:hover {
    background-color: #654321;
}
"""

def run():
    app = QtWidgets.QApplication([])
    app.setStyleSheet(qss)                                   # <---
    application = Window()
    application.show()
    app.exec_()

run()
Antworten