QT-QlistWidget自定义item

1、将自定义的widget添加到QlistWidget中

QWidget *widget=new QWidget(this);
QWidget *mainArea = new QWidget(widget);
QWidget *fileinfoWidget = new QWidget(mainArea);

//显示区域的布局
QVBoxLayout *verLayout = new QVBoxLayout();
QHBoxLayout *horLayout = new QHBoxLayout();
QHBoxLayout *fileinfoLayout = new QHBoxLayout();

//主要控件
QLabel *idLabel = new QLabel(widget);
QLabel *textLabel = new QLabel(mainArea);
QLabel *speedLabel = new QLabel(mainArea);
QProgressBar *progressbar = new QProgressBar(mainArea);
QPushButton *giveupButton = new QPushButton(widget);
QLabel *tipLabel1 = new QLabel(widget);
QLabel *tipLabel2 = new QLabel(widget);
QLabel *filesizeLabel = new QLabel(widget);

//设置不同控件的样式
idLabel->setFixedSize(30, 30);
idLabel->setStyleSheet("background:" + currentColor + "; border-radius:15px;color:white;");
idLabel->setText(setFormat(id));
idLabel->setAlignment(Qt::AlignCenter);

giveupButton->setToolTip(QStringLiteral("取消下载"));
giveupButton->setFixedSize(24, 24);
QIcon icon3(qexeFullPath + "/../res/minus.png");
giveupButton->setIcon(icon3);
giveupButton->setIconSize(QSize(16, 16));

giveupButton->setProperty("index", QString::number(id - 1));

QFont tempFont(QStringLiteral("宋体"), 9, 0);
textLabel->setText(filename);
textLabel->setFont(tempFont);
textLabel->setFixedHeight(16);
progressbar->setValue(0);
progressbar->setFixedHeight(16);
progressbar->setFont(tempFont);
speedLabel->setFixedSize(150,16);
speedLabel->setAlignment(Qt::AlignRight); 
speedLabel->setFont(tempFont);

filesizeLabel->setText("0KB/0KB");
filesizeLabel->setFixedWidth(120);
filesizeLabel->setAlignment(Qt::AlignCenter);

tipLabel1->setText(QStringLiteral(" 未下载 "));
tipLabel1->setFixedWidth(80);
tipLabel1->setAlignment(Qt::AlignCenter);

tipLabel2->setFixedSize(28,28);

fileinfoLayout->setContentsMargins(0, 0, 0, 0);
fileinfoLayout->setSpacing(10);
fileinfoLayout->addWidget(textLabel);
fileinfoLayout->addWidget(speedLabel);
fileinfoWidget->setLayout(fileinfoLayout);

verLayout->setContentsMargins(0, 0, 0, 0);
verLayout->setMargin(0);
verLayout->setSpacing(5);
verLayout->addWidget(fileinfoWidget);
verLayout->addWidget(progressbar);
mainArea->setLayout(verLayout);

horLayout->setContentsMargins(0, 0, 0, 0);
horLayout->setSpacing(10);
horLayout->addWidget(idLabel);
horLayout->addWidget(mainArea);
horLayout->addWidget(filesizeLabel);
horLayout->addWidget(tipLabel1);
horLayout->addWidget(tipLabel2);
horLayout->addWidget(giveupButton);
widget->setLayout(horLayout);

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);
2、效果

QT-QlistWidget自定义item_第1张图片


你可能感兴趣的:(C++)