QlistWidget自定义item

原文地址:https://blog.csdn.net/q5512049/article/details/47036349

原文地址: https://blog.csdn.net/weixin_33699280/article/details/81109406?utm_source=blogxgwz5

东西不能知道拿来用,但可以借鉴效果

简单版

    QListWidget *list=new QListWidget;
    QListWidgetItem *item=new QListWidgetItem(list,0); 
    
    item->setSizeHint(QSize(100,100));
    QWidget *w = new QWidget(list);
    QHBoxLayout *layout=new QHBoxLayout(w);
    QPushButton *pushButton=new QPushButton(w);
    QCheckBox *checkBox=new QCheckBox(w);
    layout->addWidget(checkBox);
    layout->addWidget(pushButton);
    w->setLayout(layout);
    w->show();
    list->setItemWidget(item,w);
    list->show();

复杂版

QWidget *widget = new QWidget(ui.listWidget);
 
//创建自定义的item
widget->setStyleSheet("background:transparent;");
 
QLabel *TypeLabel = new QLabel(widget);
QPixmap myPix(qexeFullPath + QStringLiteral("/../res/DataBase/Common/") + GetFileType(filename) + ".png");
TypeLabel->setFixedSize(62, 32);
TypeLabel->setPixmap(myPix);
TypeLabel->setScaledContents(true);
TypeLabel->setStyleSheet("QLabel{padding-left:15px;padding-right:15px;}");
 
QWidget *frontArea = new QWidget(widget);
frontArea->setFixedHeight(32);
 
QLabel *TextLabel = new QLabel(frontArea);
TextLabel->setText(filename);
 
QLabel *TotalCopyLabel = new QLabel(frontArea);
TotalCopyLabel->setText("0 KB/0 KB");
 
QWidget *backArea = new QWidget(widget);
backArea->setFixedSize(158, 32);
 
QProgressBar *ProgressBar = new QProgressBar(backArea);
ProgressBar->setTextVisible(false);
ProgressBar->setFixedHeight(12);
ProgressBar->setStyleSheet("QProgressBar{ border:none; background:rgb(230, 230, 230); border-radius:0px; text-align:center; color:gray }\
                        QProgressBar::chunk{ background:rgb(71, 137, 250); border-radius:0px; }");
 
QLabel *SpeedLabel = new QLabel(backArea);
SpeedLabel->setText("0 B/s");
 
QPushButton *OpenFolderButton = new QPushButton(widget);
OpenFolderButton->setToolTip(QStringLiteral("打开文件夹"));
OpenFolderButton->setFixedSize(82, 32);
QIcon icon3(qexeFullPath + QStringLiteral("/../res/DataBase/Common/文件夹.png"));
OpenFolderButton->setIcon(icon3);
OpenFolderButton->setIconSize(QSize(24, 24));
OpenFolderButton->setProperty("index", QString::number(currentIndex));
OpenFolderButton->setStyleSheet("QPushButton{ margin-left:25px;margin-right:25px;border:none; color:white; background:none; }QPushButton:hover{color:#FFFFFF; background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 255, 255, 0% ), stop:1 rgba(200, 200, 200, 60% )); }\
                                                            QPushButton:pressed{ color:white; background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 255, 255, 0% ), stop:1 rgba(200, 200, 200, 80% )); }");
connect(OpenFolderButton, SIGNAL(clicked()), this, SLOT(OpenFolder()));
 
QLabel *TipIconLabel = new QLabel(widget);
QPixmap myPix2(qexeFullPath + QStringLiteral("/../res/DataBase/Common/等待.png"));
TipIconLabel->setFixedSize(25, 20);
TipIconLabel->setPixmap(myPix2);
TipIconLabel->setScaledContents(true);
TipIconLabel->setStyleSheet("QLabel{padding-left:0px;padding-right:5px;}");
 
QLabel *TipTextLabel = new QLabel(widget);
TipTextLabel->setText(QStringLiteral("等待中"));
TipTextLabel->setStyleSheet("QLabel{padding-left:0px;padding-right:0px;}");
TipTextLabel->setFixedWidth(55);
 
QPushButton *CloseButton = new QPushButton(widget);
CloseButton->setToolTip(QStringLiteral("取消下载"));
CloseButton->setFixedSize(34, 24);
QIcon icon4(qexeFullPath + QStringLiteral("/../res/DataBase/Common/关闭.png"));
CloseButton->setIcon(icon4);
CloseButton->setIconSize(QSize(12, 12));
CloseButton->setProperty("index", QString::number(currentIndex));
CloseButton->setStyleSheet("QPushButton{ margin-left:0px;margin-right:10px;border:none; color:white; background:none; }QPushButton:hover{color:#FFFFFF; background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 255, 255, 0% ), stop:1 rgba(200, 200, 200, 60% )); }\
                        QPushButton:pressed{ color:white; background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 255, 255, 0% ), stop:1 rgba(200, 200, 200, 80% )); }");
connect(CloseButton, SIGNAL(clicked()), this, SLOT(HideItem()));
 
QVBoxLayout *verLayout = new QVBoxLayout;
verLayout->setContentsMargins(0, 0, 0, 0);
verLayout->setMargin(0);
verLayout->setSpacing(0);
verLayout->addWidget(TextLabel);
verLayout->addWidget(TotalCopyLabel);
frontArea->setLayout(verLayout);
 
QVBoxLayout *verLayout2 = new QVBoxLayout;
verLayout2->setContentsMargins(0, 0, 0, 0);
verLayout2->setMargin(0);
verLayout2->setSpacing(0);
verLayout2->addWidget(ProgressBar);
verLayout2->addWidget(SpeedLabel);
backArea->setLayout(verLayout2);
 
QHBoxLayout *horLayout = new QHBoxLayout;
horLayout->setContentsMargins(0, 0, 0, 0);
horLayout->setMargin(0);
horLayout->setSpacing(0);
horLayout->addWidget(TypeLabel);
horLayout->addWidget(frontArea);
horLayout->addWidget(backArea);
horLayout->addWidget(OpenFolderButton);
horLayout->addWidget(TipIconLabel);
horLayout->addWidget(TipTextLabel);
horLayout->addWidget(CloseButton);
widget->setLayout(horLayout);
 
//将widget作为列表的item
QListWidgetItem *ITEM = new QListWidgetItem();
QSize size = ITEM->sizeHint();
ITEM->setSizeHint(QSize(size.width(), 56));
ui.listWidget->addItem(ITEM);
widget->setSizeIncrement(size.width(), 56);
ui.listWidget->setItemWidget(ITEM, widget);

复杂版效果图:


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 13,868评论 12 197
  • 本系列出于AWeiLoveAndroid的分享,在此感谢,再结合自身经验查漏补缺,完善答案。以成系统。 Andro...
    济公大将阅读 5,824评论 2 10
  • 又是年底,无论大企业小企业都开始为年会忙碌! 传奇也算是资深年会策划和主持了,自打进入职场,年会的策划和主持工作就...
    傅传奇阅读 3,021评论 0 1
  • 我加入 “007”行动了。 看到“007”,你是不是马上就想到那部美国间谍大片《007》,想到那个长着迷人的眼睛,...
    俞燕文阅读 1,462评论 0 3
  • 今天下夜班早,回家以后急忙洗漱睡觉,因为还要辅导朵朵写作业,巩固一下他所学的知识,为明天考试做准备。订好了...
    王若涵妈妈阅读 988评论 0 0