c++ - 是否可以将自定义小部件添加到 QListView 中?

标签 c++ qt qlistview

我有一个大的日志数据(100、1000、100000,...记录),我想以下列方式对其进行可视化:

enter image description here
enter image description here
enter image description here

为了避免性能和内存问题,我应该使用哪个小部件(例如 QListViewQListWidget )以及如何使用?

最佳答案

Is it possible to add a custom widget into a QListView?


请阅读:
How to display a scrollable list with a substantial amount of widgets as items in a Qt C++ app?

I want to show every log message in the above format


解决方案
为了达到预期的结果并远离性能问题,即使数据日志很长,请使用 QListView 使用自定义委托(delegate):
  • 创建 QStyledItemDelegate 的子类,比如说Delegate
  • 重新实现 QStyledItemDelegate::paint 进行自定义绘图的方法
  • 重新实现 QStyledItemDelegate::sizeHint 报告列表中项目的正确大小
  • 通过调用 QAbstractItemView::setItemDelegate 在 View 中使用自定义委托(delegate)

  • 例子
    我为您准备了一个工作示例,以演示如何在应用程序中实现和使用建议的解决方案。
    该示例的基本部分是委托(delegate)在 ListView 中绘制项目的方式:
    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();
    }
    
    示例的完整代码可在 GitHub 上获得.
    结果
    如所写,给定的示例产生以下结果:
    Window with a message logger

    关于c++ - 是否可以将自定义小部件添加到 QListView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105343/

    相关文章:

    c++ - boolean 函数不能正常工作

    c++ - WDK 10 - Rpcrt4.dll - 系统错误 2

    命名空间中的 C++ 全局变量

    c++ - 如何编写 Qt HTTP GET 请求?

    c++ - 如何从 QListView 中搜索和选择项目?

    C++:带有 3 个星号的变量声明

    c++ - 如何使用 QStyle.drawControl() 呈现红色按钮?

    c++ - 在 QT Creator 中封装 UI 还是传递指向 UI 的指针更好?

    python - PYQT4, ListView : How to get selected rows using QStandardItemModel

    scroll - 在 QListView(或 QAbstractItemView)中平滑滚动