+ 3

QT table selection as column and cell

I have a qt table and i want to allow user to either select one cell or entire column. In other words, I need both the provisions i.e. to allow one cell modification or entire column modification. I am using API QAbstractItemView::setSelectionBehavior with QAbstractItemView::SelectItems or QAbstractItemView::SelectColumns. How to allow both these behaviours?

17th Sep 2024, 3:39 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
34 Respostas
17th Sep 2024, 4:58 PM
Bob_Li
Bob_Li - avatar
+ 3
can you set it up so that the select entire column is only triggered when the column header is clicked? This is how it's often done in spreadsheets. Also, if the entire column is selected, alt-click should deselect the clicked cell.
18th Sep 2024, 7:32 AM
Bob_Li
Bob_Li - avatar
+ 2
The issue is that `setSelectionBehavior` can only handle one mode at a time. You need custom logic to differentiate between cell and column selection.
18th Sep 2024, 7:41 AM
AstroSakura さくら 🌸
AstroSakura さくら 🌸 - avatar
+ 2
Ketan Lalcheta maybe add the select entire column to a custom right click menu?
18th Sep 2024, 10:46 AM
Bob_Li
Bob_Li - avatar
+ 2
Ketan Lalcheta I'm not sure, but wouldn't the right click be context sensitive? I imagine that the right click would depend on which cell or header you right clicked on, not on the entire table, so other columns should not be involved ..
18th Sep 2024, 1:14 PM
Bob_Li
Bob_Li - avatar
+ 2
Ketan Lalcheta Add a checkbox to the right-click menu for selecting an entire column. If the checkbox is checked, select the whole column; if unchecked, select individual cells.
18th Sep 2024, 1:21 PM
AstroSakura さくら 🌸
AstroSakura さくら 🌸 - avatar
+ 2
I think a global column or row selection for the entire table would be counter-productive and only adds to overall complexity. Wouldn't it lead to a confusing user experience? Its easier to place the cursor over the cell or header, then right click, select column or row, then do other operations, rather than checking and unchecking a global option whenever you want cell selection or row/column selection, and mistakenly selecting entire column or row when you just want to select a cell, because you forgot that the option is on. But this is just a personal preference. Maybe some people prefer one over the other.
18th Sep 2024, 1:22 PM
Bob_Li
Bob_Li - avatar
+ 2
Now This Code the provided code is for a Qt application and cannot be run in the Sololearn compiler, as Sololearn only supports standard C++ without GUI libraries : https://sololearn.com/compiler-playground/ccsOv9Q2Myv3/?ref=app
19th Sep 2024, 12:23 PM
AstroSakura さくら 🌸
AstroSakura さくら 🌸 - avatar
+ 2
Ketan Lalcheta No Problem Wait now I am going to update code I'll help you 🙂👍
19th Sep 2024, 12:32 PM
AstroSakura さくら 🌸
AstroSakura さくら 🌸 - avatar
+ 2
Thanks for your patience and support of you and Bob_Li
19th Sep 2024, 12:32 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
I had taken an overview of the same. It suggests how to stop selecting more than one column at a time. My concern is about option to select one cell or entire column both. Once I set option QAbstractItemView::SelectColumns through QAbstractItemView::setSelectionBehavior, any selection of cell results into entire column selection. I need to have an option to either continue editing a cell or choose to edit entire column.
17th Sep 2024, 6:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
what you are describing sounds like alt-clicking, or perhaps ctrl-clicking? or maybe shift-select? Not sure which, but holding down a modifier key while left clicking things... https://forum.qt.io/topic/46348/qt-creator-column-edit
17th Sep 2024, 11:38 PM
Bob_Li
Bob_Li - avatar
+ 1
Perfect...! I got fair idea that I can select multiple cells in a column by pressing alt key along with left mouse button. This is allowed when selection behavior is set as 'SelectItems'. This works ok for me in case of rows are very limit. I have 25 rows and would like to modify one column value for all rows. This forces me to select all rows one by one which is tedious process. So, I thought to set selection behavior as SelectColumns. This helps me select entire column in a single click. But with this option, I lost the provision to modify a single cell. How to achieve both i.e. single cell modification and option to modify an entire column without a need to select an individual all rows? Again, many thanks for your help on this topic.
18th Sep 2024, 7:10 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Bob_Li I have other functionality on header select. Still can I do this behaviour? Muhammad Nouman Ali can you please share a sample code link for me to start with? I understand there might not be an exact solution matching my requirement but just pointers will also be helpful
18th Sep 2024, 8:24 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan Lalcheta Sure I can help you 😊 code : Link to Code Playground: https://sololearn.com/compiler-playground/c2Nd4c521Vyo/?ref=app If you like and Rate My Code 🤗
18th Sep 2024, 8:55 AM
AstroSakura さくら 🌸
AstroSakura さくら 🌸 - avatar
+ 1
Thanks Muhammad Nouman Ali but on qt side. Sololearn does not support qt. Right?
18th Sep 2024, 8:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan Lalcheta Yes, Sololearn does not support Qt. Use Qt Creator or a local development environment for Qt.
18th Sep 2024, 9:00 AM
AstroSakura さくら 🌸
AstroSakura さくら 🌸 - avatar
+ 1
Okay Bob_Li . If I had a option on right click, then it should be a checkbox. Right? If checkbox is checked , then set selection behaviour as column or else let section behaviour is item. What if I need one column as entire selected but for other column as cell.
18th Sep 2024, 1:04 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
That's correct Bob_Li . Right click is defined on column and should not affect the other column. But behaviour is on table. Table object has provision to set it as item selectable or column selectable
18th Sep 2024, 1:18 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Bob_Li Yes, a global column or row selection can add complexity and confuse users. 😊
18th Sep 2024, 1:27 PM
AstroSakura さくら 🌸
AstroSakura さくら 🌸 - avatar