PyQt学习(16):画图板(Qpainter),图像类:QPixmap,QImage,QPicture,QBitmap,Qss美化界面,

目录

 

 

1,图像类:QPixmap,QImage,QPicture,QBitmap

2,QSS的UI美化

2.1,使用QSSS设置窗口背景

2.2,不规则窗口的显示

2.3,设置样式

2.4,设置窗口透明

2.5,加载Qss


 

1,图像类:QPixmap,QImage,QPicture,QBitmap

PyQt中常用的图像类有四个:QPixmap,QImage,QPicture,QBitmap

--QPixmap是专门为绘图而设计的,在绘制图片时需要使用Qpixmap

--QImage提供了一个与硬件无关的图像表示函数,可以用于图片的像素级访问

--QPicture是一个绘图设备类,它继承自QPainter类,可以使用QPainter的begin()函数在QPicture上绘图,使用end()函数结束绘图,使用QPicture的save()函数将Qpainter所使用过的绘图指令保存到文件中

--QBitmap是一个继承QPixmap的简单类,它提供了1bit深度的二值图像的类,QBitmap提供的单色图像,可以用来制作游标(QCursor)或者笔刷(QBrush)

'''绘图板'''
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QHBoxLayout,QPushButton,QLabel,QComboBox,QStyleFactory
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl,QObject,Qt,QPoint
from PyQt5.QtGui import QPixmap,QPainter

class GuiDemo(QWidget):
    def __init__(self):
        super(GuiDemo, self).__init__()
        self.startPoint=QPoint()
        self.endPoint = QPoint()
        self.initUi()
    def initUi(self):
        self.setWindowTitle('demo')
        self.setGeometry(300,300,500,300)
        #鼠标绘图流程:1,建立Qpixmap绘图面板2,将面板加入到绘制到主界面3,定义鼠标函数和绘制函数绘制到绘图面板
        self.pix=QPixmap(200,200)
        self.pix.fill(Qt.white)

    def paintEvent(self, QPaintEvent):#自动调用该函数
        # 将画布绘制到主界面上
        main_painter = QPainter(self)      #必须在paintEvent中使用才有效
        main_painter.drawPixmap(0, 0, self.pix)

        #将图型绘制到画布上
        draw_painter=QPainter(self.pix)
        draw_painter.drawLine(self.startPoint,self.endPoint)
        self.startPoint=self.endPoint

    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.button()==Qt.LeftButton:
            self.startPoint=QMouseEvent.pos()
            self.endPoint=self.startPoint
    def mouseMoveEvent(self, QMouseEvent):
        if QMouseEvent.buttons() and Qt.LeftButton:
            self.endPoint=QMouseEvent.pos()
            self.update()
    def mouseReleaseEvent(self, QMouseEvent):
        if QMouseEvent.button()==Qt.LeftButton:
            self.endPoint=QMouseEvent.pos()
            self.update()


if __name__=='__main__':
    app=QApplication(sys.argv)
    demo=GuiDemo()
    demo.show()
    sys.exit(app.exec_())

2,QSS的UI美化

Qss是Qt的样式表,是用来自定义控件外观的一种机制,使页面美化和代码层分开,利于维护,QSS样式由两部分组成,其中一部分是选择器(selector),指定哪些控件会受到影响:另一部分是声明,指定哪些属性应该在控件上进行设置。声明部分是一系列的“属性:值”对

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值