Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

QStandardItemModel: where to set constraints on the data?



  • Hi All,

    I'm trying to understand the Qt model/view classes. So far I've been able to do the following:
    -Create a QStandardItemModel, and add data to it.
    -Read an excel file into the model using QXlsx
    -Convert the datatypes in the model by reading each element of the QStandardItemModel into an QStandardItem, then use QStandardItem->data to set it into a QVariant, convert the QVariant into the datatype I want, and write it back into the model via QStandardItem->setData and QStandardItemModel->setItem
    -Displaying the model by using QTableView->setModel and displaying QTableView into a QSplitter

    I'm impressed by the performance, importing a 2000 rows excel takes under a second, and my program is using only 45MB of RAM.

    Now I'm stuck on the following things:
    1)Enforcing a limit on the data. I have a column of integers that need to be confined between 0 and 254, while another column of integers needs to be between 0 and 99. Where to set these constraints?
    2)Altering the way the data is shown. I have a column of QDateTime's that I would like to be formatted like "yyyy-MM-dd hh:mm:ss". Where can I set that?
    3)Altering the way the data is shown. I have a column of QTime's that I would like to be formatted like "hh:mm:ss" Where can I set that?
    4)I would like to save / load the data into a Qsettings. I can read each item in the model to a QVariant, and then store the QVariants into the Qsettings. Is there a better way to do this?

    Cheers,
    Cedric


  • Qt Champions 2017

    1. You can't directly enforce the range here. You need to write your own model inheriting from QAbstractListModel or so & impose the constraint. Or since you are reading the data you need to look at the data and insert if it is within the range.

    2 & 4 - You need to write you custom Delegate for the same.

    1. Settings may not be the right fit to load/save the the data. For smaller data sets it looks ok. But for bigger data i set i prefer to have my own way storing the data ie csv or db or xml or something else.


  • @cdwijs
    I don't quite understand @dheerendra's proscriptions here. Depending on what you want to achieve, you can alternatively do:

    For 1, use http://doc.qt.io/qt-5/qstandarditemmodel.html#setData (setData()) to do whatever if you don't like the value/range, e.g. refuse to set the value, or error.

    For 2, use http://doc.qt.io/qt-5/qstandarditemmodel.html#data (data()) to return your desired formatted string in, say, the DisplayRole case.

    But maybe you both know these, and I am misunderstanding....



  • @dheerendra

    1. I have never written any class that inheres anything other than QObject (to enable signals and slots.) Do you have an example that inherents from QAbstractListModel? I would like to have one place where I tell Qt what the constraints are. This is because the user has 3 ways to make the data go out of range: a)Import an excel file with wrong values. b)Modify the .ini file where all the settings are stored. c) modify the data in the QTableView with the mouse and keyboard.

    2&3) I will look into custom delegates, it does not look that hard:
    https://www.bogotobogo.com/Qt/Qt5_QTableView_QItemDelegate_ModelView_MVC.php

    1. You are right, at least QSettings can only store ~32000 lines in an array, as it uses an int to return the number of items in an array. As my application is very light (the customer will use about 20 lines, I'm performance testing with 2000) this i not an issue for this project.
      @JonB
    2. QStandardItemModel::setData has all the information to enforce the data constraints, as it knows what column the data is headed. Do you have an example for me? Do the delault delegates honor the constraints i imposed in this function?

    Cheers,
    Cedric



    1. where do you need to enforce it? if you just need to manage user input you should do it on the delegate, not the model
    2. see 3
    3. The link you posted is too old. You should subclass QStyledItemDelegate. reimplementing displayText should allow you to manage how each type is displayed
    4. QSettings is a terrible idea. I beg you to reconsider. To save the models you can use the model serialisation classes of this library
    5. true but I'm still not convinced the model should take care of this, see 1

  • Qt Champions 2017

    In addition to @VRonin @JonB said, constraints for data is not function of Model. You should decided what data to be inserted in to the model.

    If you want to display the data in date format, you can use the idea suggested by JonB.
    If you want to display the date/time with using in appropriate editable format, then you need to write the your own delegate.


Log in to reply