从QComboBox中的样式列表中使用QStyleFactory设置样式

 小新 发布于 2023-02-06 10:52

我一直在使用PyQt4实现一个应用程序。

在此应用程序中,我想根据用户的选择来设置样式,并且我希望在不重新启动对话框的情况下设置样式。

这是我的代码,它会影响样式区域:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui

styles = ["Plastique","Cleanlooks","CDE","Motif","GTK+"]

class AppWidget(QWidget):
    def __init__(self,parent=None):
        super(AppWidget,self).__init__(parent)

        global styles # declaring global

        # I've skipped the useless codes

        horizontalLayout = QHBoxLayout()
        self.styleLabel =QLabel("Set Style:")
        self.styleComboBox = QComboBox()
        self.styleComboBox.addItems(styles) # adding the styles list
        horizontalLayout.addWidget(self.styleLabel)
        horizontalLayout.addWidget(self.styleComboBox)

        # skip more code

        self.setLayout(layout)

    def getStyle(self):
        return self.styleComboBox.currentIndex() # get the current index from combobox

        # another way i also implement is :
        # return self.styleComboBox.currentText()
        # after that i remove the global and directly access using this method 
        # which is of no success

if __name__ == "__main__":
    global styles # declaring global
    app = QApplication(sys.argv)
    widgetApp = AppWidget()

    i = widgetApp.getStyle() # assign the index here
    QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(styles[i])) # setting the style

    widgetApp.show()
    app.exec_()
    print i

但是我一直只获得“ Plastique”风格。

1 个回答
  • 您不需要样式的全局列表,因为QStyleFactory.keys已经提供了该列表。

    您需要做的是将这些键加载到组合框中,将组合框索引设置为当前样式,然后将组合框activated信号连接到处理程序,以便可以更改样式。

    这样的事情应该起作用:

    import sys
    from PyQt4 import QtCore, QtGui
    
    class AppWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(AppWidget, self).__init__(parent)
            horizontalLayout = QtGui.QHBoxLayout()
            self.styleLabel = QtGui.QLabel("Set Style:")
            self.styleComboBox = QtGui.QComboBox()
            # add styles from QStyleFactory
            self.styleComboBox.addItems(QtGui.QStyleFactory.keys())
            # find current style
            index = self.styleComboBox.findText(
                        QtGui.qApp.style().objectName(),
                        QtCore.Qt.MatchFixedString)
            # set current style
            self.styleComboBox.setCurrentIndex(index)
            # set style change handler
            self.styleComboBox.activated[str].connect(self.handleStyleChanged)
            horizontalLayout.addWidget(self.styleLabel)
            horizontalLayout.addWidget(self.styleComboBox)
            self.setLayout(horizontalLayout)
    
        # handler for changing style
        def handleStyleChanged(self, style):
            QtGui.qApp.setStyle(style)
    
    if __name__ == "__main__":
    
        app = QtGui.QApplication(sys.argv)
        widgetApp = AppWidget()
        widgetApp.show()
        sys.exit(app.exec_())
    

    2023-02-06 10:54 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有