pyqt 点点滴滴


记录PyQt点点滴滴


1. 对无边框的QT窗体,设置点击任务栏按钮最小化和还原事件

self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowSystemMenuHint |  Qt.WindowMinimizeButtonHint)
    # 第一个参数是设置无边框。
    # 第二个参数是允许任务栏按钮右键菜单,
    # 第三个参数是允许最小化与还原。

    然后;

    self.showMaximized() # 最小化
    self.showMinimized() # 最大化

2. 普通用户,启动pyqt 程序,获取root权限

pkexec  env  DISPLAY=$DISPLAY  XAUTHORITY=$XAUTHORITY      /opt/bin/client/client_run.sh

 client_run.sh 为启动脚本

3. 国际化

1) 生成.ts文件

lupdate  -verbose  *.cpp  -ts  fileName_zh.ts

2) 修改 .ts 文件

linguist-qt4  fileName_zh_CN.ts

3) 生成 .qm 文件

lrelease-qt4  fileName_zh_CN.ts

4. Qt/PyQt 实现 画对话框窗口圆角

1). 基于重绘

#!/usr/bin/env python
#coding=utf-8

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QDialog, QPainter, QColor


class Dialog(QDialog):
    def __init__(self,parent = None):
        super(Dialog, self).__init__(parent)
        self.setAttribute(Qt.WA_TranslucentBackground, True) # 一定要有
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint
                   | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint
                   | Qt.WindowMaximizeButtonHint)

        self.move(100,100)
        self.setFixedSize(500,300)

    def paintEvent(self, paintEvent):
        painter = QPainter(self)
        painter.setBrush(QColor("#CCCCFF"))
        painter.setPen(QColor("#00FF00"))
        # painter.setRenderHint(QPainter.Antialiasing) ## 抗锯齿
        painter.drawRoundedRect(0,0,self.width()-1,self.height() -1,20,20)


if __name__ == '__main__':
    from PyQt4.QtGui import QApplication
    import sys
    app = QApplication(sys.argv)
    dlg = Dialog()
    dlg.show()
    app.exec_()

2. 基于setMask过滤

a). QT 实现
    QPainterPath path;
    QRectF rect = QRectF(0,0,480,640);
    path.addRoundRect(rect,5,5);
    QPolygon polygon= path.toFillPolygon().toPolygon();//获得这个路径上的所有的点
    QRegion region(polygon);//根据这些点构造这个区域
    setMask(region);
b). PyQt 实现
    path =  QPainterPath()
    rect = QRectF(0,0,self.width(),self.height())
    path.addRoundedRect(rect,10,10)
    polygon = path.toFillPolygon().toPolygon()

    region = QRegion(polygon)
    self.setMask(region)

或者

class Dialog(QDialog):
    def __init__(self,parent = None):
        super(Dialog, self).__init__(parent)
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint
                   | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint
                   | Qt.WindowMaximizeButtonHint)

        self.move(100,100)
        self.setFixedSize(500,300)

        self.bitmap = QBitmap(self.size()) #生成一张位图
        painter = QPainter(self.bitmap)     #QPainter用于在位图上绘画

        painter.fillRect(self.rect(),Qt.white) #填充位图矩形框(用白色填充) 
        painter.setBrush(QColor(0,0,0)) #黑色Brush
        painter.setRenderHint(QPainter.Antialiasing)
        painter.drawRoundedRect(self.rect(),50,50) #在位图上画圆角矩形(用黑色填充)
        #
        self.setMask(self.bitmap) #使用setmask过滤即可

4. 按钮样式

setStyleSheet(
 "QPushButton     { background-color:#FFFFFF;"
                               "color:#000000;"
                               "border-color:#FFFFFF;"
                               "font:bold 15pt;"   # 字体大小
                               "border-width:2px;"
                               "border-style:solid;" # 实线
                               "border-radius:8px;"  # 圆角
                                "}"
            "QPushButton:hover{ "
                              "background:red;"
                              "color:#FFFFFF;"
                              "border-color:red;"
                              "border-width:2px;"
                              "border-style:solid;"
                              "border-radius:8px;"
                                "}"
            "QPushButton:pressed{"
                                "background: #FF0000;"
                                "border-color:#3f78ad;"
                                "border-width:2px;"
                                "border-style:solid;"
                                # "border-radius:8px;"
                                "}"
)

5.  窗口拖动

 a). Qt 实现


//在Widget的.h文件中
void mouseMoveEvent(QMouseEvent*event);
void mousePressEvent(QMouseEvent*event);
void mouseReleaseEvent(QMouseEvent*event);
QPointdrag Position;
//在Widget的.cpp文件中
void LoginDialog::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        dragPosition = QPoint(-1, -1);
        event->accept();
    }
}

void LoginDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void LoginDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() &Qt::LeftButton)
    {
        if (dragPosition != QPoint(-1, -1))
            move(event->globalPos() - dragPosition);
        event->accept();
    }
}

b). PyQt实现

from PyQt4.QtCore import Qt, QPoint
from PyQt4.QtGui import QDialog

class Dialog(QDialog):
    def __init__(self,parent = None):
        super(Dialog, self).__init__(parent)
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint
                   | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint
                   | Qt.WindowMaximizeButtonHint)

        self.setFixedSize(500,300)
        self.__dragPosition = QPoint(-1, -1)

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton :
            self.__dragPosition = QPoint(-1, -1)
            event.accept()

    def mousePressEvent(self, event):
        if (event.button() == Qt.LeftButton):
            self.__dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self ,event):
        if (event.buttons() & Qt.LeftButton):
            if (self.__dragPosition != QPoint(-1, -1)):
                self.move(event.globalPos() - self.__dragPosition)
            event.accept()

if __name__ == '__main__':
    from PyQt4.QtGui import QApplication
    import sys
    app = QApplication(sys.argv)
    dlg = Dialog()
    dlg.show()
    app.exec_()

6.  鼠标样式

QT之鼠标样式

 QVariant

python列表和QVariant

border="0" width="320" height="86" src="http://music.163.com/outchain/player?type=2&id=16966389&auto=1&height=66">

你可能感兴趣的:(pyqt,qt,pyqt)