PyQT5水平垂直布局


from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui
import sys
import cv2


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    main_win = QtWidgets.QMainWindow()
    main_win.setWindowTitle("QLabel图像显示演示")
    image_label = QtWidgets.QLabel()
    txt_label = QtWidgets.QLabel()

    # 方法一:使用QtGui直接显示
    # pixmap = QtGui.QPixmap("./image/img1.png")
    # pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式

    # 方法二:使用cv2显示
    src = cv2.imread("./image/img1.png")   # BGR
    image = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)   # 将BGR转为RGB
    h, w, c = image.shape
    img = QtGui.QImage(image.data, w, h, 3*w, QtGui.QImage.Format_RGB888)
    pixmap = QtGui.QPixmap(img)
    pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
    image_label.setPixmap(pix)   # 设置图像显示
    image_label.setAlignment(QtCore.Qt.AlignCenter)   # label上的内容居中显示
    image_label.setStyleSheet("background-color:blue;color:green")    # 背景颜色设置
    # main_win.setCentralWidget(image_label)

    txt_label.setText("Hello, PyQT5")
    txt_label.setAlignment(QtCore.Qt.AlignCenter)   # label上居中显示
    txt_label.setStyleSheet("background-color:pink;color:green")    # 背景颜色设置
    font = QtGui.QFont()
    font.setBold(True)
    font.setPointSizeF(32)
    txt_label.setFont(font)

    label3 = QtWidgets.QLabel()
    label3.setText("你好, OpenCV")
    label3.setStyleSheet("background-color:gray;color:red")  # 背景颜色设置

    panel2 = QtWidgets.QWidget()
    hvoxlayout = QtWidgets.QVBoxLayout()
    hvoxlayout.addWidget(txt_label)
    hvoxlayout.addWidget(label3)
    panel2.setLayout(hvoxlayout)

    panel1 = QtWidgets.QWidget()
    hhoxlayout = QtWidgets.QHBoxLayout()
    hhoxlayout.addWidget(panel2)
    hhoxlayout.addWidget(image_label)
    panel1.setLayout(hhoxlayout)

    main_win.setCentralWidget(panel1)
    main_win.setMinimumSize(1080, 720)
    main_win.show()
    app.exec_()

posted @ 2024-06-04 15:37  星空28  阅读(6)  评论(0编辑  收藏  举报