-1

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)

3
  • Please provide a valid minimal reproducible example (including the Card definitions). Besides, calling setSpacing() in the for loop is pointless: do it outside of the loop. Commented Oct 20, 2023 at 4:47
  • UPD: I've just created another list widget without changing anything and tried to add basic items through edit list widget in .ui file . And again same thing, once the number of items exceed the height of the list widget, items are simply not displayed. But when all items can be displayed within the list and there is no need for scrolling down, then everything works fine Commented Oct 20, 2023 at 5:05
  • Do not use generic properties in stylesheets for complex widgets, as they cause unexpected results, since those properties are propagated to all children (including index widgets of item views) and reset the appearance to the default (and ugly) QWindowsStyle. Always use proper selector types. Commented Oct 20, 2023 at 13:55

1 Answer 1

-1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's why providing a MRE is important. We cannot provide answers without a proper context, and your case clearly needed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.