Adjust search-item-model.

This commit is contained in:
iaom 2021-04-27 11:45:48 +08:00
parent e1175bdec2
commit ace0096d60
3 changed files with 15 additions and 28 deletions

View File

@ -54,7 +54,7 @@ void HighlightItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem
ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText)); ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4); QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
textRect.adjust(-20, -5, 0, 0); textRect.adjust(0, -5, 0, 0);
painter->save(); painter->save();
painter->translate(textRect.topLeft()); painter->translate(textRect.topLeft());
painter->setClipRect(textRect.translated(-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(); QString indexString = index.model()->data(index, Qt::DisplayRole).toString();
QFont ft(painter->font().family(), GlobalSettings::getInstance()->getValue(FONT_SIZE_KEY).toInt()); QFont ft(painter->font().family(), GlobalSettings::getInstance()->getValue(FONT_SIZE_KEY).toInt());
QFontMetrics fm(ft); 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(); // QFontMetrics m_QFontMetrics = painter->fontMetrics();
// QString indexColString = m_QFontMetrics.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号 // QString indexColString = m_QFontMetrics.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号
QString htmlString; QString htmlString;

View File

@ -26,6 +26,7 @@
SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent) { SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent) {
// CustomStyle * style = new CustomStyle(GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString()); // CustomStyle * style = new CustomStyle(GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString());
this->setStyle(CustomStyle::getStyle()); this->setStyle(CustomStyle::getStyle());
setRootIsDecorated(false);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionBehavior(QAbstractItemView::SelectRows);
@ -36,12 +37,12 @@ SearchListView::SearchListView(QWidget * parent, const QStringList& list, const
m_model->setItem(m_item); m_model->setItem(m_item);
this->setModel(m_model); this->setModel(m_model);
this->setHeaderHidden(true); this->setHeaderHidden(true);
this->setColumnWidth(0, 20); // this->setColumnWidth(0, 20);
this->setColumnWidth(1, 80); // this->setColumnWidth(1, 80);
this->setFixedHeight(list.count() * rowheight + 4); // this->setFixedHeight(list.count() * rowheight + 4);
this->setAttribute(Qt::WA_TranslucentBackground, true); this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setAutoFillBackground(false); this->setAutoFillBackground(false);
this->setStyleSheet("QWidget{background:transparent;}"); // this->setStyleSheet("QWidget{background:transparent;}");
m_styleDelegate = new HighlightItemDelegate(this); m_styleDelegate = new HighlightItemDelegate(this);
// m_styleDelegate->setSearchKeyword(keyword); // m_styleDelegate->setSearchKeyword(keyword);
this->setItemDelegate(m_styleDelegate); this->setItemDelegate(m_styleDelegate);

View File

@ -66,7 +66,7 @@ int SearchItemModel::rowCount(const QModelIndex& index) const {
* @return model显示的列数 * @return model显示的列数
*/ */
int SearchItemModel::columnCount(const QModelIndex& index) const { 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 { QVariant SearchItemModel::data(const QModelIndex &index, int role) const {
if(!index.isValid()) if(!index.isValid())
return QVariant(); return QVariant();
switch(index.column()) { switch(role) {
case Icon: { case Qt::DecorationRole: {
switch(role) { return m_item->getIcon(index.row());
case Qt::DecorationRole: {
return m_item->getIcon(index.row());
}
default:
return QVariant();
}
} }
case Name: { case Qt::DisplayRole: {
switch(role) { return QVariant(m_item->getName(index.row()));
case Qt::DisplayRole: {
return QVariant(m_item->getName(index.row()));
}
// case Qt::ForegroundRole: {
// return QColor(50, 50, 50);
// }
default:
return QVariant();
}
} }
default:
return QVariant();
} }
return QVariant(); return QVariant();
} }