QT Creator ListWidget does not display the items correctly

61 views Asked by At

I created a separate class Card, that is a widget. And in class main window I have a listWidget.I'm trying to add the card instances as items. Once the number of cards exceed the size of listWidget, instead of allowing me to scroll down and display all items, it doesn't display anything. But if the number of item allow all of them to fit into, everything is fine. Below is the code. Will appreciate any idea on what could be the problem.

 for(int i=0; i<6;++i) {
        auto item = new QListWidgetItem();
        auto widget = new Card(doctorName, doctorDepartment, doctorPicture);
        item->setSizeHint(widget->sizeHint());
        ui->listWidget->addItem(item);
        ui->listWidget->setItemWidget(item,widget);
    } 
ui->listWidget->setSpacing(5);

The card class:

Card::Card(QString DoctorN, QString DoctorD, QString DoctorP, QWidget *parent):
    QWidget(parent),
    ui(new Ui::Card)
{
    ui->setupUi(this);
    ui->labelBackground->setStyleSheet("image: url(:/backgrounds/Resources/Backgrounds/BCKGR_GreyCard.png);");
    DoctorName=DoctorN;
    DoctorDepartment=DoctorD;
    DoctorPicture=DoctorP;
    ui->labelDepartment->setText(DoctorDepartment);
    ui->labelDoctorName->setText(DoctorName);
    setDoctorPicture();
}

QSize Card::sizeHint() const {
return QSize(432, 114); 
}

The listWidget settings:

    ui->listWidget->setStyleSheet("background-color: rgba(255, 255, 255, 0);"
                                  "color: rgba(255, 255, 255, 0);"
                                  "border-color: rgba(255, 255, 255, 0);"
                                  "image: none;");
    ui->listWidget->setGeometry(390,150,501,561);

Nothing else really that has something to do with cards or list widget. Also I already used exactly same approach in another project and everything worked just as it was intended to. So I'm really confused with this situation. (if I set i<5, 5 items will be displayed since they can all fit into the listWidget, but once I set i<6 or any other bigger number, nothing is displayed)

1

There are 1 answers

1
Diana On

The solution was very unpredictable and I don't know what are the connections between these things, but it's like this: Initially I set image in the style sheet of the central widget in main window. Somehow this image didn't allow the scrollbar to appear in listWidget, so after deleting the image in central image, everything was displayed correctly.