[solved] selectionModel->selectedIndexes() always returns saying nothing is selected in a table
-
I'm new to Qt and wrote a sample program using a custom table model. I've used the Qt sample program located at Qt\Qt5.3.2\Examples\Qt-5.3\widgets\itemviews\addressbook as a reference, but I can't seem to figure out what I'm doing wrong. Below is the method I'm trying to get working:
@void MainWindow::on_btnDelete_clicked()
{
QItemSelectionModel *selectionModel = ui->tblviewPersons->selectionModel();
QModelIndexList indexes = selectionModel->selectedIndexes();
//QModelIndexList indexes = selectionModel->selectedRows();// PROBLEM: indexes is always empty // indexes.count() == 0 // .... other code omitted
}
@I've also tried selectedRows() but it also fails.
This answer makes me think I'm on the right track.
http://doc.qt.digia.com/4.6/model-view-selection.htmlI have searched for answers to this but so far the solutions offered do not work, so I must be doing something else wrong. My guess is that when I implemented my "own custom model", I did not implement something that I needed to in order for this to work.
Any help is appreciated. I've tried to include ALL of the code but the post was too large.
Thanks,
Patrick====================================================================
Only the code is below so if you don't need to look at it, stop reading here!Below are some of the files of this "sample". The sample has a TableView and two PushButtons (add and delete). The data used in the example is a "Person" class and a person has a name and a phone number.
The class PersonTableModel is the code that maintains a list of "Person" objects and handles all of the Qt GUI model work.NOTE: This is my first MVC program with Qt so I may be "off base" with this implementation. If I am please just tell me so I can improve my understanding of Qt. Thanks.
Next is PersonTableModel.h. This implements the TableModel.
@
#ifndef PersonTableModel_h
#define PersonTableModel_h#include <QAbstractTableModel>
#include <QString>class Person;
const int COLS= 3;
const int ROWS= 2;class PersonTableModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit PersonTableModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const ;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
Qt::ItemFlags flags(const QModelIndex & index) const ;
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());void addPerson( Person* pPerson );
private:
QList<Person*> m_listPersons;
//QString m_gridData[ROWS][COLS]; //holds text entered into QTableView
signals:
void editCompleted(const QString &);
void dataChanged( QModelIndex& topLeft, QModelIndex& bottomRight);};
#endif // PersonTableModel_h
@Below is PersonTableModel.cpp
@
#include <QDebug>#include "PersonTableModel.h"
#include "Person.h"PersonTableModel::PersonTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
qDebug() << "MyModel::MyModel";
}bool PersonTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if (role == Qt::EditRole)
{
//save value from editor to member m_gridData
Person* pPerson = m_listPersons.at( index.row() );
if ( index.column() == 0 )
{
// Update the name
pPerson->setName( value.toString() );
}
else
{
pPerson->setPhone( value.toString() );
}
emit editCompleted( QString( value.toString() ) );
}
return true;
}Qt::ItemFlags
PersonTableModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags rtn = QAbstractTableModel::flags(index);
if ( index.column() < 2 )
{
rtn = Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}
return rtn;
}int
PersonTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED( parent );
return m_listPersons.count();
}int
PersonTableModel::columnCount( const QModelIndex &parent) const
{
Q_UNUSED( parent );
return COLS;
}QVariant PersonTableModel::data(const QModelIndex &index, int role) const
{
QString rtn;qDebug() << "MyModel::data: row,col=" << index.row() << "," << index.column(); if ( ! index.isValid() ) return QVariant(); if (role == Qt::DisplayRole || role == Qt::EditRole) { int row = index.row(); int col = index.column(); Person* pPerson = m_listPersons.at( row ); if ( col == 0 ) { rtn = pPerson->getName(); } else { rtn = pPerson->getPhoneNum(); } } return rtn;
}
void
PersonTableModel::addPerson( Person* pPerson )
{
int position = m_listPersons.count();
int rows = 1;
beginInsertRows( QModelIndex(), position, position + rows - 1 );
m_listPersons.insert( position, pPerson );endInsertRows(); qDebug() << "PersonTableModel::addPerson ADDED: " << pPerson->getName();
}
bool
PersonTableModel::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
for( int ii = 0; ii < rows; ii++ )
{
m_listPersons.insert( position + ii, new Person( QString(), QString() ) );
}
endInsertRows();
return true;}
bool
PersonTableModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);m_listPersons.removeAt( position ); endRemoveRows(); return true;
}
@
-
I don't know what caused this program to fail but I did the same code in another application and it worked correctly, so I'm going to mark this problem as solved and assume that there is some other problem that I have that is preventing the selections from working.
-
I don't know what caused this program to fail but I did the same code in another application and it worked correctly, so I'm going to mark this problem as solved and assume that there is some other problem that I have that is preventing the selections from working.