选择QTableWidgetItem时如何保持原始文本颜色,使用setForeground()设置
我的问题:
现在,正如您在下面的图2 上看到的那样,当选择第一行时,所有文本的颜色都设置为 black ,而不是保持其原始图1 上显示的样式。
选择行时是否有一种方法可以保留原始颜色(如图3 所示)吗?我应该继续依靠CSS吗?还是应该创建qabstractitemdelegate
来解决此问题?
如果需要一个qabtractitemdelegate
,它比使用CSS更快还是会减慢应用程序的速度?
图1:没有任何选择的情况。
图2:用户选择行0时的外观。
图3:在油漆上制作,我希望它的行为。
设置:
我正在使用pyside2:qt5在Windows 10上的Python 3.10。
在我的示例中,在将行插入其中之前,我已经在qtableWidget
上设置了一些属性:
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.setDragDropMode(QAbstractItemView.NoDragDrop)
self.setFocusPolicy(Qt.NoFocus)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.setAlternatingRowColors(True)
self.setShowGrid(False)
tv = self.verticalHeader()
tv.setVisible(False)
tv.setSectionsClickable(False)
tv.setSectionResizeMode(QHeaderView.Fixed)
tv.setDefaultAlignment(Qt.AlignHCenter)
th = self.horizontalHeader()
th.setHighlightSections(False)
th.setStretchLastSection(True)
th.setSectionResizeMode(QHeaderView.Interactive)
# With Exception to Column 0: ResizeMode is ResizeToContents
然后,我创建了一个函数以轻松将项目插入其中。 qtableWidgetItem 的几种方法:
# Insert the row at the table, on specified index
self.insertRow(index)
# Get the QTableWidgetItem from row r and column c, and ensure it exists
item = self.item(r, c)
if (item is None):
item = QTableWidgetItem()
self.setItem(r, c, item)
# To change the text color, using a QColor
QTableWidgetItem.setForeground()
# To change the text alignment, using Qt Flags
QTableWidgetItem.setTextAlignment()
# To set the text inside the cell
QTableWidgetItem.setText()
我的CSS与这些小部件有关
QTableWidget {
border: 1px solid #ddd;
}
QTableWidget::item {
background-color: white;
}
QTableWidget::item:alternate {
background-color: #f1f1f1;
}
/* I Have already tried setting 'color: inherit', but there is no effect */
QTableView::item:selected, QTableView::item:alternate:selected {
background-color: #92b7d1;
}
QHeaderView::section {
background-color: lightgray;
}
QHeaderView::section:hover {
background-color: silver;
}
这是一个很大的功能,它依赖于qtableWidget
和:“颜色”列上的圆形彩色正方形是Fontawesomes的Unicode文本,我下载了OTF文件并使用Pyside2中的
QfontDatabase
添加到应用程序中。基本上,这个人在这里。
My Problem:
Right now, as you can see on Fig 2 below, as the first row is selected, all texts' colors are set to black, instead of keeping their original style displayed on Fig 1.
Is there a way to keep the original colors when the row is selected (as shown in Fig 3)? Should I keep relying on CSS alone or should I just create a QAbstractItemDelegate
to fix this problem?
If a QAbstractItemDelegate
is needed, is it faster than using CSS or does it slow down the application?
Fig 1: How it looks like without any selection.
Fig 2: How it looks like when user selects Row 0.
Fig 3: Made on Paint, how I would like it to behave.
Settings:
I am using PySide2: Qt5 on Python 3.10 on Windows 10.
In my example, I have set a few properties on my QTableWidget
before inserting rows inside it:
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.setDragDropMode(QAbstractItemView.NoDragDrop)
self.setFocusPolicy(Qt.NoFocus)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.setAlternatingRowColors(True)
self.setShowGrid(False)
tv = self.verticalHeader()
tv.setVisible(False)
tv.setSectionsClickable(False)
tv.setSectionResizeMode(QHeaderView.Fixed)
tv.setDefaultAlignment(Qt.AlignHCenter)
th = self.horizontalHeader()
th.setHighlightSections(False)
th.setStretchLastSection(True)
th.setSectionResizeMode(QHeaderView.Interactive)
# With Exception to Column 0: ResizeMode is ResizeToContents
Then I created a function to easily insert items inside it. It's a big function, which relies on a few methods from QTableWidget
and QTableWidgetItem
shown here:
# Insert the row at the table, on specified index
self.insertRow(index)
# Get the QTableWidgetItem from row r and column c, and ensure it exists
item = self.item(r, c)
if (item is None):
item = QTableWidgetItem()
self.setItem(r, c, item)
# To change the text color, using a QColor
QTableWidgetItem.setForeground()
# To change the text alignment, using Qt Flags
QTableWidgetItem.setTextAlignment()
# To set the text inside the cell
QTableWidgetItem.setText()
My CSS, related to these widgets, is:
QTableWidget {
border: 1px solid #ddd;
}
QTableWidget::item {
background-color: white;
}
QTableWidget::item:alternate {
background-color: #f1f1f1;
}
/* I Have already tried setting 'color: inherit', but there is no effect */
QTableView::item:selected, QTableView::item:alternate:selected {
background-color: #92b7d1;
}
QHeaderView::section {
background-color: lightgray;
}
QHeaderView::section:hover {
background-color: silver;
}
Forgot to mention after posting: the rounded colored square, on 'Color' Column, is an Unicode Text from FontAwesomes, which I downloaded the OTF file and added to the application using the QFontDatabase
from PySide2. Basically this guy here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于仍然想知道的任何人,一年左右后,我发现存在 module qicons 甚至 qwidgets 的fontawesome(和其他源)图标。
使用该模块,您可以在创建过程中指定颜色参数并随意更改图标的颜色。比完成我在这篇文章中所做的一切要容易得多。
我建议您检查一下,如果您要浏览此帖子的路线。
For anyone still wondering, after an year or so, I found out that there exists a module that converts FontAwesome (and other sources of) icons into QIcons and even QWidgets.
Using that module, you can specify a color parameter and change the icon's color at will during its creation. Much, much easier than doing everything I've done on this post.
I recommend checking it out if you're going the through the route of this post.