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. QTreeView and QListWidget, how to keep selection after lost focus?
Forum Updated to NodeBB v4.3 + New Features

QTreeView and QListWidget, how to keep selection after lost focus?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 7 Posters 13.5k 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.
  • RatzzR Ratzz

    @wqking
    For QTreeView you can try this .
    ui->QTreeView->selectionModel()->select(child->index(),QItemSelectionModel::ClearAndSelect);

    W Offline
    W Offline
    wqking
    wrote on last edited by wqking
    #3

    @Ratzz said:

    @wqking
    For QTreeView you can try this .
    ui->QTreeView->selectionModel()->select(child->index(),QItemSelectionModel::ClearAndSelect);

    Thanks, but I don't think it's the solution.
    The selection is selected by the user, not by the code.
    What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white. What I want to do it the item keeps selected after the user clicks other place.

    My personal website https://www.kbasm.com/
    My Github https://github.com/wqking

    RatzzR 2 Replies Last reply
    0
    • D Offline
      D Offline
      Devopia53
      wrote on last edited by
      #4
      This post is deleted!
      1 Reply Last reply
      0
      • W wqking

        @Ratzz said:

        @wqking
        For QTreeView you can try this .
        ui->QTreeView->selectionModel()->select(child->index(),QItemSelectionModel::ClearAndSelect);

        Thanks, but I don't think it's the solution.
        The selection is selected by the user, not by the code.
        What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white. What I want to do it the item keeps selected after the user clicks other place.

        RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by
        #5

        @wqking said:

        What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white.

        So you want the background to be blue even if user clicks outside??
        What i feel is that it is not background change it is losing focus.

        --Alles ist gut.

        W 1 Reply Last reply
        0
        • RatzzR Ratzz

          @wqking said:

          What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white.

          So you want the background to be blue even if user clicks outside??
          What i feel is that it is not background change it is losing focus.

          W Offline
          W Offline
          wqking
          wrote on last edited by
          #6

          @Ratzz said:

          @wqking said:

          What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white.

          So you want the background to be blue even if user clicks outside??

          Exactly. The background remains blue after lost focus.

          I searched and found that we can re-set the selection (like your first reply) in FocusOut event, but it didn't work after I tried.

          My personal website https://www.kbasm.com/
          My Github https://github.com/wqking

          1 Reply Last reply
          0
          • W wqking

            @Ratzz said:

            @wqking
            For QTreeView you can try this .
            ui->QTreeView->selectionModel()->select(child->index(),QItemSelectionModel::ClearAndSelect);

            Thanks, but I don't think it's the solution.
            The selection is selected by the user, not by the code.
            What I want to do is, after the user select some item, the item background turns to light blue color, but in current version, after the user clicks any elsewhere, the selected item become unselected and the background turns to white. What I want to do it the item keeps selected after the user clicks other place.

            RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by
            #7

            @wqking said:

            but in current version, after the user clicks any elsewhere,

            Anywhere in the sense other widget in your main window or outside the treeview?

            --Alles ist gut.

            W 1 Reply Last reply
            0
            • RatzzR Ratzz

              @wqking said:

              but in current version, after the user clicks any elsewhere,

              Anywhere in the sense other widget in your main window or outside the treeview?

              W Offline
              W Offline
              wqking
              wrote on last edited by
              #8

              @Ratzz said:

              @wqking said:

              but in current version, after the user clicks any elsewhere,

              Anywhere in the sense other widget in your main window or outside the treeview?

              I want both.

              My personal website https://www.kbasm.com/
              My Github https://github.com/wqking

              RatzzR 1 Reply Last reply
              0
              • W wqking

                @Ratzz said:

                @wqking said:

                but in current version, after the user clicks any elsewhere,

                Anywhere in the sense other widget in your main window or outside the treeview?

                I want both.

                RatzzR Offline
                RatzzR Offline
                Ratzz
                wrote on last edited by
                #9

                @wqking
                You can cheat a bit with QTimer.

                --Alles ist gut.

                1 Reply Last reply
                0
                • W wqking

                  Hi,

                  I have a QTreeView and a QListWidget. Now after I select some rows in them, after they loses focus, the selection becomes invalid.
                  How to keep selection after they lose focus?
                  I didn't find anything from Qt document.

                  Thanks

                  R Offline
                  R Offline
                  Riki P
                  wrote on last edited by
                  #10

                  @wqking

                  Hi,

                  are You sure that the selection is lost when the view loses focus?
                  In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
                  If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

                  hope this helps
                  hi
                  riki

                  W 2 Replies Last reply
                  0
                  • R Riki P

                    @wqking

                    Hi,

                    are You sure that the selection is lost when the view loses focus?
                    In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
                    If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

                    hope this helps
                    hi
                    riki

                    W Offline
                    W Offline
                    wqking
                    wrote on last edited by
                    #11

                    @Riki-P said:

                    @wqking

                    Hi,

                    are You sure that the selection is lost when the view loses focus?
                    In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
                    If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

                    hope this helps
                    hi
                    riki

                    The selection is not lost after lost focus, but the color changes, same as what you said.

                    I will investigate in style sheet. But can you share how you did with style sheet? I have no any experience on Qt style sheet.

                    Thanks

                    My personal website https://www.kbasm.com/
                    My Github https://github.com/wqking

                    1 Reply Last reply
                    0
                    • kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #12

                      Out of the top of my head, you could use an QItemDelegate and provide your own background drawing for the given model index. I think it should work for your case.

                      Kind regards.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • R Riki P

                        @wqking

                        Hi,

                        are You sure that the selection is lost when the view loses focus?
                        In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
                        If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

                        hope this helps
                        hi
                        riki

                        W Offline
                        W Offline
                        wqking
                        wrote on last edited by
                        #13

                        @Riki-P said:

                        @wqking

                        Hi,

                        are You sure that the selection is lost when the view loses focus?
                        In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
                        If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

                        hope this helps
                        hi
                        riki

                        I think QListWidget::item:selected:!active in style sheet should do the work. But how to make it using the active color instead of a hard coded color?

                        My personal website https://www.kbasm.com/
                        My Github https://github.com/wqking

                        R J 2 Replies Last reply
                        1
                        • W wqking

                          @Riki-P said:

                          @wqking

                          Hi,

                          are You sure that the selection is lost when the view loses focus?
                          In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
                          If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

                          hope this helps
                          hi
                          riki

                          I think QListWidget::item:selected:!active in style sheet should do the work. But how to make it using the active color instead of a hard coded color?

                          R Offline
                          R Offline
                          Riki P
                          wrote on last edited by
                          #14

                          @wqking

                          Hi, I have a minimal qss file where I added the following lines:

                          QTableView {
                              selection-background-color: %TableViewInactiveHighLightedBackground%;
                              selection-color: %TableViewInactiveHighLightedText%;
                          }
                          QTableView:active {
                              selection-background-color: %TableViewHighLightedBackground%;
                              selection-color: %TableViewHighLightedText%;
                          }
                          

                          and on program startup i change the placeholders %....% with the actual values:

                          fileName = ":/qss/QssFileName.qss";
                          QFile qssFile(fileName);
                          QString qss;
                          
                          if (qssFile.open(QFile::ReadOnly))
                          {
                              QTextStream qssStream(&qssFile);
                              qss = qssStream.readAll();
                          
                              // customize the QSS file contents to use the correct values for the highlighted rows in table views
                              QPalette p = QApplication::palette("MainWindow");
                              qss.replace("%TableViewInactiveHighLightedBackground%", toQssFormat(p.color(QPalette::Active, QPalette::Highlight).lighter()));
                              qss.replace("%TableViewInactiveHighLightedText%",       toQssFormat(p.color(QPalette::Active, QPalette::HighlightedText).darker()));
                              qss.replace("%TableViewHighLightedBackground%",         toQssFormat(p.color(QPalette::Active, QPalette::Highlight)));
                              qss.replace("%TableViewHighLightedText%",               toQssFormat(p.color(QPalette::Active, QPalette::HighlightedText)));
                          }
                          

                          toQssFormat is just

                          QString toQssFormat(const QColor &color)
                          {
                              return QString("rgba(%1, %2, %3, %4)")
                                      .arg(color.red())
                                      .arg(color.green())
                                      .arg(color.blue())
                                      .arg(color.alpha());
                          }
                          

                          Hope this helps
                          bye
                          riki

                          1 Reply Last reply
                          1
                          • W wqking

                            @Riki-P said:

                            @wqking

                            Hi,

                            are You sure that the selection is lost when the view loses focus?
                            In my experience the problem is that (on Windows) the color of the selected items (for views without focus) is so similar to the color of the not selected items that it is very difficult to see any difference.
                            If this is the case I solved it by using qss style sheet (tried with setPalette() but the result was not completely satisfactory)

                            hope this helps
                            hi
                            riki

                            I think QListWidget::item:selected:!active in style sheet should do the work. But how to make it using the active color instead of a hard coded color?

                            J Offline
                            J Offline
                            jkkj
                            wrote on last edited by
                            #15

                            @wqking said in QTreeView and QListWidget, how to keep selection after lost focus?:

                            QListWidget::item:selected:!active

                            thanks, it works

                            Mike FinchM 1 Reply Last reply
                            0
                            • J jkkj

                              @wqking said in QTreeView and QListWidget, how to keep selection after lost focus?:

                              QListWidget::item:selected:!active

                              thanks, it works

                              Mike FinchM Offline
                              Mike FinchM Offline
                              Mike Finch
                              wrote on last edited by
                              #16

                              A combination of @wqking and @Riki-P suggestions worked for me. In my app, I have both a QTreeView and a QTableView. So, I did the following in my QMainWindow-derived class. Thanks!

                              MainWindow::MainWindow( QWidget * pParent, Qt::WFlags flags )
                              : QMainWindow( pParent, flags )
                              {
                                ...
                              
                                QStringList styles;
                              
                                // Force views to highlight the selected rows even when the view does not have focus.
                                QString fg = toQssFormat( palette().color( QPalette::Active, QPalette::HighlightedText ).lighter() );
                                QString bg = toQssFormat( palette().color( QPalette::Active, QPalette::Highlight ).lighter() );
                                styles << QString( "QTreeView:!active { selection-color: %1; selection-background-color: %2; }" )
                                  .arg( fg )
                                  .arg( bg );
                                styles << QString( "QTableView:!active { selection-color: %1; selection-background-color: %2; }" )
                                  .arg( fg )
                                  .arg( bg );
                              
                                setStyleSheet( styles.join( " " ) );
                              
                                ...
                              
                              1 Reply Last reply
                              1

                              • Login

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