deanleaf0823
2022-05-15 10:48
浏览 4

pyqt tableview左上角配色问题

我在尝试利用qtdesiger 制作一个透明的tableview后,发现在designer中预览正常,是透明的,可是放到python的项目中去使用,左上角就会变成白色的。

后来经过尝试发现,只要我把开头一句给form设置背景色的代码注释掉表格就会 变成透明的。

有没有知道原因。

这是ui代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'newstyle.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1302, 819)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
        Form.setSizePolicy(sizePolicy)
        #Form.setStyleSheet()    就是这行,如果在这里设置背景色或者图片,左上角就会变成白色。
        self.tableWidget = QtWidgets.QTableWidget(Form)
        self.tableWidget.setGeometry(QtCore.QRect(200, 100, 711, 361))
        self.tableWidget.setStyleSheet("QTableView QTableCornerButton::section {   /*左上角的button样式*/ \n"
"   color: white;     \n"
"   background-color:transparent;    /*背景颜色与表头一致*/ \n"
"   border: 3px transparent;     \n"
"   border-radius:0px;     \n"
"   border-color:transparent; \n"
"   padding: 2px transparent;        \n"
"   padding:transparent; }   \n"
"QTableView {     \n"
"color: white;                 /*表格内文字颜色*/    \n"
"gridline-color: transparent;      /*表格内框颜色*/     \n"
"background-color: transparent;               /*表格内背景色*/     \n"
"alternate-background-color: transparent;     \n"
"selection-color: white;             /*选中区域的文字颜色*/     \n"
"selection-background-color: transparent;        /*选中区域的背景色*/     \n"
"border: 1px transparent;     /*边框像素和颜色*/   \n"
"border-radius: 30px;      /*边框圆角半径*/   \n"
"padding: 1px 1px; }  \n"
"QHeaderView {       /*表头样式*/ \n"
"color: white;       \n"
"font: 14pt \"Cantarell\";\n"
"background-color: transparent;       \n"
"border:0px solid transparent;     \n"
"border-left-color: transparent;     \n"
"border-top-color: transparent;     \n"
"border-radius:0px;     \n"
"min-height:30px; }  \n"
"QHeaderView::section {     /*表头分段样式*/ \n"
"color: white;     \n"
"background-color: transparent;     \n"
"border: 1px solid transparent;     \n"
"border-radius:0px; }\n"
"")
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(6)
        self.tableWidget.setRowCount(6)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(5, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(5, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(1, 0, item)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(140, 40, 831, 481))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap(":/pic/background.png"))
        self.label.setScaledContents(True)
        self.label.setObjectName("label")
        self.label.raise_()
        self.tableWidget.raise_()

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        item = self.tableWidget.verticalHeaderItem(0)
        item.setText(_translate("Form", "新建行"))
        item = self.tableWidget.verticalHeaderItem(1)
        item.setText(_translate("Form", "新建行"))
        item = self.tableWidget.verticalHeaderItem(2)
        item.setText(_translate("Form", "新建行"))
        item = self.tableWidget.verticalHeaderItem(3)
        item.setText(_translate("Form", "nihao "))
        item = self.tableWidget.verticalHeaderItem(4)
        item.setText(_translate("Form", "新建行"))
        item = self.tableWidget.verticalHeaderItem(5)
        item.setText(_translate("Form", "新建行"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("Form", "新建列"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("Form", "新建列"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("Form", "新建列"))
        item = self.tableWidget.horizontalHeaderItem(3)
        item.setText(_translate("Form", "新建列"))
        item = self.tableWidget.horizontalHeaderItem(4)
        item.setText(_translate("Form", "新建列"))
        item = self.tableWidget.horizontalHeaderItem(5)
        item.setText(_translate("Form", "新建列"))
        __sortingEnabled = self.tableWidget.isSortingEnabled()
        self.tableWidget.setSortingEnabled(False)
        item = self.tableWidget.item(1, 0)
        item.setText(_translate("Form", "nihao"))
        self.tableWidget.setSortingEnabled(__sortingEnabled)
import rec


这是调用测试代码


import sys
import PyQt5

import sys


from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

from PyQt5.QtWidgets import *


from newstyle import Ui_Form




class QmyMainWindow(QMainWindow):

   def __init__(self, parent=None):
      super().__init__(parent)  # 调用父类构造函数,创建窗体
      self.ui = Ui_Form()  # 创建UI对象
      self.ui.setupUi(self)  # 构造UI界面


      #self.setStyleSheet("image: url(C:\Users\YAO\Desktop/newstyle/background.png);")
      #self.setAttribute(Qt.WA_TranslucentBackground)  # 窗体背景透明
      #self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool)







##  ============窗体测试程序 ================================
if __name__ == "__main__":  # 用于当前窗体测试
   app = QApplication(sys.argv)  # 创建GUI应用程序
   form = QmyMainWindow()  # 创建窗体
   form.show()
   sys.exit(app.exec_())







  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

相关推荐 更多相似问题