Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidgetItem selection issue

QListWidgetItem selection issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 351 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    strukfit
    wrote on last edited by
    #1

    I am having a problem with QListWidgetItem selection. I set the margin for items in QListWidgets, but the selection area does not change.

    6cf9d365-4d0e-4445-8b76-dfd3949fef5c-image.png

    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);
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 2 Replies Last reply
      0
      • SGaistS SGaist

        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.

        S Offline
        S Offline
        strukfit
        wrote on last edited by
        #3

        @SGaist Got it, thanks

        1 Reply Last reply
        0
        • S strukfit has marked this topic as solved on
        • SGaistS SGaist

          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.

          S Offline
          S Offline
          strukfit
          wrote on last edited by
          #4

          @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?

          SGaistS 1 Reply Last reply
          0
          • S strukfit

            @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?

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @strukfit yes you can. QListWidget is just a QListView with a pre-built model.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved