【问题标题】:On mouse click changing selection's background color in tableview PyQt on Linux在Linux上的tableview PyQt中单击鼠标更改选择的背景颜色
【发布时间】:2016-01-21 19:47:45
【问题描述】:

我试图在双击列表中的项目时通知用户。

我使用了setStyleSheet,结果发现它在unity和cinnamon上不起作用,而在KDE和i3上起作用。

我对 qbrushes 或其他一些我正在谷歌搜索的东西并不是那么精通。

那么解决这个问题的最佳方法是什么,以便它在任何地方都有效?谢谢。

这是我的解决方案的 GIF 动图。

这是它的代码,它的pyqt5,python3

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

class My_Model_table(QAbstractTableModel):
    def __init__(self, table_data=[], parent=None):
        super().__init__()
        self.table_data = table_data

    def rowCount(self, parent):
        return len(self.table_data)

    def columnCount(self, parent):
        return 1

    def data(self, index, role):
        if role == Qt.DisplayRole:
            value = self.table_data[index.row()]
            return value
        if role == Qt.TextAlignmentRole:
            return Qt.AlignCenter

class My_table(QTableView):
    def __init__(self, parent=None):
        super().__init__()
        self.activated.connect(self.double_click_enter)

    def double_click_enter(self, QModelIndex):
        row = QModelIndex.row()
        self.setStyleSheet('selection-background-color:red;')
        self.alarm = QTimer()
        self.alarm.timeout.connect(self.row_color_back)
        self.alarm.setSingleShot(True)
        self.alarm.start(200)
        return

    def row_color_back(self):
        self.setStyleSheet('')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    data = ['1', '2', '3', '4', '5']
    main_table = My_table()
    main_table.setModel(My_Model_table(data))
    main_table.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt tableview pyqt5


    【解决方案1】:

    尝试覆盖mousePressEventmouseReleaseEvent 这是对代码的修改。我相信这应该适用于所有桌面环境

    #!/usr/bin/python3
    # -*- encoding: utf-8 -*-
    
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    import sys
    
    class TableData( QAbstractTableModel ):
        def __init__( self, data = [], parent = None ):
            super( QAbstractTableModel, TableData ).__init__( self, parent )
            self.data = data
    
        def rowCount( self, parent ):
            return len( self.data )
    
        def columnCount( self, parent ):
            return 1
    
        def data( self, index, role ):
            if role == Qt.DisplayRole:
                return self.data[ index.row() ]
            elif role == Qt.TextAlignmentRole:
                return Qt.AlignCenter
    
    class Table( QTableView ):
    
        def __init__( self, parent = None ):
            super( QTableView, Table ).__init__( self )
    
        def mousePressEvent( self, mpEvent ) :
            self.setStyleSheet( "QTableView{ selection-background-color: red; }" )
            QTableView.mousePressEvent( self, mpEvent )
    
        def mouseReleaseEvent( self, mrEvent ) :
            self.setStyleSheet( "QTableView{ selection-background-color: none; }" )
            QTableView.mouseReleaseEvent( self, mrEvent )
    
    if __name__ == '__main__':
    
        app = QApplication( sys.argv )
    
        Gui = Table()
        Gui.setModel( TableData( ['1', '2', '3', '4', '5'], Gui ) )
        Gui.show()
    
        sys.exit( app.exec_() )
    

    PS:我刚刚在 KDE/Plasma 5 中尝试过这个。我没有 GNOME/Unity 界面来检查代码。

    【讨论】:

    • 不,对unity不起作用,我认为setStyleSheet的方式不会带来成功。
    【解决方案2】:

    好的,当我查看qdarkstylesheet 时发现了我的错误。 而不仅仅是selection-background-color,我应该使用QTableView::item:selected:active

    gif 来自 ubuntu

    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    import sys
    
    
    class My_Model_table(QAbstractTableModel):
        def __init__(self, table_data=[], parent=None):
            super().__init__()
            self.table_data = table_data
    
        def rowCount(self, parent):
            return len(self.table_data)
    
        def columnCount(self, parent):
            return 1
    
        def data(self, index, role):
            if role == Qt.DisplayRole:
                value = self.table_data[index.row()]
                return value
            if role == Qt.TextAlignmentRole:
                return Qt.AlignCenter
    
    
    class My_table(QTableView):
        def __init__(self, parent=None):
            super().__init__()
            self.activated.connect(self.double_click_enter)
    
        def double_click_enter(self, QModelIndex):
            row = QModelIndex.row()
    
            self.setStyleSheet(
               '''
               QTableView::item:selected:active {
                    background: #00ff00;
                }
               '''
            )
    
            self.alarm = QTimer()
            self.alarm.timeout.connect(self.row_color_back)
            self.alarm.setSingleShot(True)
            self.alarm.start(200)
            return
    
        def row_color_back(self):
            self.setStyleSheet('')
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        data = ['1', '2', '3', '4', '5']
        main_table = My_table()
        main_table.setModel(My_Model_table(data))
    
        main_table.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2012-11-08
      • 2018-09-12
      • 2018-03-04
      • 2019-01-23
      • 2011-12-18
      • 1970-01-01
      • 2018-12-22
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多