【问题标题】:Setting style in using QStyleFactory from a list of styles in a QComboBox从 QComboBox 中的样式列表中使用 QStyleFactory 设置样式
【发布时间】:2014-01-24 09:37:11
【问题描述】:

我一直在使用 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”风格。

【问题讨论】:

    标签: python pyqt4 qcombobox qstyle


    【解决方案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_())
    

    【讨论】:

    • @LucyJaa。我已经为我添加的行添加了 cmets。对于global stylesgetStyle,您不需要示例中的任何代码。我还在某些地方添加了QtGui(但这不会有任何区别)。除此之外,我的代码与您的示例相同。
    猜你喜欢
    • 2012-01-15
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2015-07-20
    • 2013-10-14
    • 2014-02-10
    相关资源
    最近更新 更多