Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

python - Qt buttons style varying from ui preview to original window

I am trying to design a virtual keyboard using qt creator and PyQt5, the problem is, the buttons looked modern in the qt creator and when i executed the python file, I got a very old styling for the buttons. How can i get the original styling?

The code for buttons used are,

    self.Q = QtWidgets.QPushButton(self.KEYBOARD)
    self.Q.setGeometry(QtCore.QRect(0, 0, 80, 60))
    font = QtGui.QFont()
    font.setPointSize(20)
    self.Q.setFont(font)
    self.Q.setObjectName("Q")

This is the ui rendering in qt creator.

This is the one when the python file is executed.

My working environment is,

RELEASE=18.1
CODENAME=serena
EDITION="Cinnamon 64-bit"
DESCRIPTION="Linux Mint 18.1 Serena"
DESKTOP=Gnome
TOOLKIT=GTK
NEW_FEATURES_URL=http://www.linuxmint.com/rel_serena_cinnamon_whatsnew.php
RELEASE_NOTES_URL=http://www.linuxmint.com/rel_serena_cinnamon.php
USER_GUIDE_URL=help:linuxmint
GRUB_TITLE=Linux Mint 18.1 Cinnamon 64-bit
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Qt handles styles using the QStyle class, usually Qt Designer uses the style fusion, so it shows it that way, and this does not match the one that is configured the OS:

enter image description here

The solution in your case is to configure the application that uses the fusion style with the following code:

app = QApplication(sys.argv)
app.setStyle("fusion")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...