QTreeView and QListWidget, how to keep selection after lost focus?
-
@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. -
@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. -
@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.
-
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 -
@Riki-P said:
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
rikiThe 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
-
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.
-
@Riki-P said:
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
rikiI 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?
-
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 -
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( " " ) ); ...