Results 1 to 2 of 2

Thread: model/view call setdata() function help?

  1. #1
    Join Date
    Aug 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question model/view call setdata() function help?

    What I'm trying to do is use a button in a dialog to add the struct data(XianStruct) in a reimplemented QAbstractListModel that is shown through a QTableView.
    but I dont know how to call setData() function ? QTableView not refreshing after push the add button?

    thanks! I have the data function as follows:

    //Dialog.h
    Model *ModelData;
    //Dialog.cpp

    ModelData = new Model(this);

    void CParamDialog::on_ButtonAdd_clicked()
    {
    Model::XianStruct addMe;

    ModelData->insertRows(0, 1);

    ModelData->setData( ModelData->index(-1), &addMe, Qt::DisplayRole); ???

    }

    // model.h
    class Model : public QAbstractListModel
    {
    Q_OBJECT

    public:
    Model(QObject *parent = 0);
    ~Model();

    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
    Qt::ItemFlags flags(const QModelIndex &index) const;

    //Required to be able to resize the model (add & remove shit)
    bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
    bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());

    struct XianStruct
    {
    qint32 iZaShu, iFenZhi;
    float fZhouBegin, fZhouEnd, fDiameter ;
    //Make a quick constructor for our struct to have default values for a new item
    XianStruct()
    {
    iZaShu = 50;
    iFenZhi = 1;
    fZhouBegin = 8.0;
    fZhouEnd = 60.0;
    fDiameter = 0.6;
    }
    };

    private:

    QList<XianStruct> _data;

    };

    Q_DECLARE_METATYPE(Model::XianStruct);


    //model.cpp
    QVariant Model::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    return QVariant();

    int row = index.row();
    if ( row < 0 || row >= _data.count() )
    return QVariant();

    if (role == Qt::DisplayRole)
    {
    switch( index.column() ){
    case 0: return _data[row].iZaShu;
    case 1: return _data[row].iFenZhi;
    case 2: return _data[row].fZhouBegin;
    case 3: return _data[row].fZhouEnd;
    case 4: return _data[row].fDiameter;
    default: return _data[row].iZaShu;
    }
    }
    else
    return QVariant();

    return QVariant();
    }
    bool Model::setData(const QModelIndex &index, const QVariant &value, int role )
    {
    if (index.isValid() && role == Qt::DisplayRole)
    {
    int row = index.row();

    XianStruct newData = value.value<XianStruct>();

    //get a pointer to the list item so we can change its contents
    _data[row].iZaShu = newData.iZaShu;
    _data[row].iFenZhi = newData.iFenZhi;
    _data[row].fZhouBegin = newData.fZhouBegin;
    _data[row].fZhouEnd = newData.fZhouEnd;
    _data[row].fDiameter = newData.fDiameter;

    emit dataChanged( index, index);

    return true;
    }
    else return false;
    }:confused:

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: model/view call setdata() function help?

    Don't use -1 to query an index. It will return an invalid index.
    Qt Code:
    1. ModelData->insertRows(0, 1);
    2. ModelData->setData( ModelData->index(0), &addMe, Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    Also, I'm afraid there is something wrong with the way the custom struct is passed around:
    Qt Code:
    1. void CParamDialog::on_ButtonAdd_clicked()
    2. {
    3. Model::XianStruct addMe;
    4. ...
    5. ModelData->setData(..., &addMe, ...);
    6. }
    7.  
    8. bool Model::setData(const QModelIndex &index, const QVariant &value, int role )
    9. {
    10. ...
    11. XianStruct newData = value.value<XianStruct>();
    12. ...
    13. }
    To copy to clipboard, switch view to plain text mode 
    Notice that you are passing a pointer but then you're expecting it to be a value.
    J-P Nurmi

Similar Threads

  1. Cannot call function without object
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 11th June 2007, 15:55
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 09:52
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26

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.