1.失去焦点背景颜色,代码设置全选的时候,背景会是白色,需要设置失去焦点背景颜色。(设置焦点,会出现白转化成设置背景色,效果不好)
QPalette p;
p.setColor(QPalette::Inactive,QPalette::Highlight,QColor(51,153,255));
p.setColor(QPalette::Inactive,QPalette::HighlightedText,QColor(Qt::white));
p.setColor(QPalette::Inactive,QPalette::Text,QColor(Qt::white));
lwItems->setPalette(p); 去掉选择虚线框 MyListWidgetDelegate *listWidgetDelegate=new MyListWidgetDelegate(lwItems);
lwItems->setItemDelegate(listWidgetDelegate);
void DataDlg::createMiddleListView() { lwItems=new QListWidget(); lwItems->setSelectionMode(QAbstractItemView::MultiSelection); lwItems->setEditTriggers(QAbstractItemView::SelectedClicked); QPalette p; p.setColor(QPalette::Inactive,QPalette::Highlight,QColor(51,153,255)); p.setColor(QPalette::Inactive,QPalette::HighlightedText,QColor(Qt::white)); p.setColor(QPalette::Inactive,QPalette::Text,QColor(Qt::white)); lwItems->setPalette(p); lwItems->setIconSize(QSize(15,15)); lwItems->setObjectName("lwItems"); MyListWidgetDelegate *listWidgetDelegate=new MyListWidgetDelegate(lwItems); lwItems->setItemDelegate(listWidgetDelegate); addExportItems(); contentLayout->addWidget(lwItems); }
添加数据
void DataDlg::addExportItems() { isExportData=true; lwItems->clear(); btnImportExport->setText(exportText); for(int i=0;i<ExportFieldsCount;i++) { lwItems->addItem(ExportFields[i].fields); setListWidgetIcon(i,false); } }
选择触发事件
void DataDlg::listWidgetEvent(QListWidgetItem *clickedItem) { bool isSeleced=clickedItem->isSelected(); if(isSeleced==true) { clickedItem->setIcon(QIcon(":/res/icons/images/checkbox_checked.png")); } else { clickedItem->setIcon(QIcon(":/res/icons/images/checkbox_unchecked.png")); } }
设置全选或者取消全选
void DataDlg::onSelectAll() { isSelectAll=!isSelectAll; int itemCount=lwItems->count(); for(int i=0;i<itemCount;i++) { lwItems->item(i)->setSelected(isSelectAll); setListWidgetIcon(i,isSelectAll); } }
添加图标
void DataDlg::setListWidgetIcon(int row,bool isChecked) { if(isChecked) { lwItems->item(row)->setIcon(QIcon(":/res/icons/images/checkbox_checked.png")); } else { lwItems->item(row)->setIcon(QIcon(":/res/icons/images/checkbox_unchecked.png")); } }
取出选中的值
int selectedCount=lwItems->selectedItems().count(); if(selectedCount==0) { return; } QString selectedText=""; for(int i=0;i<selectedCount;i++) { selectedText=lwItems->selectedItems().at(i)->text(); }
2. 去掉选择虚线框
#ifndef MYLISTWIDGETDELEGATE_H #define MYLISTWIDGETDELEGATE_H #include <QObject> #include<QItemDelegate> #include<QPen> #include<QPainter> #include<QBrush> #include<QStyledItemDelegate> #include<QDebug> class MyListWidgetDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit MyListWidgetDelegate(QObject *parent = 0); protected: void paint(QPainter *painter,const QStyleOptionViewItem &option, const QModelIndex &index) const; private: signals: public slots: }; #endif // MYLISTWIDGETDELEGATE_H
#include "mylistwidgetdelegate.h" MyListWidgetDelegate::MyListWidgetDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void MyListWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem itemOption(option); if(itemOption.state & QStyle::State_HasFocus) { itemOption.state = itemOption.state ^ QStyle::State_HasFocus; } //调用默认委托 QStyledItemDelegate::paint(painter,itemOption,index); QPen pen; pen.setColor(QColor(255,255,255)); pen.setStyle(Qt::DotLine); painter->setPen(pen); painter->drawLine(itemOption.rect.bottomLeft(),itemOption.rect.bottomRight()); }
qss
QListWidget#lwItems{ color:#FFFFFF; font:16pt "DejaVu Sans"; background-color:#190101; margin:2px; padding-left:10px; padding-right:10px; border: 1px solid #32435E; } QListWidget#scaleItemsList,QListWidget#connectedIPList{ color:#FFFFFF; font:16pt "DejaVu Sans"; background-color:#190101; margin:20px; border: 1px solid #32435E; } QListWidget::item{ border:solid #656565; border-width:0px 0px 1px 0px; padding:10px 0px 10px 15px; margin:0px 5px 0px 5px; } QListWidget::item:selected { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6A848C, stop: 1.0 #0F9EAF); } QListWidget::item:selected:!active { border-width: 0px ; } QListWidget::item:selected:active { border-width: 0px; }
时间: 2024-10-17 17:59:38