Results 1 to 9 of 9

Thread: QTableview Sorting and hiding

  1. #1
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Smile QTableview Sorting and hiding

    Hi, the code below does not sort and does not hide the column but the column header shows active when is clicked. Please, help

    Qt Code:
    1. model->setQuery("SELECT f1, id, f3, serialno FROM person2 WHERE f1 like '" + nametxt + "'");
    2. model->setHeaderData(0, Qt::Horizontal, tr("f1"));
    3. model->setHeaderData(1, Qt::Horizontal, tr("id"));
    4.  
    5.  
    6. QTableView *view = new QTableView;
    7.  
    8. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
    9. view->setSortingEnabled(true);
    10. :) view->setSelectionBehavior(QAbstractItemView::SelectRows);
    11. view->setSelectionMode(QAbstractItemView::SingleSelection);
    12.  
    13. view->setAlternatingRowColors(true);
    14. view->setAutoFillBackground( true );
    15. QPalette p = view->palette();
    16. p.setColor(QPalette::Base, Qt::darkYellow);
    17. p.setColor(QPalette::Highlight, Qt::magenta);
    18. view->setPalette(p);
    19.  
    20. view->hideColumn(0); // does not work!
    21.  
    22. view->verticalHeader()->hide();
    23.  
    24. view->setModel(model);
    25. view->show();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTableview Sorting and hiding

    What happens if you hide the column after you've set the model?

  3. #3
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableview Sorting and hiding

    I think the problem is I am using QTableView with QSqlQueryModel instead of QSqlTableModel but I do not
    know how to sovle it maybe Proxy? please help.
    Thanks

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableview Sorting and hiding

    You cannot hide columns in the view before the view knows it has columns to hide. Set the model first, then hide the column.

  5. #5
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QTableview Sorting and hiding

    Chris,
    Solved, thanks a lot; now the problem is mapToSource:
    Qt Code:
    1. QModelIndex index=model->index(index.row());
    2. index = proxyModel->[B]mapToSource[/B](index);
    3. connect(view, SIGNAL(clicked(QModelIndex)),
    4. this, SLOT(for3(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::for3(QModelIndex,index)
    2. QSqlRecord record = [B]model[/B]->record(index.row());
    To copy to clipboard, switch view to plain text mode 

    What should be for model in for3 function or any other sugestion; sorry for the so many mistakes, but please give some hints. Thanks!

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableview Sorting and hiding

    What's the problem and what do you want to achieve? I can give you hints and advice like "Be nice to your mother" but it probably won't help.

    If the view is using a proxy model then the index in the clicked() signal will be a proxy model index. If you need the source model index then you need to use mapToSource() but you might not need to do that depending on what you wish to achieve.

  7. #7
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableview Sorting and hiding

    Hi Chris,
    I created a view, it was working fine (but not sorting) when clicked could go to for3 function, from there I could manipulate the record with model record. Then, I used QSortFilterProxyModel as below for sorting, it sorts but when it is clicked it goes to wrong record. As you mentioned I do not know how to implement the index2 with click! Or I might be missing or using wrong statements. Sorry for confusion and thank you for your help.

    Qt Code:
    1. filterModel->setSourceModel(model);
    2. QTableView *view = new QTableView;
    3. filterModel->setDynamicSortFilter(true);
    4. filterModel->dynamicSortFilter();
    5.  
    6. view->setModel(filterModel);
    7. QModelIndex index = view->currentIndex();
    8. QModelIndex [B]index2 [/B]= filterModel->mapToSource(index);
    9.  
    10. connect(view, SIGNAL(clicked([B]QModelIndex[/B])),
    11. this, SLOT(for3(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::fory3(QModelIndex index)
    2. {
    3. QSqlRecord record = [B]model[/B]->record(index.row());
    4. QString name = record.value("artist").toString();
    5. QString jobv = record.value("job").toString();
    6.  
    7. QString person2serial= record.value("serialno").toString();
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableview Sorting and hiding

    Line 8 and 9 in your first code snippet make no sense where you have put them in the construction code.

    I have commented in your second code snippet:
    Qt Code:
    1. void MainWindow::fory3(QModelIndex index) // I assume this is a typing error fory3 or for3?
    2. // ^^^^^ this is an index in the proxy model
    3. {
    4. // Before you can use the index against the source QSqlTableModel you need to map it
    5. QModelIndex srcIndex = filterModel->mapToSource(index);
    6. QSqlRecord record = model->record(srcIndex.row());
    7. QString name = record.value("artist").toString();
    8. QString jobv = record.value("job").toString();
    9.  
    10. QString person2serial= record.value("serialno").toString();
    To copy to clipboard, switch view to plain text mode 
    However, you don't need to worry about the layering of models if you use the Model/View interface to access the data
    Qt Code:
    1. void MainWindow::for3(QModelIndex index)
    2. {
    3. int row = index.row();
    4. QString name = index.sibling(row, nameColumn).data().toString();
    5. QString jobv = index.sibling(row, jobvColumn).data().toString();
    6. QString person2serial= index.sibling(row, serialnoColumn).data().toString()
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableview Sorting and hiding

    Hi Chris,
    Solved; supper. I realy appriciate your great favor. That is very kind of you.
    Thanks a lot

Similar Threads

  1. Replies: 3
    Last Post: 9th December 2010, 19:27
  2. QTableView sorting
    By realdarkman71 in forum Newbie
    Replies: 12
    Last Post: 1st December 2010, 22:45
  3. QTableView sorting
    By Bojan in forum Newbie
    Replies: 2
    Last Post: 28th September 2006, 08:11
  4. QAbstractTableModel row hiding and sorting
    By Rayven in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2006, 02:39
  5. Sorting QTableView
    By Jimmy2775 in forum Qt Programming
    Replies: 7
    Last Post: 9th February 2006, 16:47

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