如何为特定的QTableWidgetItem或QTableWidget中的行分配边框? [英] How do I assign a border to a specific QTableWidgetItem or a row in a QTableWidget?
问题描述
我试图根据item(cell)中包含的信息,使QTableWidget中的某些单元格具有不同的彩色边框.
I am trying to make certain cells in my QTableWidget have different colored borders based on the information contained in an item(cell).
我不想选择那些单元格并使用选择颜色样式,因为需要选择/突出显示不同的单元格.
I do not want to select those cells and use the selection-color styles because different cells need to be selected/highlighted.
例如
我有一个3列3行的表格.所有单元格中的每个单元格均具有简单文本.
[] [名称] [值] [单位]
[1] [一个] [1] [cm]
[2] [两个] [2] [in]
[3] [三] [3] [m]
for ex.
I have a table with 3 columns and 3 rows. All the cells have simple text in each of them.
[] [Name] [Value] [Units]
[1] [one] [1] [cm]
[2] [two] [2] [in]
[3] [three][3] [m]
第一行被用户选中并突出显示,后台的进程将表中的值更新,并将第三行中的值更新为4.现在,我想使第三行周围有一个红色边框它.
The 1st row is selected by the user and is highlighted, a process in the background updates the values in the table and updates the value in the 3rd row to 4. Now I want to make the 3rd row have a red border around it.
推荐答案
要更改边框本身,您可能需要创建一个自定义委托,该委托可以执行以下操作:
To change the border itself you'll probably need to create a custom delegate that does something along these lines:
class MyDelegate : public QItemDelegate {
public:
MyDelegate( QObject *parent ) : QItemDelegate( parent ) { }
void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const {
QItemDelegate::paint( painter, option, index );
if( /* some condition */ ) {
painter->setPen( Qt::red );
painter->drawRect( option.rect );
}
}
}
然后您可以致电:
myTableWidget->setItemDelegate( new MyDelegate(this) );
您可以使用QTableWidgetItem::setData()
和QModelIndex::data()
函数在表和委托之间来回传递必要的信息
You can use QTableWidgetItem::setData()
and the QModelIndex::data()
functions to pass the necessary information back and forth between your table and the delegate
这篇关于如何为特定的QTableWidgetItem或QTableWidget中的行分配边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!