Results 1 to 3 of 3

Thread: Retrieving data from QTableWidget to create a nested list of like values

  1. #1
    Join Date
    Jul 2019
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Retrieving data from QTableWidget to create a nested list of like values

    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.

    TableEx.jpg

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Retrieving data from QTableWidget to create a nested list of like values

    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
    Qt Code:
    1. std::map< int, std::list< std:pair< int, int > > >
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. (groupMap[ groupNumber ]).push_back( std::make_pair( startIndex, endIndex ) );
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 26th July 2019 at 02:19.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2019
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Retrieving data from QTableWidget to create a nested list of like values

    This is perfect, Thanks!

Similar Threads

  1. Replies: 6
    Last Post: 21st November 2012, 19:54
  2. [SOLVED] Retrieving data from QTableWidget
    By timmu in forum Qt Programming
    Replies: 0
    Last Post: 7th October 2011, 12:36
  3. Replies: 0
    Last Post: 25th July 2011, 15:11
  4. Replies: 1
    Last Post: 23rd April 2011, 18:33
  5. Retrieving and converting values
    By jcoop in forum Newbie
    Replies: 3
    Last Post: 19th February 2009, 09:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.