从数组添加图标到QListWidget项

时间:2012-03-11 22:38:44

标签: c++ qt qt4 qt-creator qt4.8

我写了一个循环,在我的QListWidget中添加了一些项目。现在我想让他们从我的qrc文件中获得一个图标。为此,我将使用一个循环,在其中为每个项目添加一个唯一的图标。

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]);

接下来要清除List:

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

现在丑陋的部分:

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);

这是我的问题:

    ItemIcon.addFile(IconList[iRow], QSize(), QIcon::Normal, QIcon::Off);
    ui->listWidget_Available->item(iRow)->setIcon(ItemIcon);   <---  how to 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);
}
}

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您应该每次使用构造函数和新的QIcon而不是QIcon::addFile

QIcon ItemIcon(IconList[iRow]);
ui->listWidget_Available->item(iRow)->setIcon(ItemIcon);
只有当图标所在项目的每个状态都有不同的图像时,

addFile才会有用(正常,激活,禁用......)。