我如何将 QTableWidgetItem 图标放在单元格的中心 [英] how do i place QTableWidgetItem Icon in center of cell

查看:37
本文介绍了我如何将 QTableWidgetItem 图标放在单元格的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个表格单元格只有一个没有任何文字的图标.

i want a table cell to have just an icon without any text.

我看到 QTableWidgetItem 类有一个对齐文本的方法 (int QTableWidgetItem::textAlignment () const)

i see the QTableWidgetItem class has a method to align the text (int QTableWidgetItem::textAlignment () const)

我找不到调整图标位置的方法(它似乎卡在左边——即使单元格中没有文本)

i find no way to adjust the placement of the icon (which seems to get stuck on the left -- even where there is no text in the cell)

查看状态和能量列.

推荐答案

您可以通过样式选项影响图标相对于文本的位置.

You can affect the position of the icon in relation to the text via the style options.

如果 QTableWidgetItem 是在没有任何文本的情况下构造的(通过不接受文本参数的构造函数),那么 Qt::DisplayRole 数据项不会被设置,文本不会被显示,也不会影响图标显示矩形.

If the QTableWidgetItem is constructed without any text (via the constructor that does not accept a text argument), then the Qt::DisplayRole data item is not set and the text will not be displayed nor will it affect the icons display rectangle.

通过继承 QTableWidget、覆盖 viewOptions 方法并设置视图选项的 decorationAlignment 字段,我能够影响 QTableWidgetItem 图标的位置,如下所示:

I was able to affect the position of the QTableWidgetItem's icon by subclassing the QTableWidget, overriding the viewOptions method and setting the decorationAlignment field of the view options, like this:

QStyleOptionViewItem MyTableWidget::viewOptions() const
{
    QStyleOptionViewItem option = QTableWidget::viewOptions();
    option.decorationAlignment = Qt::AlignHCenter | Qt::AlignCenter;
    option.decorationPosition = QStyleOptionViewItem::Top;
    ...
    return option;
}

这篇关于我如何将 QTableWidgetItem 图标放在单元格的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆