QListWidgetItem selection issue
-
I am having a problem with QListWidgetItem selection. I set the margin for items in QListWidgets, but the selection area does not change.
As you can see on the screenshot, I moved the cursor to the side, but the selection works the same way as clicking on an item.
Complete function for UI:
void MainWindowUI::setupMainWindowUI(QMainWindow* MainWindowClass) { centralWidget = new QWidget(MainWindowClass); centralWidget->setStyleSheet("background-color: #272727;"); centralLayout = new QHBoxLayout(centralWidget); centralLayout->setContentsMargins(0, 0, 0, 0); centralLayout->setSpacing(0); functionSelectorWidget = new QWidget(centralWidget); functionSelectorWidget->setStyleSheet("background-color: white;"); functionSelectorWidget->setFixedWidth(48); alarmsWidget = new AlarmsWidget(centralWidget); alarmsListLayout = new QVBoxLayout(alarmsWidget); alarmsListLayout->setContentsMargins(0, 0, 0, 0); alarmsListWidget = new QListWidget(alarmsWidget); alarmsListWidget->setSelectionMode(QAbstractItemView::NoSelection); alarmsListWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); alarmsListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); alarmsListWidget->setStyleSheet(R"( QListWidget::item { background-color: #323232; margin: 12px 48px 12px 48px; } QListWidget::item::hover { background-color: white; border-radius: 5px; margin: 12px 48px 12px 48px; } )"); alarmsListLayout->addWidget(alarmsListWidget); alarmsManagerWidget = new QWidget(alarmsWidget); alarmsManagerWidget->setStyleSheet("background-color: #2C2C2C; border: 1px solid #404040; border-radius: 10px;"); QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect; shadowEffect->setBlurRadius(20); shadowEffect->setColor(QColor(0, 0, 0, 30)); shadowEffect->setOffset(3, 7); alarmsManagerWidget->setGraphicsEffect(shadowEffect); alarmsManagerWidget->setFixedSize(QSize(88, 48)); QObject::connect(alarmsWidget, &AlarmsWidget::resized, [&] { alarmsManagerWidget->move(alarmsWidget->geometry().width() - alarmsManagerWidget->width() - 24, alarmsWidget->geometry().height() - alarmsManagerWidget->height() - 20); }); alarmsManagerLayout = new QHBoxLayout(alarmsManagerWidget); alarmsManagerLayout->setContentsMargins(6, 0, 6, 0); deleteAlarmsButton = new IconPushButton(alarmsManagerWidget, "", "transparent", "transparent", "Resources/pen-white.svg", "Resources/pen-grey.svg", "transparent", "#383838", "#343434"); deleteAlarmsButton->setFixedSize(QSize(36, 36)); deleteAlarmsButton->setIconSize(QSize(20, 20)); confirmButton = new IconPushButton(alarmsManagerWidget, "", "transparent", "transparent", "Resources/confirm-white.svg", "Resources/confirm-grey.svg", "transparent", "#383838", "#343434"); confirmButton->setFixedSize(QSize(36, 36)); confirmButton->setIconSize(QSize(16, 16)); addAlarmButton = new IconPushButton(alarmsManagerWidget, "", "transparent", "transparent", "Resources/add-white.svg", "Resources/add-grey.svg", "transparent", "#383838", "#343434"); addAlarmButton->setFixedSize(QSize(36, 36)); addAlarmButton->setIconSize(QSize(20, 20)); alarmsManagerLayout->addWidget(deleteAlarmsButton); alarmsManagerLayout->addWidget(confirmButton); confirmButton->hide(); alarmsManagerLayout->addWidget(addAlarmButton); centralLayout->addWidget(functionSelectorWidget); centralLayout->addWidget(alarmsWidget); MainWindowClass->setLayout(centralLayout); MainWindowClass->setCentralWidget(centralWidget); QMetaObject::connectSlotsByName(MainWindowClass); }
Here you can see that I initialize the QListWidget and set the css style:
alarmsListWidget = new QListWidget(alarmsWidget); alarmsListWidget->setSelectionMode(QAbstractItemView::NoSelection); alarmsListWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); alarmsListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); alarmsListWidget->setStyleSheet(R"( QListWidget::item { background-color: #323232; margin: 12px 48px 12px 48px; } QListWidget::item::hover { background-color: white; border-radius: 5px; } )"); alarmsListLayout->addWidget(alarmsListWidget);
Function in which i add a new items(if needed):
void MainWindow::setAlarm(const int& id, const QString& name, const QTime& time) { AlarmClockWidget* alarm = new AlarmClockWidget(this, id, time, name); QListWidgetItem* item = new QListWidgetItem(ui->alarmsListWidget); item->setSizeHint(QSize(733, 200)); ui->alarmsListWidget->setItemWidget(item, alarm); QTimer::singleShot(1000, this, &MainWindow::checkAlarm); }
-
Hi and welcome to devnet,
setItemWidget is meant to show static content, it does not make widgets put in that cell behave as an item.
If you want that, you should create a custom QStyledItemDelegate.
-
Hi and welcome to devnet,
setItemWidget is meant to show static content, it does not make widgets put in that cell behave as an item.
If you want that, you should create a custom QStyledItemDelegate.
-
-
Hi and welcome to devnet,
setItemWidget is meant to show static content, it does not make widgets put in that cell behave as an item.
If you want that, you should create a custom QStyledItemDelegate.
-
@SGaist Hi again. Could you also tell me if i can use QStyledItemDelegate with QListWidget in my situation or do i need to use something like QTreeView?