Results 1 to 6 of 6

Thread: Sort QTableView Columns with drag and drop

  1. #1
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Sort QTableView Columns with drag and drop

    I'm trying to set Drag and Drog columns to my QTabeView, but it's a bit frustrating.
    I've been googling about 2 days, and tryng a lot of things:
    -Setted the table to DragDrop enabled + internal move via Qt Designer, via code, enabled QHeaderView::setSectionsMovable(), etc.. but nothing works.
    I think that the problem is that i reimplemented data an HeaderData in my custom QAbstractTableModel:

    Qt Code:
    1. QVariant rs_GameModel::headerData(int section, Qt::Orientation orientation, int role) const {
    2. if (role != Qt::DisplayRole)
    3. return QVariant();
    4. if (role == Qt::DisplayRole) {
    5. if (section == cols.at(0)) {
    6. return tr("Juego");
    7. } else if (section == cols.at(1)) {
    8. return tr("Tipo");
    9. } else if (section == cols.at(2)) {
    10. return tr("Valoracion");
    11. } else if (section == cols.at(3)) {
    12. return tr("Emulador");
    13. } else if (section == cols.at(4)) {
    14. return tr("Usos");
    15. } else if (section == cols.at(5)) {
    16. return tr("Genero");
    17. } else if (section == cols.at(6)) {
    18. return tr("Idioma");
    19. } else if (section == cols.at(7)) {
    20. return tr("Año");
    21. } else if (section == cols.at(8)) {
    22. return tr("Compañia");
    23. } else if (section == cols.at(9)) {
    24. return tr("Jugadores");
    25. } else if (section == cols.at(10)) {
    26. return tr("Ruta");
    27. }
    28. }
    29. return QVariant();
    30. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QVariant rs_GameModel::data(const QModelIndex &index, int role) const {
    2. if (!index.isValid()) {
    3. return QVariant();
    4. }
    5. if (index.row() >= romCount || index.row() < 0 ) {
    6. return QVariant();
    7. }
    8.  
    9. if (role == Qt::TextAlignmentRole) {
    10. if (index.column() > 0 && index.column() < 10) {
    11. return Qt::AlignHCenter;
    12. }
    13.  
    14. }
    15. if (role == Qt::UserRole) {
    16. if (index.column() == cols.at(0)) {
    17. return ROMs.at(index.row()).section(";", 11, 11);
    18. }
    19. }
    20. if (role == Qt::DecorationRole) {
    21. if (index.column() == cols.at(0)) {
    22. return QIcon(QString("Skin/%1/icons/sys/rom16.png").arg(CurrSkin));
    23. }
    24. if (index.column() == cols.at(1)) {
    25. QString tmpExt = ROMs.at(index.row()).section(";", 1, 1);
    26.  
    27. if (tmpExt.compare("zip", Qt::CaseInsensitive) == 0 || tmpExt.compare("7Z", Qt::CaseInsensitive) == 0 || tmpExt.compare("r", Qt::CaseInsensitive) == 0 ) {
    28. return QIcon(QString("Skin/%1/icons/sys/compressed16.png").arg(CurrSkin));
    29. } else if (tmpExt.compare("iso", Qt::CaseInsensitive) == 0 || tmpExt.compare("cue", Qt::CaseInsensitive) == 0 || tmpExt.compare("bin", Qt::CaseInsensitive) == 0 ||
    30. tmpExt.compare("nrg", Qt::CaseInsensitive) == 0 || tmpExt.compare("mdf", Qt::CaseInsensitive) == 0 || tmpExt.compare("mds", Qt::CaseInsensitive) == 0 ) {
    31. return QIcon(QString("Skin/%1/icons/sys/cddvd16.png").arg(CurrSkin));
    32. } else {
    33. return QIcon(QString("Skin/%1/icons/sys/cartridge16.png").arg(CurrSkin));;
    34. }
    35. }
    36. }
    37. if (role == Qt::DisplayRole) {
    38. if (index.column() == cols.at(0)) {
    39. return ROMs.at(index.row()).section(";", 0, 0);
    40. }
    41.  
    42. if (index.column() == cols.at(3)) {
    43. if (ROMs.at(index.row()).section(";", 3,3) == "0") {
    44. return cEmulator;
    45. } else { return ROMs.at(index.row()).section(";", 3,3); }
    46. }
    47. if (index.column() == cols.at(4)) {
    48. return ROMs.at(index.row()).section(";", 4, 4);
    49. }
    50. if (index.column() == cols.at(6)) {
    51. return ROMs.at(index.row()).section(";", 6, 6);
    52. }
    53. if (index.column() == cols.at(10)) {
    54. return ROMs.at(index.row()).section(";", 10, 10);
    55. }
    56.  
    57. }
    58.  
    59. return QVariant();
    60. }
    To copy to clipboard, switch view to plain text mode 

    Probably the problem is that i must change de content of "cols" members, that should change the column for each section, i'm right?
    So i need a signal, or something to know when and what "cols" members have to change. Tryed QHeaderView::sectionMoved(int,int,int) signal, but seems that the signal is not emitted.
    Really need some clue to know how could approach this.
    Maybe need i reimplement any move related function on my custom QAbstractTableModel? Any help will be appreciated, i'm stucked with this, and i think this is an standard feature for any app, so i must find a way to implement it.
    thanks in advance.
    Always trying to learn >.<

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Sort QTableView Columns with drag and drop

    The problem you are facing is with Sort/Filter model, which you have not posted.

    Did you look at Basic Sort/Filter Model Example, it does the same thing you are looking for
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Sort QTableView Columns with drag and drop

    I've readed the example, but it seems to be a sorting cells example, i'm right?
    I want move columns withouth sort the column's cells, only move the cells with the header moved, just swap columns.
    Last edited by aguayro; 5th January 2013 at 14:41.
    Always trying to learn >.<

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Sort QTableView Columns with drag and drop

    You can drag drop the columns in the example. Try running it.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Sort QTableView Columns with drag and drop

    i've runned the example, the last two columns are swapable as you said, but in my QTableView, i can't see any drop indicator, it seems like a "picture of a table", i click and drag, but nothing changes, there is no a header shadow indicating where to move the column.

    I've tryed:

    Qt Code:
    1. ui->gameView->setDragEnabled(true);
    2. ui->gameView->setDragDropMode(QTableView::InternalMove);
    3. ui->gameView->horizontalHeader()->setSectionsMovable(true);
    4. ui->gameView->horizontalHeader()->setDropIndicatorShown(true);
    5. ui->gameView->horizontalHeader()->setDragEnabled(true);
    6. ui->gameView->horizontalHeader()->setDragDropMode(QTableView::InternalMove);
    To copy to clipboard, switch view to plain text mode 

    I'm very confused with this, nothing seems to work. The headers are static, only can resize them and click to sort cells, but no way to drag.

    EDIT: Ok, I solved it, was a own mistake in app structure, some header routines seems to work before the table is fill, some others not. Thanks for the help!!
    Last edited by aguayro; 5th January 2013 at 16:50.
    Always trying to learn >.<

  6. #6
    Join Date
    Nov 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sort QTableView Columns with drag and drop

    i've the same problem, someone can explain me the solution?

    Thanks

Similar Threads

  1. QTableView Drag and Drop
    By alenyashka in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2012, 14:06
  2. Replies: 2
    Last Post: 13th October 2010, 21:51
  3. QTableView and drag & drop
    By salvaste in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2010, 16:51
  4. Drag and Drop QTableWidget and QTableView
    By scorpion11 in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 09:33
  5. Drag and drop items in view to sort order
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2006, 19:43

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.