当前位置:   article > 正文

常用pyqt5 API

pyqt5 api

python

os.path.exists(a_pathFile)

获取python系统路径

import distutils.sysconfig
distutils.sysconfig.get_python_lib(True)

    BASE_EXEC_PREFIX = r'C:\Users\Win_Lin\AppData\Local\Programs\Python\Py...
    BASE_PREFIX = r'C:\Users\Win_Lin\AppData\Local\Programs\Python\Python3...
    EXEC_PREFIX = r'C:\Users\Win_Lin\AppData\Local\Programs\Python\Python3...
    PREFIX = r'C:\Users\Win_Lin\AppData\Local\Programs\Python\Python36-32'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
鼠标跟踪 配合QMouseMoveEvent
self.setMouseTracking(True);
self.centralWidget.setMouseTracking(True);
  • 1
  • 2
  • 3

QWidget

#鼠标穿透
self.setAttribute(Qt.WA_TransparentForMouseEvents,True);

#widget背景样式不被父窗体覆盖
self.setAttribute(Qt.WA_StyledBackground, True);

#窗体模态
self.setWindowModality(Qt.ApplicationModal);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1。设置为模态
header 1header 2header 3
Qt::NonModal0The window is not modal and does not block input to other windows.
Qt::ApplicationModal2The window is modal to the application and blocks input to all windows.
Qt::WindowModal1The window is modal to a single window hierarchy and blocks input to its parent window, all grandparent windows, and all siblings of its parent and grandparent windows.
self.setWindowModality()
  • 1

QDialog

QWidget::setAttribute(Qt::WA_TransparentForMouseEvents,true);

QMessageBox

  • 1 。 连环确认
msg=QMessageBox.warning(self, '请确认', '已满10条,是否重新开始?',
                    QMessageBox.Yes|QMessageBox.No, QMessageBox.No)
if msg==QMessageBox.Yes:
    #ToDo

elif msg==QMessageBox.No:
    msg=QMessageBox.warning(self, '生成脚本', '开始生成脚本。',
                    QMessageBox.Yes|QMessageBox.No, QMessageBox.Yes)

    if msg==QMessageBox.Yes:
        #ToDo
    elif msg==QMessageBox.No:
        #ToDo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 2 。 弹窗报错装饰器
import traceback
from PyQt5 import  QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

def showERROR():
    def _doWhat(fun)
        try:
            func()
        except:
            errmsg = traceback.format_exc()      
            QMessageBox.warning(QWidget(), '请确认', errmsg,
                        QMessageBox.Ok)
    return _doWhat()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

QAbstractItemView

#只读
self.setEditTriggers(QAbstractItemView.NoEditTriggers); 
  • 1
  • 2

QTableWidget

  • 1 。充满行/列宽
header 1header 2header 3header 4
QHeaderView::Interactive0可拉伸,默认用户可设置,也可被程序设置成默认大小
QHeaderView::Fixed2不可拉,等宽用户不可更改列宽
QHeaderView::Stretch1不可拉根据空间,自动改变列宽,用户与程序不能改变列宽
QHeaderView::ResizeToContents3不可拉,根据内容调整根据内容改变列宽,用户与程序不能改变列宽
self.horizontalHeader().setSectionResizeMode(3)#列宽设置

self.horizontalHeader().setStretchLastSection(True); #充满列宽    

self.verticalHeader().setSectionResizeMode(1)#行高设置          

self.verticalHeader().setStretchLastSection(True); #充满行高   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

自动换行

  • 2 。行选择模式
    效果图
self.tableWidget.setSelectionBehavior(QTableWidget.SelectRows)#行选择模式
self.tableWidget.setSelectionMode(QAbstractItemView.SingleSelection);#无法拖拽选择
  • 1
  • 2

删除多行

    self.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu) #弹出菜单
    self.tableWidget.customContextMenuRequested.connect(self.myListWidgetContext)#右键请求

    def myListWidgetContext(self):
        popMenu =QMenu()

        popMenu.addAction(u'删除行',lambda:self.del_Item(1))

        popMenu.exec_(QCursor.pos())#鼠标位置

    def del_Item(self, type=1):

        for model_index in self.tableWidget.selectionModel().selectedRows():     
            index = QtCore.QPersistentModelIndex(model_index)         
            self.tableWidget.removeRow(index.row())

    # Listwidget
    def del_Item(self, type=1):

        for model_index in self.listWidget.selectedItems():     
            row = self.listWidget.indexFromItem(model_index).row()
            self.listWidget.takeItem(row);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Spacer

self.spacer.changeSize(100, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)#改变弹簧长度
self.Layout.invalidate()# 重新布局
  • 1
  • 2
  • 3

Style

app = QtWidgets.QApplication(sys.argv)
  • 1
  • 1。qt自带
app.setStyle(QStyleFactory.create("Fusion"))
  • 1
  • 2。三方库
import qdarkstyle
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) 
  • 1
  • 2
  • 3。自定义qss
ui = MainWindow()
styleFile = './style.css'
qssStyle = CommonHelper.readQss( styleFile )  
ui.setStyleSheet( qssStyle )     
ui.show()   
  • 1
  • 2
  • 3
  • 4
  • 5

QStatusBar

self.statusBar.showMessage(" 菜单选项被点击了", msec=5000)
self.statusBar.addWidget(*QWidget)#加到左下角
self.statusBar.addPermanentWidget(*QWidget)#加到右下角
  • 1
  • 2
  • 3

https://blog.csdn.net/cqltbe131421/article/details/78646228

buttonGroup

self.buttonGroup.checkedButton().text(), 
  • 1

QPainter

self.pixmap=QPixmap()
self.pixmap.load(':/image/main.jpg')

def paintEvent(self, e):

    painter=QPainter(self) ;
    painter.drawPixmap(0,0,self.width(), self.height(), self.pixmap);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

字体图标

  • fontawesome

self.b1.setText(“\uf0b1”+ “\n”*line+ “你好啊”)
self.b2.setText(“\uf0e8”+ “\n”*line+ “你好啊”)
self.b3.setText(“\uf0f0”+ “\n”*line+ “你好啊”)
self.b4.setText(“\uf0ad”+ “\n”*line+ “你好啊”)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    app.setStyle(QStyleFactory.create("Fusion"))

    fontId = QFontDatabase.addApplicationFont(":/image/fontawesome-webfont.ttf")
    fontName = QFontDatabase.applicationFontFamilies ( fontId )[0]
    app.setFont(QFont(fontName, 15));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • -
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/930904
推荐阅读
  

闽ICP备14008679号