Results 1 to 1 of 1

Thread: [QDataTable] Sorting a column not by its content but by what it displays

  1. #1
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Question [QDataTable] Sorting a column not by its content but by what it displays

    Hi all

    My class myTable inherits QDataTable and allows me to retrieve data from a MySql database.

    I am using a QSqlCursor on a table called "staff" : it is really effective, you can edit/delete/insert records easily.
    One column of myTable contains identifiers called "id_staff" that refer to another table called "personnel".
    In the method paintField(), I tell not to draw these "id_staff", but the corresponding names retrieved using "personnel".

    For instance, if I have tables "staff" and "personnel" like that :

    //staff//
    -----------------------------------
    || id_staff (int) ||
    -----------------------------------
    || 12 ||
    || 23 ||
    || 37 ||
    ------------------------------------

    //personnel//
    -----------------------------------------------------------------
    || id(int) || name (varchar) ||
    -----------------------------------------------------------------
    || 12 || Ringo ||
    || 23 || John ||
    || 37 || Paul ||
    ------------------------------------------------------------------

    myTable will display as following.
    (The default sorting of the items is defined by another column not mentioned here.)

    (myTable)
    Name
    --------------------------------------
    || Ringo ||
    || Paul ||
    || John ||


    If I click on the header of this column, it gets sorted using the id_staff.

    (myTable after header click)
    Name
    --------------------------------------
    || Ringo ||
    || John ||
    || Paul ||


    But I want to sort the column alphebetically, using the names displayed. It is more user-friendly.

    ( myTable after header click)
    Name
    --------------------------------------
    || John ||
    || Paul ||
    || Ringo ||


    How can I do that ?
    I have several ideas, but no one is efficient now :
    - QSqlCursor is ok when you use only one table of your database, but I did not succeed to subclass it and make it reference both tables "staff" and "personnel".
    - I intended to redefine the method that sort the columns or the one that compares the items of the QDataTable, but I did not find them.



    Qt Code:
    1. //***********************************************************************//
    2. myTable::myTable(...)
    3. {
    4. ...
    5. QSqlCursor* cursor = new QSqlCursor ("staff");
    6. setSqlCursor(cursor);
    7.  
    8. //I retrieve five columns, but this is the one I am interested in here
    9. addColumn("id_staff", "Name");
    10. .....
    11.  
    12. setSorting( true );
    13.  
    14. ....
    15. }
    16.  
    17. myTable::paintField(QPainter *p, const QSqlField *field,
    18. const QRect& cr, bool selected)
    19. {
    20. if( ! field )
    21. return;
    22.  
    23. ....
    24. if ( field->name() == "id_staff" )
    25. {
    26. int id = field->value().toInt();
    27.  
    28. //using the id_staff, I retrieve the name of the staff member in table "personnel"
    29. QString name = retrieveName(id);
    30.  
    31. p->drawText( 2, 2, cr.width() - 4, cr.height() - 4, Qt::AlignLeft, name);
    32. }
    33. }
    34. //***********************************************************************//
    To copy to clipboard, switch view to plain text mode 

    I hope this is clear and someone has ever been through this before.
    Thanks in advance
    Last edited by Charlie37; 5th November 2008 at 15:07.

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
  •  
Qt is a trademark of The Qt Company.