NSTableView中设置selectedRow的高亮颜色

有两种方法:第一种是继承NSCell:

                      第二种是继承NSTableView。

在tableView中,使用

  1. [myView setUsesAlternatingRowBackgroundColors:NO];//取消行与行之间蓝白交替显示的背景  
[myView setUsesAlternatingRowBackgroundColors:NO];//取消行与行之间蓝白交替显示的背景

继承NSCell:

  1. @interface subNSTableCell : NSCell  
  2. {  
  3.     NSColor*                _cellBKColor;  
  4.     NSColor*                _cellFontColor;  
  5.     NSAttributedString*                     _cellAttributedString;  
  6. }  
  7.   
  8. - (void)setSelectionBKColor:(NSColor*)cellColor;  
  9. - (void)setSelectionFontColor:(NSColor*)cellFontColor;  
  10. - (NSAttributedString*)getCellAttributes;  
  11.   
  12. @end  
@interface subNSTableCell : NSCell
{
	NSColor*				_cellBKColor;
	NSColor*				_cellFontColor;
	NSAttributedString*                     _cellAttributedString;
}

- (void)setSelectionBKColor:(NSColor*)cellColor;
- (void)setSelectionFontColor:(NSColor*)cellFontColor;
- (NSAttributedString*)getCellAttributes;

@end

设置color和属性:

  1. - (NSAttributedString*)getCellAttributes  
  2. {  
  3.     NSDictionary*  _attributes = [NSDictionary dictionaryWithObjectsAndKeys:_cellFontColor,NSForegroundColorAttributeName,nil];  
  4.     NSString* _cellString = [self stringValue];  
  5.       
  6.     _cellAttributedString = [[[NSAttributedString alloc]  
  7.                               initWithString:_cellString attributes:_attributes] autorelease];  
  8.       
  9.     return _cellAttributedString;  
  10. }  
- (NSAttributedString*)getCellAttributes
{
	NSDictionary*  _attributes = [NSDictionary dictionaryWithObjectsAndKeys:_cellFontColor,NSForegroundColorAttributeName,nil];
	NSString* _cellString = [self stringValue];
	
	_cellAttributedString = [[[NSAttributedString alloc]
							  initWithString:_cellString attributes:_attributes] autorelease];
	
	return _cellAttributedString;
}

然后重写方法highlightColorWithFrame:inView:

  1. - (NSColor*)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView  
  2. {  
  3.     NSRect newRect = NSMakeRect(cellFrame.origin.x - 1, cellFrame.origin.y, cellFrame.size.width + 5, cellFrame.size.height);  
  4.     if (_cellBKColor)  
  5.     {  
  6.         [_cellBKColor set];  
  7.         NSRectFill(newRect);  
  8.     }  
  9.       
  10.     [self setAttributedStringValue:[self getCellAttributes]];  
  11.       
  12.     return nil;  
  13. }  
- (NSColor*)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
	NSRect newRect = NSMakeRect(cellFrame.origin.x - 1, cellFrame.origin.y, cellFrame.size.width + 5, cellFrame.size.height);
	if (_cellBKColor)
	{
		[_cellBKColor set];
		NSRectFill(newRect);
	}
	
	[self setAttributedStringValue:[self getCellAttributes]];
	
	return nil;
}

最后,在tableView中使用子类化的cell。

  1. subNSTableCommonCell*   tTableCell = nil;  
  2.       
  3.     tTableCell = [[subNSTableCommonCell alloc] initTextCell:@""];  
  4.       
  5.     [tTableCell setSelectionBKColor:[NSColor lightGrayColor]];  
  6.     [tTableCell setSelectionFontColor:[NSColor redColor]];  
  7.       
  8.     [[[mytableView tableColumns] objectAtIndex:0] setDataCell:tTableCell];  
subNSTableCommonCell*	tTableCell = nil;
	
	tTableCell = [[subNSTableCommonCell alloc] initTextCell:@""];
	
	[tTableCell setSelectionBKColor:[NSColor lightGrayColor]];
	[tTableCell setSelectionFontColor:[NSColor redColor]];
	
	[[[mytableView tableColumns] objectAtIndex:0] setDataCell:tTableCell];

这样继承的cell,在处于选中状态时,能够按照我的设置背景显示成lightGrayColor,cell中的字体为redColor。

但是当选中的一行含有多列时,cell与cell之间的空隙会被系统的那个高亮颜色——蓝色给占据。即使只有一行时,在cell的边框能很明显的看见蓝色。

然后在网上查找到的资源,继承NSTableView,直接改变处于选中状态的系统设置的蓝色。

继承NSTableView:

将以下方法加入到子类化的tableView中可以将tableView中处于选中状态的行按照_highlightBKColor的颜色来显示。_highlightBKColor可以自行设置。

  1. - (id)_highlightColorForCell:(id)cell   
  2. {  
  3.       
  4.     if([self selectionHighlightStyle] == 1)   
  5.     {  
  6.         return nil;  
  7.     }   
  8.     else   
  9.     {  
  10.         return _highlightBKColor;  
  11.     }  
  12. }  
- (id)_highlightColorForCell:(id)cell 
{
	
	if([self selectionHighlightStyle] == 1) 
	{
		return nil;
	} 
	else 
	{
		return _highlightBKColor;
	}
}
另外,设置选中状态字体颜色等可以放在以下方法中执行。

  1. - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex  
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex

在QT的QTreeWidget,你可以通过一些方法来设置节点的高亮,通常用于标识选中的项目或者是特定状态。以下是一些步骤: 1. **设置默认样式**: 首先,确保你有一个QTreeWidgetItem的样式已经配置好,比如背景色、前景色等基础属性。可以使用`QTreeWidgetItem::setBackground()`和`QTreeWidgetItem::setForeground()`方法设置这些样式。 2. **选中高亮**: - 在`QTreeWidgetItem::itemSelected()`槽函数,可以改变选中的项目的样式。例如: ```cpp void MyTreeWidget::itemClicked(QTreeWidgetItem *item, int column) { if (item->isSelected()) item->setBackgroundColor(QColor("lightblue")); // 设置蓝色背景 else item->setBackgroundColor(Qt::white); // 取消选择时恢复白色背景 } ``` 3. **自定义高亮**: - 如果你想对特定条件下的节点进行高亮,可以在事件处理器添加判断。例如,你可以定义一个`customHighlightNode()`方法,检查节点的数据或属性,并应用相应的样式。 4. **动态更改**: - 如果树结构发生变化,可以遍历所有节点并更新其样式。例如,在`QTreeWidget::clear()`或`addTopLevelItem()`之后调用`update()`方法来刷新所有样式。 5. **使用CSS样式表**: - 如果想要更精细的控制,可以利用Qt的CSS样式表来定义节点的不同状态。将CSS规则绑定到特定的节点或状态。 记得要在适当的地方连接信号槽,以便在事件发生时触发相应的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值