开发者

how can i paint row with different color in QTreeWidget (Qt)

开发者 https://www.devze.com 2023-01-21 05:35 出处:网络
i have in my application that 开发者_JAVA技巧when i click on row i have the default blue row marked

i have in my application that 开发者_JAVA技巧when i click on row i have the default blue row marked but beside this blue color i what to paint the row in different color via function not depending on user clicked signal , in sort i need a function that i will pass the row index and it will paint the row in x color.


create a delegate function with an item role. set the item role in each tree item. Access the item in the paint event of tree delegate and paint the row accordingly.


For QStandardItem you can set the back ground role to your desired color.

void customSelect(const QModelIndex &index)
{
  if( !index.isValid() )
     return;


  QStandardItemModel* myModel = dynamic_cast<QStandardItemModel*> ( index.model() );
  if( !myModel )
    return;

  int row = index.row();
  for( int col = 0 , colCount = myModel.columnCount(); col < colCount ; ++i)
  {
    QStandardItem *item = myModel.item( row, col );
    item->setData(Qt::blue, Qt::BackgroundColorRole);
  }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消