Unsolved QTreeView and QListWidget, how to keep selection after lost focus?
-
@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?
-
-
@wqking
You can cheat a bit withQTimer
. -
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 -
@wqking said in QTreeView and QListWidget, how to keep selection after lost focus?:
QListWidget::item:selected:!active
thanks, it works
-
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( " " ) ); ...