Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    7
    16
    11429
    Loading More Posts
    • 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.
    • Ratzz
      Ratzz @wqking last edited by

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

      --Alles ist gut.

      W 1 Reply Last reply Reply Quote 0
      • W
        wqking @Ratzz last edited by 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.

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

        Ratzz 2 Replies Last reply Reply Quote 0
        • D
          Devopia53 last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • Ratzz
            Ratzz @wqking last edited by

            @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 Reply Quote 0
            • W
              wqking @Ratzz last edited by

              @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 Reply Quote 0
              • Ratzz
                Ratzz @wqking last edited by

                @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 Reply Quote 0
                • W
                  wqking @Ratzz last edited by

                  @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

                  Ratzz 1 Reply Last reply Reply Quote 0
                  • Ratzz
                    Ratzz @wqking last edited by

                    @wqking
                    You can cheat a bit with QTimer.

                    --Alles ist gut.

                    1 Reply Last reply Reply Quote 0
                    • R
                      Riki P @wqking last edited by

                      @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 Reply Quote 0
                      • W
                        wqking @Riki P last edited by

                        @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 Reply Quote 0
                        • kshegunov
                          kshegunov Moderators last edited by

                          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 Reply Quote 0
                          • W
                            wqking @Riki P last edited by

                            @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 Reply Quote 1
                            • R
                              Riki P @wqking last edited by

                              @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 Reply Quote 1
                              • J
                                jkkj @wqking last edited by

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

                                QListWidget::item:selected:!active

                                thanks, it works

                                Mike Finch 1 Reply Last reply Reply Quote 0
                                • Mike Finch
                                  Mike Finch @jkkj last edited by

                                  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 Reply Quote 1
                                  • First post
                                    Last post