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(,,));
p.setColor(QPalette::Inactive,QPalette::HighlightedText,QColor(Qt::white));
p.setColor(QPalette::Inactive,QPalette::Text,QColor(Qt::white));
lwItems->setPalette(p);
lwItems->setIconSize(QSize(,));
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=;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=;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==)
{
return;
} QString selectedText="";
for(int i=;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 = ); 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(,,));
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:#;
margin:2px;
padding-left:10px;
padding-right:10px;
border: 1px solid #32435E; } QListWidget#scaleItemsList,QListWidget#connectedIPList{ color:#FFFFFF;
font:16pt "DejaVu Sans";
background-color:#;
margin:20px;
border: 1px solid #32435E; }
QListWidget::item{ border:solid #;
border-width:0px 0px 1px 0px;
padding:10px 0px 10px 15px;
margin:0px 5px 0px 5px; } QListWidget::item:selected { background: qlineargradient(x1: , y1: , x2: , y2: ,
stop: #6A848C, stop: 1.0 #0F9EAF); } QListWidget::item:selected:!active {
border-width: 0px ; }
QListWidget::item:selected:active {
border-width: 0px; }
05-11 15:23