Add icon from array to QListWidget item

2019-08-10 15:45发布

I've written a loop which adds some items to my QListWidget. Now I want them to have an icon from my qrc file. For this I would use a loop in which I add a unique icon to each item.

void Test::Query()
{
  ui->listWidget_Available->clear();

  QString CmdList[4] = {"kcmshell4 --list|grep -q kcm_grub2",
                        "kcmshell4 --list|grep -q kcm_networkmanagement",
                        "which pastebunz",
                        "[ -z $ink3_ver ]"};
  QString NameList[4] = {"kcm_grub2",
                         "kcm_networkmanagement",
                         "pastebunz",
                         "Shellmenu"};
  QString IconList[4] = {":/icons/icons/GNU.png",
                         ":/icons/icons/networkmanager.png",
                         ":/icons/icons/edit-paste.png",
                         ":/icons/icons/menu.png"};

QIcon ItemIcon;
int iCntSize = sizeof(CmdList) / sizeof(CmdList[0]);

Next thing is to clear the List:

for(int iItem = 0; iItem < iCntSize; iItem++)
{
    ui->listWidget_Available->addItem(NameList[iItem]);
    ui->listWidget_Available->item(iItem)->setCheckState(Qt::PartiallyChecked);
}

Now the ugly part:

for(int iRow = 0; iRow < iCntSize; iRow++)
{
    int BarValue = ui->progressBar->value();
    ui->progressBar->setValue(BarValue+(100/iCntSize));

    QString status = QString("Processing index %1 of %2. Name: %3").arg(iRow).arg(iCntSize).arg(NameList[iRow]);
    qDebug() << status << (BarValue+25);

And here is my problem:

    ItemIcon.addFile(IconList[iRow], QSize(), QIcon::Normal, QIcon::Off);
    ui->listWidget_Available->item(iRow)->setIcon(ItemIcon);   <---  how to do THIS?

How can I do this?

    ui->textEdit_Informations->append("Searching for "+NameList[iRow]);
    mProcess = new QProcess();
    mProcess->start("/bin/bash", QStringList() << "-c" << QString(CmdList[iRow]));
    mProcess->waitForFinished();

    if ( mProcess->exitStatus() == QProcess::CrashExit )
    {
        ui->textEdit_Informations->setTextColor(Qt::red);
        ui->textEdit_Informations->append("\t[FATAL]");
    }
    else if ( mProcess->exitCode() != 0 )
    {

        ui->textEdit_Informations->setTextColor(Qt::magenta);
        ui->textEdit_Informations->append("\t[MISSED]");
        ui->listWidget_Available->item(iRow)->setCheckState(Qt::Unchecked);
    }
    else if ( mProcess->exitCode() == 0 )
    {
        ui->textEdit_Informations->setTextColor(Qt::green);
        ui->textEdit_Informations->append("\t[FOUND]");
        ui->listWidget_Available->item(iRow)->setCheckState(Qt::Checked);
    }
    ui->textEdit_Informations->setTextColor(Qt::white);
}
}

Any suggestions?

1条回答
smile是对你的礼貌
2楼-- · 2019-08-10 15:59

You should use the constructor and a new QIcon each time instead of QIcon::addFile:

QIcon ItemIcon(IconList[iRow]);
ui->listWidget_Available->item(iRow)->setIcon(ItemIcon);

addFile would only be useful if you had a different image for each state of the item the icon is in (normal, activated, disabled, ...).

查看更多
登录 后发表回答