class MyModel : public QAbstractListModel{ QStringList * m_pList;public: void SetList(QStringList * pList) { m_pList = pList; } QVariant data(const QModelIndex &index, int role /* = Qt::DisplayRole */) const { if (role == Qt::DisplayRole && index.isValid()) return QVariant(m_pList->at(index.row())); return QVariant(); } int rowCount(const QModelIndex &parent) const { return m_pList->size(); } bool setData(const QModelIndex &index, const QVariant &value, int role) { if (index.row() >= 0 && index.row() < m_pList->size() && (role == Qt::EditRole || role == Qt::DisplayRole)) { m_pList->replace(index.row(), value.toString()); emit dataChanged(index, index); return true; } return false; }};
m_pStringList = new QStringList; QStringList & List = *m_pStringList; List<<"hello"<<"world"; m_pEdit = new QLineEdit; setCentralWidget(m_pEdit); m_pCompleter = new QCompleter(this); MyModel * pModel = new MyModel; pModel->SetList(m_pStringList); //QStringListModel * pModel = new QStringListModel; //pModel->setStringList(List); m_pCompleter->setModel(pModel); m_pEdit->setCompleter(m_pCompleter);
class MyModel : public QAbstractListModel{ Q_OBJECTprivate: QStringList * m_pList;public:...
class MyModel : public QAbstractListModel{ Q_OBJECT QStringList * m_pList;public: void SetList(QStringList * pList) { m_pList = pList; } virtual QVariant data(const QModelIndex &index, int role /* = Qt::DisplayRole */) const { if (role == Qt::DisplayRole && index.isValid()) return QVariant(m_pList->at(index.row())); return QVariant(); } virtual int rowCount(const QModelIndex &parent) const { return m_pList->size(); } virtual bool setData(const QModelIndex &index, const QVariant &value, int role) { if (index.row() >= 0 && index.row() < m_pList->size() && (role == Qt::EditRole || role == Qt::DisplayRole)) { m_pList->replace(index.row(), value.toString()); emit dataChanged(index, index); return true; } return false; }};
QVariant QStringListModel::data(const QModelIndex &index, int role) const{ if (index.row() < 0 || index.row() >= lst.size()) return QVariant(); if (role == Qt::DisplayRole || role == Qt::EditRole) return lst.at(index.row()); return QVariant();}bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role){ if (index.row() >= 0 && index.row() < lst.size() && (role == Qt::EditRole || role == Qt::DisplayRole)) { lst.replace(index.row(), value.toString()); emit dataChanged(index, index); return true; } return false;}
m_pStringList = new QStringList; QStringList & List = *m_pStringList; List<<"hello"<<"world"; QStringListModel ; m_pEdit = new QLineEdit; setCentralWidget(m_pEdit); m_pCompleter = new QCompleter(this); m_pModel = new MyModel; m_pModel->SetList(m_pStringList); m_pCompleter->setModel(m_pModel); m_pEdit->setCompleter(m_pCompleter); m_pTestAction = new QAction(tr("Test action"), this); m_pTestAction->setShortcut(tr("Ctrl+T")); addAction(m_pTestAction); connect(m_pTestAction, SIGNAL(triggered()), this, SLOT(Test()));
QStringList & List = *m_pStringList; List<<"hacker"<<"wrapper"; //m_pModel->SetList(m_pStringList);
QStringList & List = *m_pStringList;List<<"hacker"<<"wrapper";m_pCompleter->setModel(pModel);
connect(source, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(rowsInserted())); connect(source, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(invalidate()));