Results 1 to 14 of 14

Thread: Howto swap list of data in a tableview

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto swap list of data in a tableview

    IMO the easiest solution is to have one model for all your streams and either have a proxy filter model to filter out the things you don't want or to make each stream a top-level item of the view and stream data children of a respective item and then to only set a different index as the root index of the view to show a given stream.

    Of course your original approach would have worked too. The problem doesn't seem related to Qt but rather to C++ and indexing a C-array.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. The following user says thank you to wysota for this useful post:

    andsun (21st March 2015)

  3. #2
    Join Date
    Mar 2015
    Posts
    16
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto swap list of data in a tableview

    Hi, interresting, never heard/thought about either of your suggested solutions, but must try them out. Feels like stream as top level might work best... mainly because I don't understand the proxy filter solution but hope the Qt manual will help.

    Agree on point about my solution, must be in my c++ code.

  4. #3
    Join Date
    Mar 2015
    Posts
    16
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto swap list of data in a tableview

    To quit the thread with an answer
    What I wanted to do is not supported, as I can find out about from "Advanced-Qt-Programming-Creating-Great-Software-with-Cpp-and-Qt-4".
    Tree models work in terms of parents and children, where an item’s row is
    its position in its parent’s list of children. (In theory, a tree model can be a
    recursive tree of tables, but none of Qt’s views supports this.)
    However, testing with wysotas suggestion to use QProxyFilter works just fine, only have to add one column with streams to filter on, and hide it. Found example here http://doc.qt.io/qt-5/qsortfilterproxymodel.html
    and here as an example http://doc.qt.io/qt-5/qtwidgets-item...l-example.html

    Thanks guys -this can be closed for now

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto swap list of data in a tableview

    What exactly is "not supported"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Mar 2015
    Posts
    16
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto swap list of data in a tableview

    Hi wysota,

    Haven't tried it out fully yet, but my problem displays where it is not supported, as I understand it.

    I don't know for sure but as I got it, the tree model with subitems of tables can't be displayed in, at the time existing Views like QTreeView.
    This is where my problem lays, and why I implemented the proxy filter to get around it.
    I filter out the "table" within one single model, instead of fetching a model that holds each table from the array of models.

    Make sense? What do you think?

    Sorry to say haven't found any newer, good books on the subject (Advanced Qt programming with Qt 5.nn), suggestions on that?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto swap list of data in a tableview

    Quote Originally Posted by andsun View Post
    the tree model with subitems of tables can't be displayed in, at the time existing Views like QTreeView.
    Why not? QTreeView, as the name suggests, can display tree models. Not that you need that in my opinion, I suggested using root indexes to display a list/table from a tree model.

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. int main(int argc, char **argv) {
    4. QApplication app(argc, argv);
    5. QVBoxLayout *l = new QVBoxLayout(&w);
    6. QComboBox *cb = new QComboBox;
    7. l->addWidget(cb);
    8. QListView *view = new QListView;
    9. l->addWidget(view);
    10. for(int s=0;s<10;++s) {
    11. QStandardItem *stream = new QStandardItem(QString("Stream %1").arg(s+1));
    12. int cnt = qrand() % 20;
    13. for(int e = 0; e < cnt; ++e) {
    14. QStandardItem *item = new QStandardItem(QString("Entry %1 of stream %2").arg(e+1).arg(s+1));
    15. stream->appendRow(item);
    16. }
    17. model.appendRow(stream);
    18. }
    19.  
    20. view->setModel(&model);
    21. cb->setModel(&model);
    22. view->setRootIndex(model.index(cb->currentIndex(), 0));
    23. QObject::connect(cb, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [&](int idx) { view->setRootIndex(model.index(idx, 0)); });
    24. l->addWidget(new QLabel("Full model:"));
    25. QTreeView *tview = new QTreeView;
    26. l->addWidget(tview);
    27. tview->setModel(&model);
    28. w.show();
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Mar 2015
    Posts
    16
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto swap list of data in a tableview

    Hi, thks for code sample
    Think I'm too inexperianced here though. don't get
    QObject::connect(cb, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChang ed), [&](int idx) { view->setRootIndex(model.index(idx, 0)); });
    Guess somehow explained(?) in the old style that I grasp/still use ... and in pseudo stylish it means like:
    connect ( cboBox, SIGNAL( currentIndexChanged(int idx)), treeView, SLOT( treeView->SetRootIndex(int idx)))

    Last edited by andsun; 24th March 2015 at 07:12.

  9. #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: Howto swap list of data in a tableview

    This line uses the Qt5 new-style connect() and a C++11 lambda function in place of a named slot function.
    Qt Code:
    1. QObject::connect(
    2. cb,
    3. //^^^ sender object
    4. static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
    5. //^^^ a PointerToMemberFunction for the currentIndexChanged(int) signal function and not the currentIndexChanged(QString) one
    6. [&](int idx) { view->setRootIndex(model.index(idx, 0)); }
    7. //^^^ a C++11 lambda function accepting an int argument (Functor)
    8. );
    9.  
    10. // equivalent to the traditional Qt
    11.  
    12. connect(
    13. cb,
    14. SIGNAL(QComboBox::currentIndexChanged(int)),
    15. SLOT(someSlot(int))
    16. );
    17.  
    18. // with a slot function
    19. void someSlot(int idx) {
    20. view->setRootIndex(model.index(idx, 0));
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 6
    Last Post: 25th June 2011, 11:35
  2. Managing data in a TableView
    By scarleton in forum Qt Programming
    Replies: 6
    Last Post: 11th June 2010, 00:48
  3. Replies: 0
    Last Post: 28th April 2010, 11:41
  4. Replies: 2
    Last Post: 29th September 2008, 00:08
  5. getting data from tableView
    By mkarakaplan in forum Newbie
    Replies: 1
    Last Post: 7th November 2007, 09:51

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.