Qt:复选框和QItemDelegate-发生选择时不绘制复选框

发布于 2024-10-08 02:50:09 字数 597 浏览 7 评论 0原文

我使用 QItemDelegate 结合 QTableView 的 IsUserCheckable 标志来绘制居中的复选框列。在我启用表的行选择之前,一切都工作正常。

当进行选择时,蓝色选择背景是唯一绘制的内容,并且不再看到复选框。

以下是我用来从委托内绘制复选框的代码。

void CheckboxDelegate::drawCheck(QPainter* painter, QStyleOptionViewItem const& option, QRect const& rect, Qt::CheckState state) const
{
    QSize size = check(option, option.rect, Qt::Checked).size();
    QRect checkboxRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, size, option.rect);
    QItemDelegate::drawCheck(painter, option, checkboxRect, state);
}

关于为什么在做出选择时无法正确绘制的任何想法?

I am using a QItemDelegate combined with the QTableView's IsUserCheckable flag to draw a centered checkbox column. All has been working fine until I have enabled row selection for the table.

When selection occurs, the blue selection background is the only thing painted, and the checkboxes are no longer seen.

The following is my code I use to paint the checkbox from within the delegate.

void CheckboxDelegate::drawCheck(QPainter* painter, QStyleOptionViewItem const& option, QRect const& rect, Qt::CheckState state) const
{
    QSize size = check(option, option.rect, Qt::Checked).size();
    QRect checkboxRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, size, option.rect);
    QItemDelegate::drawCheck(painter, option, checkboxRect, state);
}

Any ideas as to why this is not painting correctly when a selection is made?

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

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

发布评论

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

评论(1

涙—继续流 2024-10-15 02:50:09

由于缺少发布的代码,很难确信我的答案会对您有所帮助,但我认为您的问题是画家不仅绘制 QRect,而且还绘制 QItemDelegate。这意味着,除非您明确告诉它您想要 QRect 的颜色是什么,否则它会将其绘制为与整个单元格相同的颜色。

如果这是真的,那么您的 QRect 仍然存在,但只是与单元格的其余内容的颜色相同。

您可以通过执行 painter.setPen(QColor.red); 更改 QRect 的画家的颜色。

同样,我没有太多代码可以使用,但如果您希望检查不同的颜色,你需要将其设置为不同的颜色。

如果您提供更多代码我可以更清楚地回答您。

It is difficult to be confident that my answer will help you because of the lack of code that is posted, but I think your problem is that the painter not only paints the QRect, but it also paints the QItemDelegate. This means that unless you specifically tell it what you want the color of the QRect to be, it will paint it to be the same color as the entire cell.

If this is true, then your QRect is still there, but is simply the same color as the rest of the contents of the cell.

You can change the color of the painter for the QRect by doing painter.setPen(QColor.red);

Again, I do not have much code to work off of, but if you want the check to be a different color, you need to set it to be a different color.

If you provide more code I could answer you more clearly.

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