qlistview 自定义控件_是否可以在QListView中添加自定义窗口小部件?
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中添加自定义窗口小部件?相关推荐
- c语言context窗口,如何在gtk3中为窗口小部件设置特定的css类? (C)
我正在尝试gtk3中的CSS,我不明白如何使用特定的类. C代码: provider = gtk_css_provider_new(); display = gdk_display_get_defau ...
- wordpress创建_十分钟即可在WordPress中创建动态小部件
wordpress创建 Have you tried to find functionality for a specific purpose in WordPress but failed to f ...
- python entry如何清空_如何清除tkinter中来自Entry小部件的以前输入?
当我在Entry小部件中输入输入值a并点击Click Me.该按钮调用takes_input()方法,该方法更新Text小部件.看起来是这样的:如何清除tkinter中来自Entry小部件的以前输入? ...
- python中label前景色的动态改变_在ttk / python中更改Label小部件的填充颜色
我试图在python中使用ttk / tkinter显示图像.图像有一个白色边框,我想在更大的白色背景上显示这个图像 – 所以它周围有很多白色空间. 为此,我在标签中使用"padx" ...
- perl/tk_在Perl / Tk中使用高级小部件
perl/tk 与Perl语言的广泛使用相比,Perl的GUI工具集Perl / Tk不太受欢迎. 这很奇怪,因为它可以说是最容易编程的GUI工具包之一(至少对于UNIX平台而言),因此建议将其自身用 ...
- python entry如何清空_Python tkinter,从类中清除Entry小部件
这是我正在调用的类,并且是来自其他文件的函数 class CalcFunc: def clearScreen(self): self.log("CLEAR (CE)") ent.d ...
- wordpress移动_如何在移动设备上隐藏WordPress小部件(易于初学者使用)
wordpress移动 Do you want to hide a specific WordPress widget for mobile users? 您要为移动用户隐藏特定的WordPress小 ...
- python中填充颜色结束的程序_在ttk/python中更改标签小部件的填充颜色
我试图用python中的ttk/tkinter显示图像.图像有一个白色的边框,我想在一个更大的白色背景上显示这个图像,所以它周围有很多空白.在 为此,我在标签中使用"padx"和& ...
- python为text添加滚动条_在Tkinter中向一组小部件添加滚动条
概述 您只能将滚动条与一些小部件关联起来,根小部件和Frame不是那组小部件的一部分. 最常见的解决方案是创建一个画布小部件,并将滚动条与该小部件关联起来.然后,将包含标签小部件的框架嵌入到画布中.确 ...
最新文章
- java 线程池 分组_JAVA面试题解惑系列(十)——话说多线程
- 自己动手构建iSCSI磁盘阵列
- [HAOI 2010]软件安装
- Codeforces 982 C. Cut 'em all! 图的遍历
- elementui 加载中_ElementUI cascader级联动态加载回显和搜索看这个就够了
- PL/SQL详细介绍
- php 迭代设计模式 -- BookList -- 分页
- 46 CO配置-控制-利润中心会计-创建虚拟利润中心
- 《linux核心应用命令速查》连载六:batch:在系统负载水平允许的时候执行命令
- 36.Linux驱动调试-根据oops定位错误代码行
- Bzoj2882 工艺
- Unix系统中常用的信号含义
- 网络聊天室的分析与实现
- 找软件测试工作自测,测试经验分享:做一个靠谱的软件测试人员
- Unity UGUI屏幕自适应
- Coding and Paper Letter(八十七)
- 本轮大宗商品涨价:(背后原因分析)2021-09
- 如何批量OCR识别各类票据关键信息,导出为结构化格式数据
- Java实现用汉明距离进行图片相似度检测的
- 移动端登录页面-vue