改变qt中的单元格背景颜色

发布于 2024-09-01 00:07:52 字数 179 浏览 6 评论 0原文

我是 pyqt 的新手,我仍然面临一些新手问题:D
我有一个 QTableWidget,它是委托给 QChoice 控件的项目(希望我说得对) 每当用户更改选择控件选择时,我需要更改单元格背景颜色
简而言之:如何更改表格小部件中的单元格背景颜色?
我使用 pyqt4 和 python 2.6
提前致谢

i'm new to pyqt , and i'm still facing some newbie problems :D
i have a QTableWidget that is item delegated on a QChoice control ( hope i said it right )
i need to have the cell background color changes whenever a user change the choice control selection

briefly: how to change a cell background color in a table widget ??

i use pyqt4 and python 2.6

thanx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

沒落の蓅哖 2024-09-08 00:07:52

我使用了这样的东西:

brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
item.setBackground(brush)

Where item is QTableWidgetItem object

I used something like this:

brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
item.setBackground(brush)

Where item is QTableWidgetItem object

笑梦风尘 2024-09-08 00:07:52

使用

QTableWidgetItem QTableWidget.item(row, column)

and

QTableWidgetItem setData(role, data)

with

Qt.BackgroundRole

如下:

table.item(0, 0).setData(Qt.BackgroundRole, color).

并阅读 Qt 模型/视图中使用的角色机制。

Use

QTableWidgetItem QTableWidget.item(row, column)

and

QTableWidgetItem setData(role, data)

with

Qt.BackgroundRole

as follows:

table.item(0, 0).setData(Qt.BackgroundRole, color).

And read about the Roles mechanism used in Qt Model/View.

千秋岁 2024-09-08 00:07:52

如果你使用 QTableView 使用这个:

model.setData(model.index(0, 0), QVariant(QBrush(Qt::red)), Qt::BackgroundRole);

if you use QTableView use this:

model.setData(model.index(0, 0), QVariant(QBrush(Qt::red)), Qt::BackgroundRole);
帅冕 2024-09-08 00:07:52

以下是一些有用的代码行。抱歉冗余,我正在努力获得一些声誉。

QStandardItemModel* model = new QStandardItemModel(numRows, numColumns);
QStringList headers;
headers.append("Date");
model->setHorizontalHeaderLabels(headers);
QStandardItem* item = new QStandardItem(text);
item->setData(Qt::AlignCenter, Qt::TextAlignmentRole);
item->setData(QVariant(QBrush(Qt::green)), Qt::BackgroundRole);
model->setItem(row, column, item);

或者简单地:

item->setBackground(Qt::green);

Here are some useful lines of code. Sorry for redundancy, I'm trying to gain some reputation.

QStandardItemModel* model = new QStandardItemModel(numRows, numColumns);
QStringList headers;
headers.append("Date");
model->setHorizontalHeaderLabels(headers);
QStandardItem* item = new QStandardItem(text);
item->setData(Qt::AlignCenter, Qt::TextAlignmentRole);
item->setData(QVariant(QBrush(Qt::green)), Qt::BackgroundRole);
model->setItem(row, column, item);

or simply:

item->setBackground(Qt::green);
随波逐流 2024-09-08 00:07:52

嘿,您为表格小部件设置了委托方法。在委托的绘制事件中,您处理颜色变化技术。
看看这个示例,他们在这里完成了自定义选择颜色。与处理项目单元格绘画的方式相同

Hey, you set the delegate method for the table widget. in the paint event of the delegate you handle the color changing technique..
have a look at this example,here they have done custom selection color. same way you handle the item cell painting

停顿的约定 2024-09-08 00:07:52

对于C++方式的补充,如果你想绘制与Qt::red等不同的自定义颜色,你可以这样做:
ui->tableWidget->item(i, j)->setBackground(QColor(152,234,112));

For supplement in C++ way, if you want to paint custom color unlike Qt::red and so on, you can do something like :
ui->tableWidget->item(i, j)->setBackground(QColor(152,234,112));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文