PySide2----UI布局 案例练习

文章讲述了在Qt环境中使用Qtdesigner设计UI界面,遇到的问题包括按钮区因外边距导致的布局问题,以及如何通过qss样式控制元素间距和显示二维码。同时,提到了多个QLabel在网格布局中的应用和调整。
摘要由CSDN通过智能技术生成

UI 设计

使用Qt designer 实现如下界面:

  1. 公共界面
    右边是按钮区(宽200)
    左边是内容区、子界面区

  2. 子界面
    在这里插入图片描述
    设计界面布局时,元素之间的距离(layoutSpacing)可为0,然后通过qss控制间距

问题1:如下问题,可以忽略。
在这里插入图片描述
问题2:按钮部分没有完整展示
在这里插入图片描述

右侧按钮区宽度一定,按钮设置外边距,会把按钮部分挤出, 如下图;增加总窗口的宽度无法解决,必须增加按钮区的宽度。
在这里插入图片描述

PySide2的控件与PyQt5 不能混合导入

程序逻辑


class App(QWidget):
    """
        传入一个子窗口,设置公共窗口
    """
    def __init__(self, ui_form, parent=None):
        super(App, self).__init__(parent=parent)
        # 传入的子窗口
        self.ui_form = ui_form

        self.resize(800, 600)
        desk = QDesktopWidget().geometry()
        width, height = desk.width(), desk.height()
        self.move(width//2 - self.width()//2, height//2 - self.height()//2)

    def init_ui(self):
        self.base_ui = Ui_base()
        self.base_ui.setupUi(self)


class Diag(App):
    def __init__(self, ui_form=Ui_diag(), parent=None):
        super(Diag, self).__init__(ui_form, parent)
        # 设置基本窗口
        self.init_ui()

        self.set_ui()

    def set_ui(self):
        self.ui_form.setupUi(self.base_ui.appPanel)
        self.setStyleSheet(get_global_style())

qss 样式

加入样式
在这里插入图片描述

/* 颜色参考 https://www.runoob.com/html/html-colorvalues.html */
QWidget#titlePanel QLabel#title {
    color: #000;
    font-size: 24px;
    font-family: "宋体";
    font-weight: bold;
    margin-left: 8px;
    margin-top: 8px;
    margin-bottom: 8px;
}

QPushButton {
    min-height: 40px;
    max-height: 40px;
    min-width: 80px;
    padding: 0 12px;
}
/* 主要按钮 */
QWidget#rightButtonPanel {
    border: 1px solid pink;
}

QWidget#rightButtonPanel QPushButton[button_type="primary"] {
    border: 1px solid blue;
    border-radius: 4px;
    color: red;
}

QWidget#rightButtonPanel QPushButton[button_type="primary"]:hover {
    background-color: #D3D3D3;
    font-weight: bold;
}

QWidget#rightButtonPanel QPushButton[button_type="primary"]:pressed {
    background-color: #C0C0C0;
    font-weight: bold;
    border: 2px solid blue;
}

QWidget#rightButtonPanel QPushButton[button_type="primary"]:disabled {
    border: 1px solid #D3D3D3;
    border-radius: 4px;
    color: #D3D3D3;
}


/* 次要按钮 */
QWidget#rightButtonPanel QPushButton {
    border: 1px solid blue;
    border-radius: 4px;
    color: blue;
}

QWidget#rightButtonPanel QPushButton:hover {
    background-color: #D3D3D3;
    font-weight: bold;
}

QWidget#rightButtonPanel QPushButton:pressed {
    background-color: #C0C0C0;
    font-weight: bold;
    border: 2px solid blue;
}

QWidget#rightButtonPanel QPushButton:disabled {
    border: 1px solid #D3D3D3;
    border-radius: 4px;
    color: #D3D3D3;
}

/* appPanel 次要按钮 */
QWidget#appPanel {
    border: 1px solid red;
}

QWidget#appPanel QPushButton {
    border: 1px solid blue;
    border-radius: 4px;
    color: blue;
    margin-left: 0;
}

QWidget#appPanel QPushButton:hover {
    background-color: #D3D3D3;
    font-weight: bold;
}

QWidget#appPanel QPushButton:pressed {
    background-color: #C0C0C0;
    font-weight: bold;
    border: 2px solid blue;
}

QWidget#appPanel QPushButton:disabled {
    border: 1px solid #D3D3D3;
    border-radius: 4px;
    color: #D3D3D3;
}

/* appPanel 二级标题 */
QWidget#appPanel QLabel[objectName^='L2'] {
    font-family: "宋体";
    font-size: 24px;
    font-weight: bold;
    border: 1px solid red;
    margin-left: 16px;
    padding-left: 8px;
}

QWidget#appPanel QLabel[objectName^='L3'] {
    font: bold 20px "宋体"; /* 注意顺序 */
    padding: 8px;
    margin-left: 0;
    border: 1px solid red;
    border-image: url(imgs/dog.jpg); /* 执行文件的 相对路径 */
}

QWidget#appPanel QCheckBox {
    font: normal 16px "宋体";
}

/* 选框 */
QWidget#appPanel QCheckBox::indicator {
    width: 24px;
    height: 24px;
}
QWidget#appPanel QCheckBox::indicator:hover {
    /*border: 1px solid red;*/
}
QWidget#appPanel QCheckBox::indicator:pressed {
    width: 25px;
    height: 25px;
}
QWidget#appPanel QCheckBox::indicator:checked {
    image: url(:/imgs/dog.jpg); /* ?? */
}

/*线性渐变*/
#btn{
	background: qlineargradient(x1:0, x2:1, stop:0 gray, stop:0.5 white, stop:1 gray); /*水平渐变 设置三个渐变点*/
}
#btn2{
	background: qlineargradient(y1:0, y2:1, stop:0 gray, stop:0.5 white, stop:1 gray); /*垂直渐变 设置三个渐变点*/
}

#btn3{
	box-shadow: inset 0 1px 3px 1px red;  /*设置阴影, PySide2 中 不支持box-shadow */
}

stylesheet 更多参考

 

Qt程序无法运行

在这里插入图片描述
如下方式配置环境变量:
在这里插入图片描述
解决!

qt中展示二维码

使用qrcode 生成二维码

def setUI(self):
    """ 生成二维码并展示 """
    import qrcode
    import numpy as np

    url = "https://www.baidu.com"
    bar_code = qrcode.make(data=url)
    # 获取PIL.Image.Image对象
    img = bar_code.get_image()
    w, h = img.size
    # 获取数据
    data = np.array(img.getdata()).reshape(h, w)

    # 展示图像
    img_view = pg.ImageView(self)
    img_view.resize(self.width(), self.height())

    img_item = pg.ImageItem(data)
    img_view.addItem(img_item)

 

多个Qlabel 网格布局

多个QLabel 网格布局的空隙问题。
在这里插入图片描述
设置网格布局的水平间隔、垂直间隔为0,并取消一些格子的边框,避免边框线过黑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laufing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值