Issues with QListWidget

  • Hello, i'm trying to use QListWidget for a variety of purposes however it is difficult to work with, I'm having an issue where an uninstanced item is appearing in the list and an instanced one... simply isn't, its very odd and frustrating, please help.

    void UnitWindow::setupDataDisplay()
    tempRibbonAddress = new DataRibbon;
    lightRibbonAddress = new DataRibbon;
    moistureRibbonAddress = new DataRibbon;
    humidityRibbonAddress = new DataRibbon;

    QListWidgetItem* tempItemAddress = new QListWidgetItem(ui->DataList);
    ui->DataList->setItemWidget(tempItemAddress, tempRibbonAddress);
    ui->DataList->insertItem(0, tempItemAddress);
    tempRibbonAddress->setDataTitle("<font color = 'Red'> Temperature </font>");
    QListWidgetItem* lightItemAddress = new QListWidgetItem(ui->DataList);
    ui->DataList->setItemWidget(lightItemAddress, lightRibbonAddress);
    ui->DataList->insertItem(1, lightItemAddress);
    lightRibbonAddress->setDataTitle("<font color = 'Yellow'> Light </font>");
    QListWidgetItem* moistureItemAddress = new QListWidgetItem(ui->DataList);
    ui->DataList->setItemWidget(moistureItemAddress, moistureRibbonAddress);
    ui->DataList->insertItem(2, moistureItemAddress);
    moistureRibbonAddress->setDataTitle("<font color = 'Light Green'>Moisture</font>");
    QListWidgetItem* humidityItemAddress = new QListWidgetItem(ui->DataList);
    ui->DataList->setItemWidget(humidityItemAddress, humidityRibbonAddress);
    ui->DataList->insertItem(3, humidityItemAddress);
    lightRibbonAddress->setDataTitle("<font color = 'Light Blue'>Humidity</font>");


    Produces a list with temperature, humidity and moisture displayed as normal, on rows 0,1,2 (weird), light is not appearing and instead a blank version of the 'DataRibbon' class is appearing with 'TextLabel' in black in place of the Data Title.

  • DataRibbon* tempRibbonAddress;
    DataRibbon* lightRibbonAddress;
    DataRibbon* moistureRibbonAddress;
    DataRibbon* humidityRibbonAddress;

    Here is the header file declarations of the DataRibbon instances, it is a graphical widget with some QLabels

  • @Hubbard I think you might be adding the items twice. According to the docs that leads to undefined behaviour.
    The constructor of the QListWidgetItem adds it to its parent. Then it looks like you add it again with setItemWidget, or am I mistaken?
    Can you post your ui file too?

  • Nah it was because I set the DataTitle for light twice

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.