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. Change Highlight item's color if a QListWidgetItem

Change Highlight item's color if a QListWidgetItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 6.0k 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.
  • Q Offline
    Q Offline
    qt.1234
    wrote on 28 Jan 2020, 02:18 last edited by
    #1

    Hi, I have created a QListWidget dynamically to populate menu items and allow user to select them and access different functions.

    The code is as follows:

    QListWidgetItem* homePage = new QListWidgetItem(tr("HOME"));
    QListWidgetItem* productPage = new QListWidgetItem(tr("PRODUCTS"));
    QListWidgetItem* locationPage = new QListWidgetItem(tr("LOCATIONS"));
    
    ui.listWidget->insertItem(0, homePage);
    ui.listWidget->insertItem(1, productPage);
    ui.listWidget->insertItem(2, locationPage);
    
    ui.listWidget->setCurrentRow(0);
    
    
    

    How do i go about changing the set current row highlighted color?? Please advise. Thank you.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Fuel 0
      wrote on 28 Jan 2020, 02:38 last edited by
      #2

      You can use QPalette or a Stylesheet for that. I would recommen a simple Stylesheet. Something like

      ui.listWidget->setStylesheet("QListWidget:item { selection-background-color: blue; }"
      

      I dont know if that Code works, but something like this. You can set the Stylesheet in the Form through Qt Creater as well.

      Q 1 Reply Last reply 28 Jan 2020, 02:58
      0
      • Q Offline
        Q Offline
        qt.1234
        wrote on 28 Jan 2020, 02:58 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • F Fuel 0
          28 Jan 2020, 02:38

          You can use QPalette or a Stylesheet for that. I would recommen a simple Stylesheet. Something like

          ui.listWidget->setStylesheet("QListWidget:item { selection-background-color: blue; }"
          

          I dont know if that Code works, but something like this. You can set the Stylesheet in the Form through Qt Creater as well.

          Q Offline
          Q Offline
          qt.1234
          wrote on 28 Jan 2020, 02:58 last edited by
          #4

          @Fuel-0

          Thank you. I have tried using stylesheet through Qt creator however it does not work.

          I use the following

          QListView::item:selected
          {
          background-color: rgb(255,0,0);
          }

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Fuel 0
            wrote on 28 Jan 2020, 03:06 last edited by
            #5

            Try QListWidget instead of QListView then

            Q 1 Reply Last reply 28 Jan 2020, 03:17
            0
            • F Fuel 0
              28 Jan 2020, 03:06

              Try QListWidget instead of QListView then

              Q Offline
              Q Offline
              qt.1234
              wrote on 28 Jan 2020, 03:17 last edited by
              #6

              @Fuel-0 it does not work even if i use QListWidget.

              M 1 Reply Last reply 30 Jan 2020, 08:10
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 28 Jan 2020, 22:00 last edited by
                #7

                Hi,

                Please take a look at the stylesheet reference for QListView. It shows how to customize several aspects of that widget.

                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
                3
                • Q qt.1234
                  28 Jan 2020, 03:17

                  @Fuel-0 it does not work even if i use QListWidget.

                  M Offline
                  M Offline
                  mr ZX
                  wrote on 30 Jan 2020, 08:10 last edited by mr ZX
                  #8

                  @qt-1234 said in Change Highlight item's color if a QListWidgetItem:

                  @Fuel-0 it does not work even if i use QListWidget.

                  maybe you set this style sheet in wrong widget.
                  try this:

                  ui->listWidget->setStyleSheet("QListView::item:selected{background-color: rgb(255,0,0);}");
                  

                  if not work again maybe your IDE is have a problem.
                  good luck

                  1 Reply Last reply
                  0

                  7/8

                  28 Jan 2020, 22:00

                  • Login

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