Results 1 to 1 of 1

Thread: Problem with QAbstractListModel

  1. #1
    Join Date
    Mar 2010
    Posts
    69
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QAbstractListModel

    Hi,
    I have subclassed QAbstractListModel and viewing my model using QListView. My only problem is I can edit all my rows in the model. Some of them are editable but some are not and some times the scrollbar is also not working. Any help will be appreciated. Thanks in advance.
    Regards,
    Chandan





    #ifndef PATIENT_MODEL_H
    #define PATIENT_MODEL_H



    #include <QAbstractListModel>
    #include <QList>

    class CCameraDisplayManager;
    class CPatientListModel : public QAbstractListModel{
    //, parent(), rowCount(), columnCount(), and data()
    Q_OBJECT

    private:
    // private statics

    // private members
    CCameraDisplayManager *m_pCameraDiplayManager;
    QStringList m_StringList;

    protected:
    // protected members

    private:
    // private methods - function declarations

    protected:
    // protected methods - function declarations


    ///////////////////////////////////////////////////
    // public interface
    ///////////////////////////////////////////////////

    public:
    // public members

    public:
    // public methods - include class constructors and destructor function declarations here

    public:
    /* StringListModel(const QStringList &strings, QObject *parent = 0)
    : QAbstractListModel(parent), stringList(strings) {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation,
    int role = Qt:isplayRole) const;*/
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    bool insertRows(int position, int rows, const QModelIndex &parent);
    Qt::ItemFlags flags(const QModelIndex &index) const;
    bool setData(const QModelIndex &index, const QVariant &value,
    int role = Qt::EditRole);

    CPatientListModel();
    ~CPatientListModel();
    };

    #endif






    cpp file



    #include "PatientModel.h"

    /////////////////////////////////////////////////////////////////////////////////
    // static member defintions
    /////////////////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////////////////////
    // private methods - function definitions & private constructor / destructor
    /////////////////////////////////////////////////////////////////////////////////
    // private methods - function declarations
    /////////////////////////////////////////////////////////////////////////////////


    /////////////////////////////////////////////////////////////////////////////////
    // protected methods - function definitions
    /////////////////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////////////////////
    // public methods - function definitions
    /////////////////////////////////////////////////////////////////////////////////

    int CPatientListModel::rowCount(const QModelIndex &parent) const
    {
    // returns the number of row in the list

    return m_StringList.count();
    }

    QVariant CPatientListModel::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    return QVariant();

    if (index.row() >= m_StringList.size())
    return QVariant();

    if (role == Qt:isplayRole)
    return m_StringList.at(index.row());
    else
    return QVariant();
    }

    Qt::ItemFlags CPatientListModel::flags(const QModelIndex &index) const
    {
    if (!index.isValid())
    return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    }

    bool CPatientListModel::insertRows(int iPosition, int iRows, const QModelIndex &parent)
    {
    beginInsertRows(QModelIndex(), iPosition, iPosition+iRows-1);

    for (int iRow = 0; iRow < iRows; ++iRow) {
    m_StringList.insert(iPosition, "");
    }

    endInsertRows();
    return true;
    }

    bool CPatientListModel::setData(const QModelIndex &index,
    const QVariant &value, int iRole)
    {
    if (index.isValid() && iRole == Qt::EditRole) {

    m_StringList.replace(index.row(), value.toString());
    emit dataChanged(index, index);
    return true;
    }
    return false;
    }

    /////////////////////////////////////////////////////////////////////////////////
    // public class constructors and destructor
    /////////////////////////////////////////////////////////////////////////////////

    CPatientListModel::CPatientListModel(){

    }

    CPatientListModel::~CPatientListModel(){

    }


    callser function
    Last edited by chandan; 12th April 2010 at 00:29. Reason: I have to edit

Similar Threads

  1. Replies: 6
    Last Post: 30th December 2010, 11:19
  2. QAbstractListModel problem
    By UltimatePace in forum Qt Programming
    Replies: 5
    Last Post: 27th September 2009, 10:51
  3. Problem with QAbstractListModel
    By eekhoorn12 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2009, 14:26
  4. QAbstractListModel searching.
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:41
  5. QAbstractListModel and icon problem
    By mchrk in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2009, 11:21

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.