I have a large log data (100, 1000, 100000, ... records) and I want to visualize it in the following manner:

Which widget (e.g. QListView, QListWidget) should I use and how, in order to stay away from performance and memory problems?

解决方案Is it possible to add a custom widget into a QListView?

Please, read about:

I want to show every log message in the above format

Solution

To achieve the desired result and stay away from performance issues, even with a very long data log, use a QListView with a custom delegate:

Create a subclass of QStyledItemDelegate, say Delegate

Reimplement the QStyledItemDelegate::paint method to do the custom drawing

Reimplement the QStyledItemDelegate::sizeHint to report the correct size of the items in the list

Use the custom delegate in the view by calling QAbstractItemView::setItemDelegate

Example

I have prepared a working example for you in order to demonstrate how the proposed solution could be implemented and used in an application.

The essential part of the example is the way the delegate paints the items in the list view:

void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,

const QModelIndex &index) const

{

QStyleOptionViewItem opt(option);

initStyleOption(&opt, index);

const QPalette &palette(opt.palette);

const QRect &rect(opt.rect);

const QRect &contentRect(rect.adjusted(m_ptr->margins.left(),

m_ptr->margins.top(),

-m_ptr->margins.right(),

-m_ptr->margins.bottom()));

const bool lastIndex = (index.model()->rowCount() - 1) == index.row();

const bool hasIcon = !opt.icon.isNull();

const int bottomEdge = rect.bottom();

QFont f(opt.font);

f.setPointSize(m_ptr->timestampFontPointSize(opt.font));

painter->save();

painter->setClipping(true);

painter->setClipRect(rect);

painter->setFont(opt.font);

// Draw background

painter->fillRect(rect, opt.state & QStyle::State_Selected ?

palette.highlight().color() :

palette.light().color());

// Draw bottom line

painter->setPen(lastIndex ? palette.dark().color()

: palette.mid().color());

painter->drawLine(lastIndex ? rect.left() : m_ptr->margins.left(),

bottomEdge, rect.right(), bottomEdge);

// Draw message icon

if (hasIcon)

painter->drawPixmap(contentRect.left(), contentRect.top(),

opt.icon.pixmap(m_ptr->iconSize));

// Draw timestamp

QRect timeStampRect(m_ptr->timestampBox(opt, index));

timeStampRect.moveTo(m_ptr->margins.left() + m_ptr->iconSize.width()

+ m_ptr->spacingHorizontal, contentRect.top());

painter->setFont(f);

painter->setPen(palette.text().color());

painter->drawText(timeStampRect, Qt::TextSingleLine,

index.data(Qt::UserRole).toString());

// Draw message text

QRect messageRect(m_ptr->messageBox(opt));

messageRect.moveTo(timeStampRect.left(), timeStampRect.bottom()

+ m_ptr->spacingVertical);

painter->setFont(opt.font);

painter->setPen(palette.windowText().color());

painter->drawText(messageRect, Qt::TextSingleLine, opt.text);

painter->restore();

}

The complete code of the example is available on GitHub.

Result

As written, the given example produces the following result:

qlistview 自定义控件_是否可以在QListView中添加自定义窗口小部件?相关推荐

  1. c语言context窗口,如何在gtk3中为窗口小部件设置特定的css类? (C)

    我正在尝试gtk3中的CSS,我不明白如何使用特定的类. C代码: provider = gtk_css_provider_new(); display = gdk_display_get_defau ...

  2. wordpress创建_十分钟即可在WordPress中创建动态小部件

    wordpress创建 Have you tried to find functionality for a specific purpose in WordPress but failed to f ...

  3. python entry如何清空_如何清除tkinter中来自Entry小部件的以前输入?

    当我在Entry小部件中输入输入值a并点击Click Me.该按钮调用takes_input()方法,该方法更新Text小部件.看起来是这样的:如何清除tkinter中来自Entry小部件的以前输入? ...

  4. python中label前景色的动态改变_在ttk / python中更改Label小部件的填充颜色

    我试图在python中使用ttk / tkinter显示图像.图像有一个白色边框,我想在更大的白色背景上显示这个图像 – 所以它周围有很多白色空间. 为此,我在标签中使用"padx" ...

  5. perl/tk_在Perl / Tk中使用高级小部件

    perl/tk 与Perl语言的广泛使用相比,Perl的GUI工具集Perl / Tk不太受欢迎. 这很奇怪,因为它可以说是最容易编程的GUI工具包之一(至少对于UNIX平台而言),因此建议将其自身用 ...

  6. python entry如何清空_Python tkinter,从类中清除Entry小部件

    这是我正在调用的类,并且是来自其他文件的函数 class CalcFunc: def clearScreen(self): self.log("CLEAR (CE)") ent.d ...

  7. wordpress移动_如何在移动设备上隐藏WordPress小部件(易于初学者使用)

    wordpress移动 Do you want to hide a specific WordPress widget for mobile users? 您要为移动用户隐藏特定的WordPress小 ...

  8. python中填充颜色结束的程序_在ttk/python中更改标签小部件的填充颜色

    我试图用python中的ttk/tkinter显示图像.图像有一个白色的边框,我想在一个更大的白色背景上显示这个图像,所以它周围有很多空白.在 为此,我在标签中使用"padx"和& ...

  9. python为text添加滚动条_在Tkinter中向一组小部件添加滚动条

    概述 您只能将滚动条与一些小部件关联起来,根小部件和Frame不是那组小部件的一部分. 最常见的解决方案是创建一个画布小部件,并将滚动条与该小部件关联起来.然后,将包含标签小部件的框架嵌入到画布中.确 ...

最新文章

  1. java 线程池 分组_JAVA面试题解惑系列(十)——话说多线程
  2. 自己动手构建iSCSI磁盘阵列
  3. [HAOI 2010]软件安装
  4. Codeforces 982 C. Cut 'em all! 图的遍历
  5. elementui 加载中_ElementUI cascader级联动态加载回显和搜索看这个就够了
  6. PL/SQL详细介绍
  7. php 迭代设计模式 -- BookList -- 分页
  8. 46 CO配置-控制-利润中心会计-创建虚拟利润中心
  9. 《linux核心应用命令速查》连载六:batch:在系统负载水平允许的时候执行命令
  10. 36.Linux驱动调试-根据oops定位错误代码行
  11. Bzoj2882 工艺
  12. Unix系统中常用的信号含义
  13. 网络聊天室的分析与实现
  14. 找软件测试工作自测,测试经验分享:做一个靠谱的软件测试人员
  15. Unity UGUI屏幕自适应
  16. Coding and Paper Letter(八十七)
  17. 本轮大宗商品涨价:(背后原因分析)2021-09
  18. 如何批量OCR识别各类票据关键信息,导出为结构化格式数据
  19. Java实现用汉明距离进行图片相似度检测的
  20. 移动端登录页面-vue

热门文章

  1. qtablewidget 选中不改变背景_C4D制作不一样的核壳结构
  2. matlab trendsurface,MATLAB 添加新的预测性维护产品
  3. C++ string字符串常见操作函数
  4. python ffmpy3与FFmpeg的安装
  5. C语言中的自定义类型
  6. MFC对话框绘制灰度直方图
  7. 模态窗口和非模态窗口
  8. PHP之MVC项目实战(二)
  9. Python 的协程库 greenlet 和 gevent
  10. C++学习之路 | PTA乙级—— 1068 万绿丛中一点红 (20 分)(精简)