首页 > 其他分享 >pyqt系列【一】个人常用工具代码

pyqt系列【一】个人常用工具代码

时间:2022-12-20 11:36:15浏览次数:48  
标签:obj name 代码 pyqt str 常用工具 self def NAME

pyqt系列开篇。
在使用pyqt写上位机软件时,经常会有很多重复的通用的代码,在这里整理记录。

字体库

在不同的平台使用各平台自带的效果比较好的字体,并调整字号。

class FontTools:
    def __init__(self):
        super(FontTools, self).__init__()

    @staticmethod
    def get_font():
        font = QFont()
        font.setPointSize(14)
        if platform.system() == "Windows":
            font.setFamily("NSimSun")
        elif platform.system() == "Linux":
            font.setFamily("NSimSun")
        else:
            font.setFamily("PingFang SC")
        return font

使用方法

在QApplication初始化以后,设置字体app.setFont(FontTools.get_font())

QtTools

一些控件的封装,避免重复劳动。

点击查看代码
class QtTools:
    QGridLayout_NAME = "QGridLayout_"
    QHBoxLayout_NAME = "QHBoxLayout_"
    QVBoxLayout_NAME = "QVBoxLayout_"
    QWidget_NAME = "QWidget_"
    QTabWidget_NAME = "QTabWidget_"
    QGroupBox_NAME = "QGroupBox_"
    QLabel_NAME = "QLabel_"
    QComboBox_NAME = "QComboBox_"
    QPushButton_NAME = "QPushButton_"
    QRadioButton_NAME = "QRadioButton_"
    QCheckBox_NAME = "QCheckBox_"
    QSplitter_NAME = "QSplitter_"
    QScrollArea_NAME = "QScrollArea_"
    QTextEdit_NAME = "QTextEdit_"
    QPlainTextEdit_NAME = "QPlainTextEdit_"
    QLineEdit_NAME = "QLineEdit_"
    QTextBrowser_NAME = "QTextBrowser_"
    QDialogButtonBox_NAME = "QDialogButtonBox_"

    @staticmethod
    def updateAllWidgetsFont():
        font = FontTools.get_font()
        for widget in QApplication.allWidgets():
            widget.setFont(font)
            widget.update()

    def newQGridLayout(self, name: str, alignment=Qt.AlignmentFlag.AlignTop) -> QGridLayout:
        obj = QGridLayout()
        obj.setObjectName(self.QGridLayout_NAME + name)
        obj.setAlignment(alignment)
        return obj

    def newQHBoxLayout(self, name: str, alignment=Qt.AlignmentFlag.AlignTop) -> QHBoxLayout:
        obj = QHBoxLayout()
        obj.setObjectName(self.QHBoxLayout_NAME + name)
        obj.setAlignment(alignment)
        return obj

    def newQVBoxLayout(self, name: str, alignment=Qt.AlignmentFlag.AlignTop) -> QVBoxLayout:
        obj = QVBoxLayout()
        obj.setObjectName(self.QVBoxLayout_NAME + name)
        obj.setAlignment(alignment)
        return obj

    def newQWidget(self, name: str) -> QWidget:
        obj = QWidget()
        obj.setObjectName(self.QWidget_NAME + name)
        return obj

    def newQTabWidget(self, name: str) -> QTabWidget:
        obj = QTabWidget()
        obj.setStyleSheet("QTabWidget {{ font-size: {}pt; }} ".format(obj.font().pointSize()+4))
        obj.setObjectName(self.QTabWidget_NAME + name)
        return obj

    def newQGroupBox(self, name: str, title: str, alignment=Qt.AlignmentFlag.AlignCenter) -> QGroupBox:
        obj = QGroupBox()
        obj.setStyleSheet("QGroupBox {{ font-size: {}pt; }} ".format(obj.font().pointSize()+4))
        obj.setObjectName(self.QGroupBox_NAME + name)
        obj.setAlignment(alignment)
        obj.setTitle(title)
        return obj

    def newQLabel(self, name: str, text: str, alignment=Qt.AlignmentFlag.AlignLeft) -> QLabel:
        obj = QLabel()
        obj.setObjectName(self.QLabel_NAME + name)
        obj.setAlignment(alignment)
        obj.setText(text)
        return obj

    def newQComboBox(self, name: str) -> QComboBox:
        obj = QComboBox()
        obj.setObjectName(self.QComboBox_NAME + name)
        return obj

    def newQPushButton(self, name: str, text: str) -> QPushButton:
        obj = QPushButton()
        obj.setObjectName(self.QPushButton_NAME + name)
        obj.setText(text)
        return obj

    def newQRadioButton(self, name: str, text: str) -> QRadioButton:
        obj = QRadioButton()
        obj.setObjectName(self.QRadioButton_NAME + name)
        obj.setText(text)
        return obj

    def newQCheckBox(self, name: str, text: str) -> QCheckBox:
        obj = QCheckBox()
        obj.setObjectName(self.QCheckBox_NAME + name)
        obj.setText(text)
        return obj

    def newQSplitter(self, name: str, orientation=Qt.Orientation.Horizontal) -> QSplitter:
        obj = QSplitter()
        obj.setObjectName(self.QSplitter_NAME + name)
        obj.setOrientation(orientation)
        return obj

    def newQScrollArea(self, name: str) -> QScrollArea:
        obj = QScrollArea()
        obj.setObjectName(self.QScrollArea_NAME + name)
        return obj

    def newQTextEdit(self, name: str) -> QTextEdit:
        obj = QTextEdit()
        obj.setObjectName(self.QTextEdit_NAME + name)
        return obj

    def newQPlainTextEdit(self, name: str) -> QPlainTextEdit:
        obj = QPlainTextEdit()
        obj.setObjectName(self.QPlainTextEdit_NAME + name)
        return obj

    def newQLineEdit(self, name: str) -> QLineEdit:
        obj = QLineEdit()
        obj.setObjectName(self.QLineEdit_NAME + name)
        return obj

    def newQTextBrowser(self, name: str) -> QTextBrowser:
        obj = QTextBrowser()
        obj.setObjectName(self.QTextBrowser_NAME + name)
        return obj

    def newQDialogButtonBoxOKCancel(self, name: str, orientation=Qt.Orientation.Horizontal) -> QDialogButtonBox:
        obj = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
        obj.setObjectName(self.QDialogButtonBox_NAME + name)
        obj.setOrientation(orientation)
        obj.button(QDialogButtonBox.StandardButton.Ok).setText("保存")
        obj.button(QDialogButtonBox.StandardButton.Cancel).setText("取消")
        return obj

    @staticmethod
    def newQProgressDialog(widget: QWidget, text: str, cancel_cb):
        progress = QProgressDialog(widget)
        progress.setWindowTitle("请稍等")
        progress.setLabelText(text)
        progress.setFixedSize(250, 150)
        progress.setRange(0, 0)
        progress.setWindowFlag(Qt.WindowContextHelpButtonHint, False)
        progress.setWindowFlag(Qt.WindowCloseButtonHint, False)
        progress.setWindowModality(Qt.WindowModality.WindowModal)
        if cancel_cb:
            progress.setCancelButtonText("取消")
            progress.canceled.connect(cancel_cb)
        else:
            progress.setCancelButton(None)
        return progress

    @staticmethod
    def inCenter(widget: QWidget):
        """
        窗口居中显示
        :return:
        """
        center = QScreen.availableGeometry(QApplication.primaryScreen()).center()
        geo = widget.frameGeometry()
        geo.moveCenter(center)
        widget.move(geo.topLeft())

标签:obj,name,代码,pyqt,str,常用工具,self,def,NAME
From: https://www.cnblogs.com/ihid/p/16993826.html

相关文章