QListWidget 自定义item

效果图如下:
QListWidget 自定义item_第1张图片
QListWidget 自定义item_第2张图片

//.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

#define BACKGROUND_ICON() QString("background-image:url(:/resources/%1);background-attachment:fixed;background-position:center;background-repeat:no-repeat;")


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("QListWidget");

    ui->listWidget->setObjectName("ListWidget");
    ui->listWidget->setStyleSheet("#ListWidget{background-color:#424655}");
    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setDragEnabled(false);
    ui->listWidget->setWordWrap(true);
    ui->listWidget->setWrapping(false);
    ui->listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
    ui->listWidget->setFrameShape(QListWidget::NoFrame);
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setFocusPolicy(Qt::NoFocus);
    ui->listWidget->setViewMode(QListWidget::ListMode);
    ui->listWidget->setFlow(QListWidget::TopToBottom);
    QScroller::grabGesture(this, QScroller::TouchGesture);
    connect(ui->listWidget, &QListWidget::itemClicked, this, &MainWindow::onItemClicked);
    /*
    //关闭越区
    QScroller* scroller = QScroller::scroller(ui->listWidget);
    QScrollerProperties properties = scroller->scrollerProperties();
    properties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);//关闭越区
    scroller->setScrollerProperties(properties);
    connect(scroller, &QScroller::stateChanged, this, &ListWidget::stateChanged);
    */

    listWidgetAddItem(BACKGROUND_ICON().arg("displaySetting.png"), "aaa");
    listWidgetAddItem(BACKGROUND_ICON().arg("standby.png"), "bbb");
    listWidgetAddItem(BACKGROUND_ICON().arg("update.png"), "ccc");
    listWidgetAddItem(BACKGROUND_ICON().arg("displaySetting.png"), "111");
    listWidgetAddItem(BACKGROUND_ICON().arg("standby.png"), "222");
    listWidgetAddItem(BACKGROUND_ICON().arg("update.png"), "333");
    listWidgetAddItem(BACKGROUND_ICON().arg("displaySetting.png"), "444");
    listWidgetAddItem(BACKGROUND_ICON().arg("standby.png"), "555");
    listWidgetAddItem(BACKGROUND_ICON().arg("update.png"), "666");
    listWidgetAddHeadItem(BACKGROUND_ICON().arg("update.png"), "啊啊啊");
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::listWidgetAddItem(QString icon, QString data)
{
    QListWidgetItem *item = new QListWidgetItem(ui->listWidget, 0);
    item->setData(Qt::UserRole, data);
    item->setSizeHint(QSize(ui->listWidget->width(), 57));
    QSize size = item->sizeHint();

    QWidget *widget = new QWidget(ui->listWidget);
    widget->setFixedSize(size);

    QLabel *icon_label = new QLabel(widget);
    icon_label->setFixedSize(36, 36);
    icon_label->setStyleSheet(icon);

    QLabel *text_label = new QLabel(widget);
    text_label->setText(data);
    text_label->setFixedSize(size.width()-36-40, size.height()-1);
    text_label->setStyleSheet("QLabel{color:#fff;font-size:30px;}");
    text_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    QLabel *icon1_label = new QLabel(widget);
    icon1_label->setFixedSize(40, 40);
    icon1_label->setStyleSheet("border-image:url(:/resources/trunRight.png)");

    QHBoxLayout *Hboxlayout = new QHBoxLayout();
    Hboxlayout->setMargin(0);
    Hboxlayout->setSpacing(0);
    Hboxlayout->addWidget(icon_label,0,Qt::AlignCenter);
    Hboxlayout->addSpacing(2);
    Hboxlayout->addWidget(text_label,0,Qt::AlignCenter);
    Hboxlayout->addWidget(icon1_label,0,Qt::AlignCenter);

    QLabel *line = new QLabel();
    line->setFixedSize(size.width()-15, 1);
    line->setStyleSheet("background-color:#6E7489;");

    QVBoxLayout *Vboxlayout = new QVBoxLayout(widget);
    Vboxlayout->setMargin(0);
    Vboxlayout->setSpacing(0);
    Vboxlayout->addLayout(Hboxlayout);
    Vboxlayout->addStretch(1);
    Vboxlayout->addWidget(line);

    ui->listWidget->setItemWidget(item, widget);
}

void MainWindow::listWidgetAddHeadItem(QString icon, QString data)
{
    QListWidgetItem *item = new QListWidgetItem();
    item->setData(Qt::UserRole, data);
    item->setSizeHint(QSize(ui->listWidget->width(), 57));
    QSize size = item->sizeHint();

    QWidget *widget = new QWidget(ui->listWidget);
    widget->setFixedSize(size);

    QLabel *icon_label = new QLabel(widget);
    icon_label->setFixedSize(36, 36);
    icon_label->setStyleSheet(icon);

    QLabel *text_label = new QLabel(widget);
    text_label->setText(data);
    text_label->setFixedSize(size.width()-36-40, size.height()-1);
    text_label->setStyleSheet("QLabel{color:#fff;font-size:30px;}");
    text_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    QLabel *icon1_label = new QLabel(widget);
    icon1_label->setFixedSize(40, 40);
    icon1_label->setStyleSheet("border-image:url(:/resources/trunRight.png)");

    QHBoxLayout *Hboxlayout = new QHBoxLayout();
    Hboxlayout->setMargin(0);
    Hboxlayout->setSpacing(0);
    Hboxlayout->addWidget(icon_label,0,Qt::AlignCenter);
    Hboxlayout->addSpacing(2);
    Hboxlayout->addWidget(text_label,0,Qt::AlignCenter);
    Hboxlayout->addWidget(icon1_label,0,Qt::AlignCenter);

    QLabel *line = new QLabel();
    line->setFixedSize(size.width()-15, 1);
    line->setStyleSheet("background-color:#6E7489;");

    QVBoxLayout *Vboxlayout = new QVBoxLayout(widget);
    Vboxlayout->setMargin(0);
    Vboxlayout->setSpacing(0);
    Vboxlayout->addLayout(Hboxlayout);
    Vboxlayout->addStretch(1);
    Vboxlayout->addWidget(line);

    ui->listWidget->insertItem(0, item);
    ui->listWidget->setItemWidget(item, widget);
}

void MainWindow::onItemClicked(QListWidgetItem *item)
{
    if(item)
        WELOG_DEBUG() << item->data(Qt::UserRole).toString();
}
//.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

namespace Ui {
class MainWindow;
}


#define WELOG_FILE_LINE "[" << __FUNCTION__ << "][" << __LINE__ << "]: "
#define WELOG_DEBUG() qDebug().nospace() << WELOG_FILE_LINE


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void listWidgetAddItem(QString icon, QString data);
    void listWidgetAddHeadItem(QString icon, QString data);
    
private:
    Ui::MainWindow *ui;

public slots:
	void onItemClicked(QListWidgetItem *item);
};
#endif // MAINWINDOW_H

你可能感兴趣的:(Qt,qt,QListWidget)