私は答えを得た(かの回避策を言わせて)別の質問を通じて:新しいQStyle
は既存のものからそれを得ている作成する最も簡単な方法を How to make Icon in QMenu larger (PyQt)?
。 PyQtはその目的のためにQProxyStyle
クラスを提供します。以下は、私がHow to make Icon in QMenu larger (PyQt)?の質問でも示した例です。この例では、カスタムQStyle
が作成され(「Fusion」スタイルから派生した)、カスタムスタイルはQMenu
の非常に大きなアイコンを提供します。
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
# Create a custom "QProxyStyle" to enlarge the QMenu icons
#-----------------------------------------------------------
class MyProxyStyle(QProxyStyle):
pass
def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None):
if QStyle_PixelMetric == QStyle.PM_SmallIconSize:
return 40
else:
return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)
# This is the main window class (with a simple QMenu implemented)
# ------------------------------------------------------------------
class TestWindow(QMainWindow):
def __init__(self):
super(TestWindow, self).__init__()
# 1. Set basic geometry and color.
self.setGeometry(100, 100, 400, 400)
self.setWindowTitle('Hello World')
palette = QPalette()
palette.setColor(QPalette.Window, QColor(200, 200, 200))
self.setPalette(palette)
# 2. Create the central frame.
self.centralFrame = QFrame()
self.centralFrame.setFrameShape(QFrame.NoFrame)
self.setCentralWidget(self.centralFrame)
# 3. Create a menu bar.
myMenuBar = self.menuBar()
fileMenu = myMenuBar.addMenu("&File")
testMenuItem = QAction(QIcon("C:\\my\\path\\myFig.png"), "&Test", self)
testMenuItem.setStatusTip("Test for icon size")
testMenuItem.triggered.connect(lambda: print("Menu item has been clicked!"))
fileMenu.addAction(testMenuItem)
# 4. Show the window.
self.show()
# Start your Qt application based on the new style
#---------------------------------------------------
if __name__== '__main__':
app = QApplication(sys.argv)
myStyle = MyProxyStyle('Fusion') # The proxy style should be based on an existing style,
# like 'Windows', 'Motif', 'Plastique', 'Fusion', ...
app.setStyle(myStyle)
myGUI = TestWindow()
sys.exit(app.exec_())
コードスニペットをコピーして* .pyファイルに貼り付けます。もちろん、アイコンのパスをローカルコンピュータの有効なパスに置き換える必要があります。 Qtがアイコン描画を見つけることを100%確実にするための完全なパス( "C:..")を入力してください。
はそれを試してみてください、あなたは、次のウィンドウを取得する必要があります
![enter image description here](https://i.stack.imgur.com/yrNXr.png)
あなたはQtの[高DPIのドキュメント](http://doc.qt.io/qt-5/highdpiを見たことがあります。 html)? –
@TheCompilerありがとうございました。どのように私は実際にこれをPythonで実装するのですか? –
私は自分自身でHighDPI画面を持っていませんが、 'app.setAttribute(Qt.AA_EnableHighDpiScaling、True)'( 'app'が' QApplication'インスタンスです)が既に助けているかもしれません。 –