将qss设置为QWidget时存在内存泄漏。代码:
QString checkBoxSelectionQSS = "QCheckBox, QCheckBox::indicator{ background-color: transparent; border: none; } QCheckBox::indicator:unchecked { image: url(:/resource/selectionIconUnchecked.png); } QCheckBox::indicator:checked { image: url(:/resource/selectionIconChecked.png); } QCheckBox::indicator { subcontrol-position: center center; }";
QString checkBoxVisibleQSS = "QCheckBox, QCheckBox::indicator{ background-color: transparent; border: none; } QCheckBox::indicator:unchecked { image: url(:/resource/visibleIconUnchecked.png); } QCheckBox::indicator:checked { image: url(:/resource/visibleIconChecked.png); } QCheckBox::indicator { subcontrol-position: center center; }";
QString pushButtonRemoveQSS = "QPushButton{ background-color: transparent; border: none; image: url(:/resource/removeIcon.png); }";
QCheckBox* pCheckBox = new QCheckBox;
pCheckBox->setChecked(true);
pCheckBox->setStyleSheet(checkBoxVisibleQSS);
pTree->setItemWidget(pItem, 1, pCheckBox);
connect(pCheckBox, SIGNAL(clicked(bool)), this, SLOT(SetVisibleItem(bool)));
pCheckBox = new QCheckBox;
pCheckBox->setChecked(true);
pCheckBox->setStyleSheet(checkBoxSelectionQSS);
pTree->setItemWidget(pItem, 2, pCheckBox);
connect(pCheckBox, SIGNAL(clicked(bool)), this, SLOT(SetSelectabelItem(bool)));
QPushButton* pPushButton = new QPushButton;
pPushButton->setStyleSheet(pushButtonRemoveQSS);
pTree->setItemWidget(pItem, 3, pPushButton);
connect(pPushButton, SIGNAL(pressed()), this, SLOT(RemoveItem()));
qss中指定的所有图像的大小均为7到50 KB,但每个小部件的内存大约为2 MB。如果对 setStyleSheet()发表评论,则不会发生泄漏。