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));
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;

View File

@ -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);

View File

@ -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();
}
}
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();
}
}
}
return QVariant();
}