explicitly setting style sheet in python pyqt4?

2024/10/13 19:22:27

In pyqt standard way for setting style sheet is like this:

MainWindow.setStyleSheet(_fromUtf8("/*\n"
"gridline-color: rgb(85, 170, 255);\n"
"QToolTip\n"
"{\n"
"    border: 1px solid #76797C;\n"
"    background-color: rgb(90, 102, 117);;\n"
"    color: white;\n"
"    padding: 5px;\n"
"    opacity: 200;\n"
"}\n"
"#label_3{\n"
"    background-color:rgb(90, 102, 117);\n"
"    color:white;\n"
"    padding-left:20px;\n"
" \n"
"}\n"
"#label_2{\n"
"    color:white;\n"
"    padding-left:20px;\n"
" \n"
"}\n"

But like we link the stylesheet in html <link rel="stylesheet" href="style.css"> .can't we do the same in pyqt?It helps in organizing the things.

Answer

There are currently only two main ways to set a stylesheet. The first is to use the setStyleSheet method:

widget.setStyleSheet("""QToolTip {border: 1px solid #76797C;background-color: rgb(90, 102, 117);color: white;padding: 5px;opacity: 200;}""")

This will only take a string, so an external resource would need to be explicitly read from a file, or imported from a module.

The second method is to use the -stylesheet command-line argument, which allows an external qss resource to be specified as a path:

python myapp.py -stylesheet style.qss

This opens up the possibility of a tempting hack, since it is easy enough to manipulate the args passed to the QApplication constructor, and explicitly insert a default stylesheet:

import sysargs = list(sys.argv)
args[1:1] = ['-stylesheet', 'style.qss']app = QtGui.QApplication(args)

(Inserting the extra arguments at the beginning of the list ensures that it is still possible for the user to override the default with their own stylesheet).

https://en.xdnf.cn/q/118044.html

Related Q&A

missing required Charfield in django is saved as empty string and do not raise an error

If I try to save incomplete model instance in Django 1.10, I would expect Django to raise an error. It does not seem to be the case.models.py:from django.db import modelsclass Essai(models.Model):ch1 =…

Beautiful soup missing some html table tags

Im trying to extract data from a website using beautiful soup to parse the html. Im currently trying to get the table data from the following webpage :link to webpageI want to get the data from the tab…

403 error Not Authorized to access this resource/api Google Admin SDK in web app even being admin

Im struggling to find the problem since two days without any idea why I get this error now even though the app was fully functional one month before.Among the tasks done by the web app, it makes an Adm…

Kivy - My ScrollView doesnt scroll

Im having problems in my Python application with Kivy library. In particular Im trying to create a scrollable list of elements in a TabbedPanelItem, but I dont know why my list doesnt scroll.Here is my…

How to get an associated model via a custom admin action in Django?

Part 2 of this question asked and answered separately.I have a Report and a ReportTemplate. +----+----------+---------------+-------------+ | id | title | data | template_id | +----+-------…

How can I use descriptors for non-static methods?

I am aware that I can use descriptors to change static property as if it were a normal property. However, when I try using descriptors for a normal class property, I end up changing the object it refer…

psycopg2 not all arguments converted during string formatting

I am trying to use psycopg2 to insert a row into a table from a python list, but having trouble with the string formatting.The table has 4 columns of types (1043-varchar, 1114-timestamp, 1043-varchar, …

inherited function odoo python

i want to inherit function in module hr_holidays that calculate remaining leaves the function is :hr_holiday.py:def _get_remaining_days(self, cr, uid, ids, name, args, context=None):cr.execute("&…

ValueError in pipeline - featureHasher not working?

I think Im having issues getting my vectorizer working within a gridsearch pipeline:data as panda df x_train:bathrooms bedrooms price building_id manager_id 10 1.5 3 3000 53a5b119b…

pandas dataframe: meaning of .index

I am trying to understand the meaning of the output of the following code:import pandas as pdindex = [index1,index2,index3] columns = [col1,col2,col3] df = pd.DataFrame([[1,2,3],[1,2,3],[1,2,3]], index…