Results 1 to 14 of 14

Thread: Howto swap list of data in a tableview

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

    Default Howto swap list of data in a tableview

    Hi guys,

    I have come across a problem the is to hard, sofar for me. Have an application with several dialogs, and one is a dialog built up with a combobox and a tableview + buttons

    I have a
    Qt Code:
    1. struct limits { float lower[10], float upper [10}]
    To copy to clipboard, switch view to plain text mode 
    that is written, based on 10 elements and 20 streams to a data file every 3 -5 minutes together with other data in my system.
    The Filehandler is pointing to next position to write and when reaching the max values, return to the beginning of the file to continue. It holds 10 000 rows of data.

    Now I want to present major data and limits based on a selected value in a combobox. That is, I create a QStandardItemModel models [ NoOfStreams ] and fills each Model with QStandardItem to be presented in a specific column and row.

    Sofar so good, but when I select a stream and want to fetch the QStandardItemModel for the selected stream, either it doesn't show anything or it chrashes, depending och solution. The swaping is done in the View and the Models were created in the Models area and sent to the View's Q_Property.

    So my question is How shall I perform the swapping, or am I doing (thinking) this wrong?

    My ideas are from example FrozenColumn and modified
    Qt Code:
    1. in Model
    2. read(filehandler, container, sizeOfLimits)
    3.  
    4. QStandardItemModel models[streams]
    5. //loop and fill with QStandardItem
    6. QStandardItem * newItem
    7. //for each stream,
    8. // for each row,
    9. // for each col
    10. newItem = new QStandardItem
    11. newItem->setText(container[col]->textValue)
    12. models[stream].setItem(row, col, newItem)
    13. //
    14. // asign models[] to VIEW ui's Q_PROPERTY
    15. gui->ui->setModel(models)
    16.  
    17. // VIEW
    18. // sofar OK
    19. // when comboBox index changed
    20. // verify in scope
    21. // this crasches
    22. tableview->setModel(Models()[cboBox_Index])
    23.  
    24. // but not this, and it displays nothing in the tableview
    25. QStandardItemModel *model = new QStandardItemModel(&m_Model[cboBox_index])
    26. tableview->setModel(model)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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

    It is very unclear what message your post and pseudo code is trying to convey. Certainly nothing in there looks like a "swap".

    If you want to change the model displayed by a view simply call the view's setModel() function with the new model.

  3. #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

    Hi ChrisW67

    Sorry, for the blurry question.
    I've created an array of QStandardItemModel's and added data divided into one Model per stream
    Figured that the easiest way is to put stream number in a combobox and when changed simply use stream number as index to find correct model in the array.
    Then take the array and use setModel(modelStreamnr).

    However I don't get this to work. Nothing displays. Thinking I'm doing something wrong in creating the model, adding data or obstructing some rule

    Above pseudo, line 22 make a setModel, by geting the Q_PROPERTY model array and index out the correct one to be returned and set in setModel

    Thanks for quick answer

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    andsun (21st March 2015)

  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, 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.

  7. #6
    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

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  9. #8
    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?

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  11. #10
    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.

  12. #11
    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 

  13. #12
    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 ChrisW67,

    perfect, now I get it.
    Also, I was missing the CONFIG += c++11 in the .pro file. And now it compiles and works.
    Shall play around with the code and in case I have trouble I'll be back.


    Added after 1 23 minutes:


    Hi again...

    I don't seem to get out what I want. Like an excel sheet with grouped rows and columns
    tabel.jpg

    Have tried to modify the above code, in its simplest form I've done this, added items to a QList and appended the list as columns
    Am I way of here, should I use a table instead or is it doable??

    or(int s=0;s<10;++s) {
    QStandardItem *stream = new QStandardItem(QString("Stream %1").arg(s+1));
    int cnt = qrand() % 20;
    QList<QStandardItem *> items;
    for(int e = 0; e < cnt; ++e) {

    QStandardItem *item = new QStandardItem(QString("Column 1 tablerow %1 of stream %2").arg(e+1).arg(s+1));
    items.append(item);

    QStandardItem *subitem = new QStandardItem(QString("Column 2 of stream %2").arg(e+1).arg(s+1));
    items.append(subitem);

    }
    stream->appendColumn(items);
    model.appendRow(stream);
    }
    /anders
    Last edited by andsun; 24th March 2015 at 13:09.

  14. #13
    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

    Please post code inside [code]...[/code] tags.

    You asked to show a table for a stream selected separately. Wysota's example does that but using a single tree model for the entire data set rather than your original array-of-models approach. The single list view in the example displays a subset of the tree based on the combo box selection. Using a table view rather than a list view is just a matter of changing the view object class and populating more than one column of the model.

    If you want to display the entire tree in a tree view then create a QTreeView and set the tree model as its model. Nothing more to it. No combo box required.

    No view in Qt out-of-the-box does anything visually like the Excel grouping screen capture.

  15. The following user says thank you to ChrisW67 for this useful post:

    andsun (25th March 2015)

  16. #14
    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

    True, & sorry for missing code tags

    I've experimented and as you say ChrisW67, just change the view... as well as one get the answer on what one asks for

    Thanks a lot guys

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.