c++ - 如何将图像和 QProgressBar 放入 QTableView 中?

标签 c++ qt

我正在开发某种下载管理器并在 QTableView 中显示文件名、大小和剩余字节数。现在我想用 QProgressBar 可视化进度并显示图像(以指示它是下载还是上传)。如何在 QTableView 的内部添加或显示 QProgressBar 和图像?

最佳答案

如果您使用的是 QTableView,我假设您使用的是链接到此 View 的模型。

一个解决方案是使用委托(delegate)(参见QItemDelegate)来绘制进度,在您必须定义的QItemDelegate::paint 方法中,使用QStyle 小部件 (widget->style()) 绘制进度(使用 QStyle::drawControlQStyle::CE_ProgressBarContents作为控件标识符)。

查看示例 Star Delegate 的文档,了解如何为您需要的列定义委托(delegate)。

后期编辑:定义delegate paint方法的例子(code sketch,未真正测试,以它为原理,未完全运行)。

void MyDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    QStyleOptionProgressBar progressStyle;
    progressStyle.rect = option.rect; // Maybe some other initialization from option would be needed

    // For the sake of the example, I assume that the index indicates the progress, and the next two siblings indicate the min and max of the progress.
    QModelIndex minIndex = index.sibling( index.row(), index.column() + 1);
    QModelIndex maxIndex = index.sibling( index.row(), index.column() + 2);

    progressStyle.minimum = qvariant_cast< int>( minIndex.data( Qt::UserRole));
    progressStyle.maximum = qvariant_cast< int>( maxIndex.data( Qt::UserRole));

    progressStyle.progress = qvariant_cast< int>( index.data( Qt::UserRole));
    progressStyle.textVisible = false;
    qApp->style()->drawControl( QStyle::CE_ProgressBarContents, progressStyleOption, painter);
}

关于c++ - 如何将图像和 QProgressBar 放入 QTableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3656306/

相关文章:

c++ - clang (MacOS 32/64) : disable stack frames in some functions

c++ - Qt - 强制关闭按钮点击(信号/插槽问题)

c++ - 在 qmake 期间检测修改的文件

c++ - 如何指定Qt插件构造函数?

c++ - 在 Visual Studio 2012 中调试 C DLL 文件时无法单步执行代码

c++ - move 代码与复制代码相同时的规则 5?

c++ - new char[n] 和 new (char[n]) 的区别

c++ - 从指向的对象本身有效地释放指针

c++ - 读取访问冲突第二次创建 QDomElement

c++ - 当我的 MainWindow 最小化时,Qt QDockWidget(floating) 最小化