【问题标题】:Cant set the background color of a QTableWidgetItem?无法设置 QTableWidgetItem 的背景颜色?
【发布时间】:2018-01-12 00:28:00
【问题描述】:

我正在尝试设置 QTableWidgetItem 的背景颜色,但它不起作用? 当我尝试更改文本颜色时,它可以正常工作..

代码:

void myWin::myFunction(int count, QVector<QColor> sampleInfoColor, QVector<QStringList> info)
{
    ...
    sampleTable->setRowCount(count + 1);
    QFont nameFont;
    nameFont.setBold(true);
    ...
    for(int i=0; i<count; i++)
    {
        ...
        QTableWidgetItem *name = new QTableWidgetItem("Text");
        name->setFont(nameFont);
        sampleTable->setItem(i+1, 0, name);
        name->setBackground(sampleInfoColor[i]);
        ...
    }
}

我也试过这个,但也没有用..:

sampleTable.item(nRow, nCol)->setBackground(QColor);

我做错了什么?

【问题讨论】:

  • 究竟是什么不工作?
  • QTableWidgetItem“名称”的背景颜色没有改变..
  • 当我对 sampleInfoColor[i] 进行 qDebug 时,我得到:QColor(ARGB 1, 0, 0, 1)。
  • 哦,伙计们,我发现了我的问题......在我的类的构造函数中,我已经为 QTableWidget 设置了样式表,没有它就可以完美运行......这很尴尬......

标签: qt qt5 qtablewidget qtablewidgetitem qcolor


【解决方案1】:

你可以试试这个:

name->setBackground( QBrush( sampleInfoColor[i] ) );

【讨论】:

  • 不起作用...也尝试过使用“sampleTable->item(row,col).setBackground(....);”,但它也不起作用...跨度>
猜你喜欢
  • 2012-06-04
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多