PyQt:Windows Vista/7上的QTableView中的QStyledItemDelegate?

2024-10-01 04:45:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在QTableView中获得一个单元格/行,如下所示:

enter image description here

运行Ubuntu时,QStyledItemDelegate同时适用于QTreeViewQTableView,但在Windows上,它只适用于QTreeView,而且只有在我不重新实现paint方法的情况下。在

所以我的两个问题是:

如何使QStyledItemDelegate看起来像QTableView中的上图?在

当重新实现paint时,如何使QStyledItemDelegate看起来像上图?在

文本旁边的图像是不需要的。我只是在寻找风格悬停和选择框。 为了以防万一,我的Qt版本是4.7.2。在


Tags: 方法图像文本版本风格ubuntuwindows情况
2条回答

我终于想够了。在

我没有解决的是,为什么不重写QStyledItemDelegate.paint()与重写它有不同的效果,比如:

def paint(self, painter, option, index):
    QStyledItemDelegate.paint(self, painter, option, index)

但这不是我问题的一部分。


我所解决的问题是如何在手工绘制时获得原始外观。以前,在绘制项目时,我使用: ^{pr2}$

有一个问题就是没有画出本土的焦点或选择。我看了QApplication.style().drawControl()的方法签名:

void QStyle::drawControl ( ControlElement element, const QStyleOption * option,
                           QPainter * painter, const QWidget * widget = 0 )

注意到widget参数,并尝试传入一个QTreeView。成功了。传递的QTreeView并不重要,但它使视图以本机方式呈现。在

因此,最终,呈现本机QTableView与调用以下命令一样简单:

QApplication.style().drawControl(QStyle.CE_ItemViewItem, option, painter, QTreeView())

QStyledItemDelegate的绘制方法中。在

我在qtcentre上打开了一个帖子,要求做同样的事情。回答我的人给我提供了一个C++代码示例和一个显示结果的图片。它似乎在C++中工作得很好,但在PySee中却不好(不知道PYQT),所以它可能是一个bug。在

工作代码如下:

void ProgressBarDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QStyledItemDelegate::paint(painter, option, index);

if( index.column() == 1 )
{
int progress = index.data().toInt();

QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.rect.setTop( option.rect.top() + 1 );
progressBarOption.rect.setHeight( option.rect.height() - 2 );
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.text = QString::number(progress) + "%";
progressBarOption.textVisible = true;
progressBarOption.textAlignment = Qt::AlignCenter;

QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
}
}

相关问题 更多 >