Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有样式表的Qt TableView_Qt_Tableview_Stylesheet - Fatal编程技术网

带有样式表的Qt TableView

带有样式表的Qt TableView,qt,tableview,stylesheet,Qt,Tableview,Stylesheet,在我的Qt项目中,在我的对话框中,我有表视图,我使用代理模型和表模型 从SQL设置其数据库。现在我想更改表视图列的颜色[仅列[3,4,5]。 搜索internet后,我终于成功更改了颜色,但更改了整个表视图的颜色。我使用以下代码: ui->tableView->setStyleSheet("background-color: yellow"); 我应该写些什么来变成黄色,只有一些列 我想我应该把“背景色”改成其他颜色,但我不太擅长CSS,我不知道要搜索什么。样式表不行,你需要通过编

在我的Qt项目中,在我的对话框中,我有表视图,我使用代理模型和表模型 从SQL设置其数据库。现在我想更改表视图列的颜色[仅列[3,4,5]。 搜索internet后,我终于成功更改了颜色,但更改了整个表视图的颜色。我使用以下代码:

ui->tableView->setStyleSheet("background-color: yellow");
我应该写些什么来变成黄色,只有一些列


我想我应该把“背景色”改成其他颜色,但我不太擅长CSS,我不知道要搜索什么。

样式表不行,你需要通过编程来完成! 请看以下示例,了解行的背景颜色:


样式表不行,您需要以编程方式完成! 请看以下示例,了解行的背景颜色:


您应该根据单元格而不是
QtableWidget
QtableModel
的功能来着色:

void MyParentWidget::highlightCell(const QModelIndex &cellIndex)
{
 for(int i=0; i<cellIndex.model()->columnCount(); i++)
 {
  for(int j=0; j<cellIndex.model()->rowCount(); j++)
  {
   if(i == cellIndex.column() && j == cellIndex.row())
   {
    ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(),     i)->setData(QBrush(Qt::yellow),
Qt::BackgroundRole);
   }
   else
   {
    ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(),  i)->setData(QBrush(Qt::white),
Qt::BackgroundRole);
   }
  }
 }
}
void MyParentWidget::highlightCell(常量QModelIndex和cellIndex)
{
对于(int i=0;icolumnCount();i++)
{
对于(int j=0;jrowCount();j++)
{
如果(i==cellIndex.column()&&j==cellIndex.row())
{
((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(),i)->setData(QBrush(Qt::yellow),
Qt:背景角色);
}
其他的
{
((QStandardItemModel*)cellIndex.model())->项(cellIndex.row(),i)->设置数据(QBrush(Qt::white),
Qt:背景角色);
}
}
}
}

您应该根据单元格而不是
QtableWidget
QtableModel
的功能来着色:

void MyParentWidget::highlightCell(const QModelIndex &cellIndex)
{
 for(int i=0; i<cellIndex.model()->columnCount(); i++)
 {
  for(int j=0; j<cellIndex.model()->rowCount(); j++)
  {
   if(i == cellIndex.column() && j == cellIndex.row())
   {
    ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(),     i)->setData(QBrush(Qt::yellow),
Qt::BackgroundRole);
   }
   else
   {
    ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(),  i)->setData(QBrush(Qt::white),
Qt::BackgroundRole);
   }
  }
 }
}
void MyParentWidget::highlightCell(常量QModelIndex和cellIndex)
{
对于(int i=0;icolumnCount();i++)
{
对于(int j=0;jrowCount();j++)
{
如果(i==cellIndex.column()&&j==cellIndex.row())
{
((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(),i)->setData(QBrush(Qt::yellow),
Qt:背景角色);
}
其他的
{
((QStandardItemModel*)cellIndex.model())->项(cellIndex.row(),i)->设置数据(QBrush(Qt::white),
Qt:背景角色);
}
}
}
}

感谢所有人的帮助,但没有任何效果,我尝试了你的建议。我甚至没有在intellisense中获得->项()函数。感谢所有人的帮助,但没有任何效果,我尝试了你的建议。我甚至没有在intellisense中获得->项()函数。