From ace0096d608d1ba2f1fcbea4a89eb9d67349f1a1 Mon Sep 17 00:00:00 2001 From: iaom <18504285112@163.com> Date: Tue, 27 Apr 2021 11:45:48 +0800 Subject: [PATCH] Adjust search-item-model. --- src/control/highlight-item-delegate.cpp | 4 ++-- src/control/search-list-view.cpp | 9 ++++---- src/model/search-item-model.cpp | 30 +++++++------------------ 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/control/highlight-item-delegate.cpp b/src/control/highlight-item-delegate.cpp index 473fe7e..a064443 100644 --- a/src/control/highlight-item-delegate.cpp +++ b/src/control/highlight-item-delegate.cpp @@ -54,7 +54,7 @@ void HighlightItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText)); QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4); - textRect.adjust(-20, -5, 0, 0); + textRect.adjust(0, -5, 0, 0); painter->save(); painter->translate(textRect.topLeft()); painter->setClipRect(textRect.translated(-textRect.topLeft())); @@ -83,7 +83,7 @@ QString HighlightItemDelegate::getHtmlText(QPainter *painter, const QStyleOption QString indexString = index.model()->data(index, Qt::DisplayRole).toString(); QFont ft(painter->font().family(), GlobalSettings::getInstance()->getValue(FONT_SIZE_KEY).toInt()); QFontMetrics fm(ft); - QString indexColString = fm.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号 + QString indexColString = fm.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() - 30); //当字体超过Item的长度时显示为省略号 // QFontMetrics m_QFontMetrics = painter->fontMetrics(); // QString indexColString = m_QFontMetrics.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号 QString htmlString; diff --git a/src/control/search-list-view.cpp b/src/control/search-list-view.cpp index feb193c..98f63ee 100644 --- a/src/control/search-list-view.cpp +++ b/src/control/search-list-view.cpp @@ -26,6 +26,7 @@ SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent) { // CustomStyle * style = new CustomStyle(GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString()); this->setStyle(CustomStyle::getStyle()); + setRootIsDecorated(false); this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setSelectionBehavior(QAbstractItemView::SelectRows); @@ -36,12 +37,12 @@ SearchListView::SearchListView(QWidget * parent, const QStringList& list, const m_model->setItem(m_item); this->setModel(m_model); this->setHeaderHidden(true); - this->setColumnWidth(0, 20); - this->setColumnWidth(1, 80); - this->setFixedHeight(list.count() * rowheight + 4); +// this->setColumnWidth(0, 20); +// this->setColumnWidth(1, 80); +// this->setFixedHeight(list.count() * rowheight + 4); this->setAttribute(Qt::WA_TranslucentBackground, true); this->setAutoFillBackground(false); - this->setStyleSheet("QWidget{background:transparent;}"); +// this->setStyleSheet("QWidget{background:transparent;}"); m_styleDelegate = new HighlightItemDelegate(this); // m_styleDelegate->setSearchKeyword(keyword); this->setItemDelegate(m_styleDelegate); diff --git a/src/model/search-item-model.cpp b/src/model/search-item-model.cpp index 548b56d..9cf560e 100644 --- a/src/model/search-item-model.cpp +++ b/src/model/search-item-model.cpp @@ -66,7 +66,7 @@ int SearchItemModel::rowCount(const QModelIndex& index) const { * @return model显示的列数 */ int SearchItemModel::columnCount(const QModelIndex& index) const { - return index.isValid() ? 0 : 2; + return index.isValid() ? 0 : 1; } /** @@ -90,30 +90,16 @@ int SearchItemModel::columnCount(const QModelIndex& index) const { QVariant SearchItemModel::data(const QModelIndex &index, int role) const { if(!index.isValid()) return QVariant(); - switch(index.column()) { - case Icon: { - switch(role) { - case Qt::DecorationRole: { - return m_item->getIcon(index.row()); - } - default: - return QVariant(); - } + switch(role) { + case Qt::DecorationRole: { + return m_item->getIcon(index.row()); } - case Name: { - switch(role) { - case Qt::DisplayRole: { - return QVariant(m_item->getName(index.row())); - } - // case Qt::ForegroundRole: { - // return QColor(50, 50, 50); - // } - default: - return QVariant(); - } + case Qt::DisplayRole: { + return QVariant(m_item->getName(index.row())); } + default: + return QVariant(); } - return QVariant(); }