日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/7/23 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中添加自定义窗口小部件?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。