Qt buttons style varying from ui preview to origin

2019-08-18 12:02发布

问题:

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

回答1:

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:

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")