def __init__(self, parent=None): super(StyleSheetEditor, self).__init__(parent) self.ui = Ui_StyleSheetEditor() self.ui.setupUi(self) regExp = QtCore.QRegExp(r'.(.*)\+?Style') defaultStyle = QtGui.QApplication.style().metaObject().className() if regExp.exactMatch(defaultStyle): defaultStyle = regExp.cap(1) self.ui.styleCombo.addItems(QtGui.QStyleFactory.keys()) self.ui.styleCombo.setCurrentIndex( self.ui.styleCombo.findText(defaultStyle, QtCore.Qt.MatchContains)) self.ui.styleSheetCombo.setCurrentIndex( self.ui.styleSheetCombo.findText('Coffee')) self.loadStyleSheet('Coffee')
def setupUi(self, StyleSheetEditor): self.ui = Ui_StyleSheetEditor() StyleSheetEditor.setObjectName(_fromUtf8("StyleSheetEditor")) StyleSheetEditor.resize(445, 289) self.gridlayout = QtGui.QGridLayout(StyleSheetEditor) self.gridlayout.setMargin(9) self.gridlayout.setSpacing(6) self.gridlayout.setObjectName(_fromUtf8("gridlayout")) self.hboxlayout = QtGui.QHBoxLayout() self.hboxlayout.setSpacing(6) self.hboxlayout.setMargin(0) self.hboxlayout.setObjectName(_fromUtf8("hboxlayout")) spacerItem = QtGui.QSpacerItem(321, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.hboxlayout.addItem(spacerItem) self.saveButton = QtGui.QPushButton(StyleSheetEditor) self.saveButton.setEnabled(True) self.saveButton.setObjectName(_fromUtf8("saveButton")) self.hboxlayout.addWidget(self.saveButton) self.applyButton = QtGui.QPushButton(StyleSheetEditor) self.applyButton.setEnabled(True) self.applyButton.setObjectName(_fromUtf8("applyButton")) self.hboxlayout.addWidget(self.applyButton) self.gridlayout.addLayout(self.hboxlayout, 2, 0, 1, 5) spacerItem1 = QtGui.QSpacerItem(32, 20, QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Minimum) self.gridlayout.addItem(spacerItem1, 0, 0, 1, 1) spacerItem2 = QtGui.QSpacerItem(32, 20, QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Minimum) self.gridlayout.addItem(spacerItem2, 0, 4, 1, 1) self.styleTextEdit = QtGui.QTextEdit(StyleSheetEditor) self.styleTextEdit.setObjectName(_fromUtf8("styleTextEdit")) self.gridlayout.addWidget(self.styleTextEdit, 1, 0, 1, 5) spacerItem3 = QtGui.QSpacerItem(10, 16, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum) self.gridlayout.addItem(spacerItem3, 0, 2, 1, 1) self.label_8 = QtGui.QLabel(StyleSheetEditor) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.label_8.sizePolicy().hasHeightForWidth()) self.label_8.setSizePolicy(sizePolicy) self.label_8.setObjectName(_fromUtf8("label_8")) self.gridlayout.addWidget(self.label_8, 0, 1, 1, 1) self.retranslateUi(StyleSheetEditor) QtCore.QMetaObject.connectSlotsByName(StyleSheetEditor)
def __init__(self, parent=None): super(StyleSheetEditor, self).__init__(parent) self.ui = Ui_StyleSheetEditor() self.ui.setupUi(self) regExp = QtCore.QRegExp(r".(.*)\+?Style") defaultStyle = QtGui.QApplication.style().metaObject().className() if regExp.exactMatch(defaultStyle): defaultStyle = regExp.cap(1) self.ui.styleCombo.addItems(QtGui.QStyleFactory.keys()) self.ui.styleCombo.setCurrentIndex(self.ui.styleCombo.findText(defaultStyle, QtCore.Qt.MatchContains)) self.ui.styleSheetCombo.setCurrentIndex(self.ui.styleSheetCombo.findText("Coffee")) self.loadStyleSheet("Coffee")
class StyleSheetEditor(QtGui.QDialog): def __init__(self, parent=None): super(StyleSheetEditor, self).__init__(parent) self.ui = Ui_StyleSheetEditor() self.ui.setupUi(self) regExp = QtCore.QRegExp(r'.(.*)\+?Style') defaultStyle = QtGui.QApplication.style().metaObject().className() if regExp.exactMatch(defaultStyle): defaultStyle = regExp.cap(1) self.ui.styleCombo.addItems(QtGui.QStyleFactory.keys()) self.ui.styleCombo.setCurrentIndex( self.ui.styleCombo.findText(defaultStyle, QtCore.Qt.MatchContains)) self.ui.styleSheetCombo.setCurrentIndex( self.ui.styleSheetCombo.findText('Coffee')) self.loadStyleSheet('Coffee') @QtCore.pyqtSlot(str) def on_styleCombo_activated(self, styleName): QtGui.qApp.setStyle(styleName) self.ui.applyButton.setEnabled(False) @QtCore.pyqtSlot(str) def on_styleSheetCombo_activated(self, sheetName): self.loadStyleSheet(sheetName) def on_styleTextEdit_textChanged(self): self.ui.applyButton.setEnabled(True) def on_applyButton_clicked(self): QtGui.qApp.setStyleSheet(self.ui.styleTextEdit.toPlainText()) self.ui.applyButton.setEnabled(False) def on_saveButton_clicked(self): fileName = QtGui.QFileDialog.getSaveFileName(self) if fileName: self.saveStyleSheet(fileName) def loadStyleSheet(self, sheetName): file = QtCore.QFile(':/qss/%s.qss' % sheetName.lower()) file.open(QtCore.QFile.ReadOnly) styleSheet = file.readAll() try: # Python v2. styleSheet = unicode(styleSheet, encoding='utf8') except NameError: # Python v3. styleSheet = str(styleSheet, encoding='utf8') self.ui.styleTextEdit.setPlainText(styleSheet) QtGui.qApp.setStyleSheet(styleSheet) self.ui.applyButton.setEnabled(False) def saveStyleSheet(self, fileName): styleSheet = self.ui.styleTextEdit.toPlainText() file = QtCore.QFile(fileName) if file.open(QtCore.QIODevice.WriteOnly): QtCore.QTextStream(file) << styleSheet else: QtGui.QMessageBox.information(self, "Unable to open file", file.errorString())
class StyleSheetEditor(QDialog): def __init__(self, parent=None): super(StyleSheetEditor, self).__init__(parent) self.ui = Ui_StyleSheetEditor() self.ui.setupUi(self) regExp = QRegExp(r'.(.*)\+?Style') defaultStyle = QApplication.style().metaObject().className() if regExp.exactMatch(defaultStyle): defaultStyle = regExp.cap(1) self.ui.styleCombo.addItems(QStyleFactory.keys()) self.ui.styleCombo.setCurrentIndex( self.ui.styleCombo.findText(defaultStyle, Qt.MatchContains)) self.ui.styleSheetCombo.setCurrentIndex( self.ui.styleSheetCombo.findText('Coffee')) self.loadStyleSheet('Coffee') def on_styleCombo_activated(self, styleName): QApplication.setStyle(styleName) self.ui.applyButton.setEnabled(False) def on_styleSheetCombo_activated(self, sheetName): self.loadStyleSheet(sheetName) def on_styleTextEdit_textChanged(self): self.ui.applyButton.setEnabled(True) def on_applyButton_clicked(self): QApplication.instance().setStyleSheet( self.ui.styleTextEdit.toPlainText()) self.ui.applyButton.setEnabled(False) def on_saveButton_clicked(self): fileName, _ = QFileDialog.getSaveFileName(self) if fileName: self.saveStyleSheet(fileName) def loadStyleSheet(self, sheetName): file = QFile(':/qss/%s.qss' % sheetName.lower()) file.open(QFile.ReadOnly) styleSheet = file.readAll() try: # Python v2. styleSheet = unicode(styleSheet, encoding='utf8') except NameError: # Python v3. styleSheet = str(styleSheet, encoding='utf8') self.ui.styleTextEdit.setPlainText(styleSheet) QApplication.instance().setStyleSheet(styleSheet) self.ui.applyButton.setEnabled(False) def saveStyleSheet(self, fileName): styleSheet = self.ui.styleTextEdit.toPlainText() file = QFile(fileName) if file.open(QFile.WriteOnly): QTextStream(file) << styleSheet else: QMessageBox.information(self, "Unable to open file", file.errorString())
QtCore.QMetaObject.connectSlotsByName(StyleSheetEditor) def retranslateUi(self, StyleSheetEditor): StyleSheetEditor.setWindowTitle(_translate("StyleSheetEditor", "StyleSheet Paster", None)) self.saveButton.setText(_translate("StyleSheetEditor", "&Save", None)) self.applyButton.setText(_translate("StyleSheetEditor", "&Apply", None)) self.label_8.setText(_translate("StyleSheetEditor", "By Zadow", None)) self.applyButton.clicked.connect(self.apply) self.saveButton.clicked.connect(self.save) def apply(self): QtGui.qApp.setStyleSheet(ui.styleTextEdit.toPlainText()) def save(self): fileName = QtGui.QFileDialog.getSaveFileName() if fileName: self.saveStyleSheet(fileName) if __name__ == "__main__": import sys app = QtGui.QApplication.instance() if not app: app = QtGui.QApplication([]) StyleSheetEditor = QtGui.QWidget() ui = Ui_StyleSheetEditor() ui.setupUi(StyleSheetEditor) StyleSheetEditor.show() app.exec_()
def retranslateUi(self, StyleSheetEditor): StyleSheetEditor.setWindowTitle( _translate("StyleSheetEditor", "StyleSheet Paster", None)) self.saveButton.setText(_translate("StyleSheetEditor", "&Save", None)) self.applyButton.setText(_translate("StyleSheetEditor", "&Apply", None)) self.label_8.setText(_translate("StyleSheetEditor", "By Zadow", None)) self.applyButton.clicked.connect(self.apply) self.saveButton.clicked.connect(self.save) def apply(self): QtGui.qApp.setStyleSheet(ui.styleTextEdit.toPlainText()) def save(self): fileName = QtGui.QFileDialog.getSaveFileName() if fileName: self.saveStyleSheet(fileName) if __name__ == "__main__": import sys app = QtGui.QApplication.instance() if not app: app = QtGui.QApplication([]) StyleSheetEditor = QtGui.QWidget() ui = Ui_StyleSheetEditor() ui.setupUi(StyleSheetEditor) StyleSheetEditor.show() app.exec_()