【发布时间】: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