PDA

View Full Version : Retrieving data from QTableWidget to create a nested list of like values



trevor44
25th July 2019, 19:36
I'm currently using PYQT5 to create a simple GUI. I've created a qtablewidget using QTDesigner where the user can group people. The data is imported into the GUI with the Name, Start, End, and Class columns completed and locked for editing. The user enters numbers starting with "1" to group the people. Below is an example of the table.

13202

As the user groups the people I need to create a nested list, or list of lists. Each of the lower lists will contain the indexes for each person in the group. So in the example groupings = [[0,1],[2,3]].

Based on the pyqt documentation it looks like I should use the getitem() function but I'm unsure how exactly to construct the iteration to retrieve the indexes and group them.

Any suggestions would be greatly appreciated.

d_stranz
26th July 2019, 01:10
I'm not a python expert, but I think it has the concept of maps (possibly called "dictionaries"). The easiest way to do this is to create a map where the lookup key is the group number and the value is the list of indexes.

In C++, this would be represented as
std::map< int, std::list< std:pair< int, int > > > where the first "int" (the key) is the group number, and the two ints inside the pair are the start and end indexes. So this is basicially a lookup table where the key is the group number and the value is the list of start and end indexes.

You then basically read through the table and do the python equivalent of:


(groupMap[ groupNumber ]).push_back( std::make_pair( startIndex, endIndex ) );

You don't need to sort the table by group number; the map and its entries will be built in the order in which the rows are processed.

This map is equivalent to a list< int, list< pair< int, int > > >, where the implementation takes care of ensuring that there is only one entry for each unique key.

Be aware that in your proposed implementation as a list of lists, you lose the identification of which pair of indexes belongs to which person. After assembling your list, all you know is the pairs of indexes for the group as a whole.

trevor44
28th July 2019, 17:36
This is perfect, Thanks!