weixin_45198643 2019-06-11 20:44 采纳率: 0%
浏览 2491

为什么PyQt5 Qss中QPushButton按钮background-color效果无效?

PyQt5 Qss中QPushButton按钮background-color效果无效
QT样式表设置背景色 background-color无效的原因的方法不能解决问题

import sys
from PyQt5.QtWidgets import QWidget,QPushButton,QLineEdit,QTextEdit,QHBoxLayout,QVBoxLayout,QLabel,QListView,QFrame,QApplication
from PyQt5.QtCore import QEvent,Qt,QStringListModel
from PyQt5.QtGui import QIcon,QCursor,QColor
import GetPoetry
import style
class Reader(QWidget):
    mystyle = style.UiSyle().style
    def __init__(self):
        super().__init__()
        self.config_index=self.readconfig()
        self.index=self.config_index
        self.ReaderUI()
        self.maxindex=GetPoetry.GetPoetryDetail.getMaxIndex(GetPoetry)
        #self.mystyle=style.UiSyle()
    def ReaderUI(self):
        '''setup reader's UI'''

        # leftFrame
        self.leftFrame=QFrame()
        self.leftFrame.setMinimumWidth(200)

        ## add Frame
        self.readerFrame = QFrame()
        self.setObjectName("readerFrame")
        self.pageButtonFrame = QFrame()
        self.setObjectName("pageButtonFrame")

        ###Reader frame

        ##### add readerFrame widgets
        self.titleEdit=QLineEdit("title")
        self.titleEdit.setObjectName("titleEdit")
        self.authorEdit=QLineEdit("author")
        self.dynastyEdit=QLineEdit("dynasty")
        self.contentEidt=QTextEdit("content")
        self.authFrame=QFrame()

        #### readerFrame widgets Layout
        self.readerLayout = QVBoxLayout()
        ##### authFrame Layout #####
        self.authFrameLayout = QHBoxLayout()
        self.authFrameLayout.addStretch(2)
        self.authFrameLayout.addWidget(self.authorEdit,1)
        self.authFrameLayout.addWidget(self.dynastyEdit,1)
        self.authFrameLayout.addStretch(2)
        self.authFrame.setLayout(self.authFrameLayout)
        ##### authFrame Layout #####
        self.readerLayout.addWidget(self.titleEdit)
        self.readerLayout.addWidget(self.authFrame)
        self.readerLayout.addWidget(self.contentEidt)
        self.readerFrame.setLayout(self.readerLayout)


        ### pageButtonFrame

        #### add pageButtonFrame widgets
        self.nextButton=QPushButton("下一篇")
        self.nextButton.setObjectName("nextButton")
        self.previewButton=QPushButton("上一篇")
        self.previewButton.setObjectName("previewButton")
        self.readIndexEdit=QLineEdit("readIndexEdit")

        #### pageButtonFrame widgets Layout
        self.pageButtonLayout=QHBoxLayout()
        self.pageButtonLayout.addWidget(self.previewButton)
        self.pageButtonLayout.addStretch(1)
        self.pageButtonLayout.addWidget(self.readIndexEdit)
        self.pageButtonLayout.addStretch(1)
        self.pageButtonLayout.addWidget(self.nextButton)
        self.pageButtonFrame.setLayout(self.pageButtonLayout)

        ## leftFrame Layout
        self.leftFrameLayout=QVBoxLayout()
        self.leftFrameLayout.addWidget(self.readerFrame)
        self.leftFrameLayout.addWidget(self.pageButtonFrame)
        self.leftFrame.setLayout(self.leftFrameLayout)


        # right listView
        self.titleListView=QListView()
        self.listModel=QStringListModel()
        #self.title_list=[str(n) for n in range(1,11)]
        #self.listModel.setStringList(self.title_list)
        #self.titleListView.setModel(self.listModel)

        self.titleListView.setMinimumWidth(50)
        self.titleListView.setMaximumWidth(150)

        # mainwindow layout
        self.mainwindowFrame=QFrame()
        self.mainwindowLayout=QHBoxLayout()
        self.mainwindowLayout.addWidget(self.leftFrame)
        self.mainwindowLayout.addWidget(self.titleListView)
        self.mainwindowFrame.setLayout(self.mainwindowLayout)
        self.mainwindowFrame.setStyleSheet("background-color:pink")

        # window buttons
        self.minButton=QPushButton("—")
        self.closeButton=QPushButton("×")
        self.closeButton.setObjectName("closeButton")
        self.minButton.setObjectName("minButton")
        self.minButton.clicked.connect(self.showMinimized)
        self.closeButton.clicked.connect(self.close)
        self.windownButtonFrame=QFrame()
        self.windownButtonLayout=QHBoxLayout()
        self.windownButtonLayout.addStretch(18)
        self.windownButtonLayout.addWidget(self.minButton,1)
        self.windownButtonLayout.addWidget(self.closeButton,1)
        self.windownButtonFrame.setLayout(self.windownButtonLayout)
        self.windownButtonFrame.setAttribute(Qt.WA_TranslucentBackground)

        # self layout
        self.mainlayout=QVBoxLayout()
        self.mainlayout.addWidget(self.windownButtonFrame)
        self.mainlayout.addWidget(self.mainwindowFrame)
        self.setLayout(self.mainlayout)

        #主窗口属性设置
        self.setWindowTitle("唐诗宋词")
        self.setWindowIcon(QIcon("icon.jpg"))
        self.setGeometry(100,100,1000,800)
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

        #插入内容
        self.putAll()
        #按钮链接到动作
        self.nextButton.clicked.connect(self.nextButtonAction)
        self.previewButton.clicked.connect(self.previewButtonAction)
        self.titleListView.clicked.connect(self.titleListAction)

        #美化界面
        #self.qssStyle=open("style.txt","rt",encoding="utf-8").read()
        self.setStyleSheet(self.mystyle)
        self.titleEdit.setAlignment(Qt.AlignCenter)
        self.authorEdit.setAlignment(Qt.AlignCenter)
        self.dynastyEdit.setAlignment(Qt.AlignCenter)
        self.contentEidt.setAlignment(Qt.AlignCenter)
    def readconfig(self):
        try:
            f=open("config.txt","rt")
            last_index=eval(f.read())
            f.close()
        except:
            f1=open("config.txt","wt")
            f1.write("1")
            last_index=1
            f1.close()
        return last_index
    def writeconfig(self):
        f2=open("config.txt","w")
        f2.write(str(self.index))
        f2.close()
    def putTitle(self):
        title=GetPoetry.GetPoetryDetail.getTitle(GetPoetry,self.index)
        self.titleEdit.setReadOnly(False)
        self.titleEdit.setText(title)
        self.titleEdit.setReadOnly(True)
    def putAuthor(self):
        author=GetPoetry.GetPoetryDetail.getAuthorname(GetPoetry,self.index)
        self.authorEdit.setReadOnly(False)
        self.authorEdit.setText(author)
        self.authorEdit.setReadOnly(True)
    def putDynasty(self):
        dynasty=GetPoetry.GetPoetryDetail.getPubtime(GetPoetry,self.index)
        self.dynastyEdit.setReadOnly(False)
        self.dynastyEdit.setText(dynasty)
        self.dynastyEdit.setReadOnly(True)
    def putContent(self):
        content=GetPoetry.GetPoetryDetail.getContent(GetPoetry,self.index)
        self.contentEidt.setReadOnly(False)
        self.contentEidt.setText(content)
        self.contentEidt.setReadOnly(True)
    def putTitleList(self):
        titlelist=GetPoetry.GetPoetryDetail.getNotelist(GetPoetry)
        self.listModel.setStringList(titlelist)
        self.titleListView.setModel(self.listModel)
        self.titleListView.setCurrentIndex(self.listModel.index(self.index))
    def putReadIndex(self):
        self.readIndexEdit.setReadOnly(False)
        string="正在阅读:第{: ^5}篇"
        self.readIndexEdit.setText(string.format(self.index+1))
        self.readIndexEdit.setReadOnly(True)
    def putAll(self):
        self.putAuthor()
        self.putContent()
        self.putDynasty()
        self.putTitle()
        self.putTitleList()
        self.putReadIndex()

    def previewButtonAction(self):
        if self.index>0:
            self.index-=1
            self.putAll()
    def nextButtonAction(self):
        if self.index<self.maxindex:
            self.index+=1
            self.putAll()
    def titleListAction(self):
        self.index=self.titleListView.currentIndex().row()
        self.putAll()
    def mousePressEvent(self, event):
        if event.button()==Qt.LeftButton:
            self.m_drag=True
            self.m_DragPosition=event.globalPos()-self.pos()
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))
    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_drag:
            self.move(QMouseEvent.globalPos()-self.m_DragPosition)
            QMouseEvent.accept()
    def mouseReleaseEvent(self, QMouseEvent):
        self.m_drag=False
        self.setCursor(QCursor(Qt.ArrowCursor))
if __name__ == "__main__":
    app=QApplication(sys.argv)
    myReader=Reader()
    myReader.show()

    sys.exit(app.exec_())

Qss代码如下:

QWidget
                {
                        font: 10pt "微软雅黑";
                        background:transparent;
                        border:0px;
                        border-radius:30px
                }
            QLineEdit
                {
                        border:0px
                }
            QFrame#pageButtonFrame
                {
                        border:0px
                }
            QTextEdit
                {
                        border:1px solid red;
                        border-radius:20px
                }
            QListView
                {
                        border:1px solod red;
                        border-radius:10px
                        }
            QLineEdit#titleEdit
                {
                        font:18pt "微软雅黑";
                }
            QPushButton#previewButton
                {
                        border:2px solid red;
                        color:#FF6600;font-size:8pt;
                        padding :8px;
                        border-radius:8px
                }
            QPushButton#previewButton:hover
                {
                        color:white;
                        border:1px solid red;
                        background:#FF6600;
                }
            QPushButton#nextButton:hover
                {
                        background:#FF6600;
                        color:white;
                        border:1px solid red;    
                }
            QPushButton#nextButton
                {
                        border:2px solid red;
                        color:#FF6600;font-size:8pt;
                        padding :8px;
                        border-radius:8px
                }
            QPushButton#closeButton:hover 
                {
                        background-color:red;
                        border-radius:10px;
                        border:0px;
                        color:white
                }  
            QPushButton#closeButton 
                {
                        border-radius:10px;
                        border:2px solid red;
                        color:red;
                        font-weight:700
                }  
            QPushButton#minButton:hover 
                {
                        background-color:pink;
                        border-radius:10px;
                        border:0px;color:white;
                }  
            QPushButton#minButton
                {
                        border-radius:10px;
                        border:2px solid pink;
                        color:pink;
                        font-weight:700
                }  

实际效果如图:图片说明

  • 写回答

1条回答 默认 最新

  • weixin_45198643 2019-06-11 21:13
    关注

    找到原因了,原来我的代码中设置了一个样式

    self.mainwindowFrame.setStyleSheet("background-color:pink")
    

    我把这句注释后,在再Qss文件中加入下,面的代码后,那个 QPushButton#previewButton:hover和QPushButton#nextButton:hover的效果就实现了。

    QFrame#mainwindowFrame
                    {
                            background-color:gold
                    }
    

    这个问题折腾了我两个晚上,现在终于解决了。虽然不知道原因,但还是可以总结一个教训:想使用Qss来统一设定界面样式时,千万要在程序代码里面加入给单独的对象使用setStyleSheet语句指定样式。

    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?