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

拖曳火力

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

屏幕截图2

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

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

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”风格。

ekhumoro

您不需要样式的全局列表,因为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_())

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在GTK + 3中使用CSS样式化列表行

来自分类Dev

使用内联样式设置单个列表项的样式?

来自分类Dev

使用内联样式设置单个列表项的样式?

来自分类Dev

在Wordpress中设置个人列表样式

来自分类Dev

使用CSS设置编号列表的样式

来自分类Dev

QCombobox透明项目列表样式表

来自分类Dev

QCombobox透明项目列表样式表

来自分类Dev

使用CSS在JavaFX中设置样式按钮

来自分类Dev

使用Sass在React中设置SVG样式

来自分类Dev

使用Java设置样式

来自分类Dev

如何在QListWidgetItem和QCombobox项中设置样式(富文本)?(PyQt / PySide)

来自分类Dev

如何在OL3中使用SLD设置图层样式

来自分类Dev

在Yii CGridview中设置按钮列样式,以在<td>中使用<div>标签包装内容

来自分类Dev

在后台代码中使用DataItem在嵌套转发器中设置类或样式

来自分类Dev

在Excel 2007中使用单元格样式进行条件格式设置?

来自分类Dev

更改QComboBox的悬停样式

来自分类Dev

如何在javaFX FXML中使用CSS设置SVG的样式

来自分类Dev

在QMenu中使用样式表设置图标?

来自分类Dev

在libgdx中使用getStyle设置Label背景样式

来自分类Dev

在libgdx中使用getStyle设置Label背景样式

来自分类Dev

在QMenu中使用样式表设置图标?

来自分类Dev

不能从列表中单独设置<a>元素的样式

来自分类Dev

设置无序列表<ul>中特定<li>元素的样式

来自分类Dev

如何在jQuery Mobile中设置“列表项>链接”的样式

来自分类Dev

如何为列中的多个列表设置样式?

来自分类Dev

如何在jquery mobile中设置“列表项>链接”的样式

来自分类Dev

更好的在Bootstrap中使用Glyphicons进行列表样式的方法

来自分类Dev

如何使用for循环浏览元素列表并动态设置元素样式?

来自分类Dev

分别设置QComboBox菜单的项目的样式PyQt5

Related 相关文章

  1. 1

    在GTK + 3中使用CSS样式化列表行

  2. 2

    使用内联样式设置单个列表项的样式?

  3. 3

    使用内联样式设置单个列表项的样式?

  4. 4

    在Wordpress中设置个人列表样式

  5. 5

    使用CSS设置编号列表的样式

  6. 6

    QCombobox透明项目列表样式表

  7. 7

    QCombobox透明项目列表样式表

  8. 8

    使用CSS在JavaFX中设置样式按钮

  9. 9

    使用Sass在React中设置SVG样式

  10. 10

    使用Java设置样式

  11. 11

    如何在QListWidgetItem和QCombobox项中设置样式(富文本)?(PyQt / PySide)

  12. 12

    如何在OL3中使用SLD设置图层样式

  13. 13

    在Yii CGridview中设置按钮列样式,以在<td>中使用<div>标签包装内容

  14. 14

    在后台代码中使用DataItem在嵌套转发器中设置类或样式

  15. 15

    在Excel 2007中使用单元格样式进行条件格式设置?

  16. 16

    更改QComboBox的悬停样式

  17. 17

    如何在javaFX FXML中使用CSS设置SVG的样式

  18. 18

    在QMenu中使用样式表设置图标?

  19. 19

    在libgdx中使用getStyle设置Label背景样式

  20. 20

    在libgdx中使用getStyle设置Label背景样式

  21. 21

    在QMenu中使用样式表设置图标?

  22. 22

    不能从列表中单独设置<a>元素的样式

  23. 23

    设置无序列表<ul>中特定<li>元素的样式

  24. 24

    如何在jQuery Mobile中设置“列表项>链接”的样式

  25. 25

    如何为列中的多个列表设置样式?

  26. 26

    如何在jquery mobile中设置“列表项>链接”的样式

  27. 27

    更好的在Bootstrap中使用Glyphicons进行列表样式的方法

  28. 28

    如何使用for循环浏览元素列表并动态设置元素样式?

  29. 29

    分别设置QComboBox菜单的项目的样式PyQt5

热门标签

归档