Hi I can't figure out why my data() function is never called when populating QTableView
I subclassed QSqlQueryModel. The header is like:
class TicketModel : public QSqlQueryModel
{
Q_OBJECT
public:
explicit TicketModel(QObject *parent = 0);
QVariant data(const QModelIndex &index, int role);
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
};
In the main window I set my model to the table
TicketModel *model = new TicketModel();
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(model);
QSqlQuery *query = _tf->search(1);
model->setQuery(*query);
_ui->dashTable->setModel(proxyModel); // <<<<<<<<<<<<<< I setting model here too, didn't work
_ui->dashTable->setSortingEnabled(true);
_ui->dashTable->horizontalHeader()->setVisible(true);
_ui->dashTable->setSelectionBehavior(QAbstractItemView::SelectRows);
The TicketModel::headerData(...) is called but TicketModel::data(...) is never called when the table is created. Why? How can I get it to be called?
I hope I just overlooked something simple but I have been trying for a few hours to figure it out.
Thanks for the help!
You've got the signature wrong. You need a const.
QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const
Related
I have a list of custom objects that I need to split across multiple pages of a SwipeView.
Right now, I implement a QAbstractListModel to provide the data to my QML view through custom roles.
But when I have more then a given number of object, I need to split my model in multiple chunks that will be displayed on different pages of a SwipeView.
If my model has 20 object, my SwipeView will have 2 pages with 12 Items in the first one and the 8 remainings in the second page for example but the number of items is, of course, dynamic.
I know that I can use a QSortProxyFilter or a DelegateModel to filter my model on a criteria but I don't know how I can use them to create groups usable as submodels for the content of the pages of the SwipeView. Because of course, I cannot just change the filter when the page change because that wouldn't make the items visible when swiping from one page to the other.
Thanks for any hint or idea on how to achieve this.
Why don't you include a model into another model? You can return the model to QML via QVariant::fromValue. See my example:
models.h
class ChildItem{
public:
ChildItem() {}
};
class ChildModel: public QAbstractListModel{
Q_OBJECT
public:
explicit ChildModel(QObject *parent = nullptr);
void addItem(const ChildItem &item);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
signals:
void itemAdded();
protected:
QHash<int,QByteArray> roleNames() const;
private:
QVector<ChildItem> m_items;
};
class ModelItem{
public:
ModelItem() {}
ChildModel *childModel() const;
private:
ChildModel *m_childModel;
};
class MainModel: public QAbstractListModel{
Q_OBJECT
public:
explicit MainModel(QObject *parent = nullptr);
void addItem(const ModelItem &item);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
signals:
void itemAdded();
protected:
QHash<int,QByteArray> roleNames() const;
private:
QVector<ModelItem> m_items;
};
models.cpp
ChildModel *ModelItem::childModel() const{
return m_childModel;
}
QVariant MainModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= (int)m_items.size())
return QVariant();
const MainModel &item = m_items[index.row()];
switch (role) {
case MainModelRoles::ChildModel:{
return QVariant::fromValue(item.childModel());
}
default: {
break;
}
}
return QVariant();
}
QHash<int, QByteArray> MainModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[MainModelRoles::ChildModel] = "child_model";
return roles;
}
From within QML you can present model role name as a new model for another element. For example:
StackLayout which is your main element, includes Repeater.
Repeater model is MainModel class instance.
Repeater delegate includes ListView
ListView takes ChildModel as a model for itself (child_model reference)
So you can contain any amount of different models inside main model and expose them to QML as model property. Is this what you're looking for?
I'm trying to support rearranging a list of large objects in a QListView. It seems that the model/view framework insists on serializing the items which, in this case, is highly inefficient. I've tried to shorten things by using shared_ptr's, so only one bit of data needs passing, but even so, it feels very messy and is not at all reliable.
Worse still, during the move I notice the serialization functions are called repeatedly (dozens/hundreds of times). Surely they should only be called once each.
I'm sure I'm missing a trick, what's the correct way to achieve simple, internal move, list ordering without heavy serialize overhead?
class Big
{
public:
Big();
~Big();
...
};
using BigPtr = std::shared_ptr<Big>;
Q_DECLARE_METATYPE(BigPtr) // and qRegisterMetaTypeStreamOperators
// Required serialization operators
// This is very ugly, and what I want rid of...
QDataStream &operator<<(QDataStream &out, const BigPtr &big)
{
qDebug() << "Out <<";
out << (quint64)big.get();
return out;
}
// ...This too...
QDataStream &operator>>(QDataStream &in, BigPtr &big)
{
qDebug() << "In >>";
quint64 raw = 0;
in >> raw;
li.reset((LayerPtr::element_type *)raw);
return in;
}
class BigModel : public QAbstractListModel
{
Q_OBJECT
public:
BigModel (std::vector<BigPtr> &bigs, QObject *parent = Q_NULLPTR);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const; // Qt::ItemIsDragEnabled or Qt::ItemIsDropEnabled depending on valid index
Qt::DropActions supportedDropActions() const; // return Qt::MoveAction
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
private:
std::vector<BigPtr> &_bigs;
};
...
// dialog setup...
ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->listView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked);
ui->listView->setDragEnabled(true);
ui->listView->setDragDropMode(QAbstractItemView::InternalMove);
ui->listView->setDropIndicatorShown(true);
ui->listView->viewport()->setAcceptDrops(true);
EDIT:
After more head scratching, I decided to overload the mimeData and dropMimeData functions and sub-class QMimeData to carry the BigPtr directly.
class BigMimeData : public QMimeData
{
Q_OBJECT
public:
BigMimeData(const QVariant &_big)
: big(_big)
{ }
bool hasFormat(const QString &/*mimeType*/) const { return true; }
QVariant big;
};
...
QMimeData *BigModel::mimeData(const QModelIndexList &indexes) const
{
if(indexes.count() > 0) {
return new BigMimeData(indexes.at(0).data());
}
return Q_NULLPTR;
}
bool BigModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int /*column*/, const QModelIndex &parent)
{
if(!data || !(action == Qt::CopyAction || action == Qt::MoveAction)) {
return false;
}
// start of list
if(row == -1) {
row = rowCount(parent);
}
auto dragged = static_cast<const BigMimeData *>(data);
// setData with stored value
insertRow(row, parent);
setData(index(row, 0, parent), dragged->big);
return true;
}
Which works, but seriously!? All that just to enable simple list reordering in the UI? It can't be this complex surely?
BTW, a lot of these answer seem to be focusing on the underlying data structure, that's not the question. The problem is that simple internal move drag/drop rearrangement in a list control seem to be ridiculously complex in QListView because it requires costly serialization. It's a UI/Widgets/MVC question.
I'm using QComboBox to select unit (an arbitrary property of the object) from Units table. The problem is that object in my data model can have no unit (NULL in the table), in which case QComboBox shows value happened on top of the list. Select 'none' is not possible.
What do you suggest to add NULL support? I have few versions:
Insert special record in Units table named '--' or 'N/A'. Not exactly NULL - will have its own id.
Set items in QComboBox and update model manually. Possible but tedious - goodbye automatic update of unit list.
What else is possible - subclassing QComboBox (overriding what)? I don't see anything similar to setEditorData/setModelData like in QAbstractItemDelegate to control items.
You can subclass the model, so that data will return a special value for NULL and then setData will check for the special value and substitute a NULL.
Example sketch of code:
class MyModel : public QSqlTableModel
{
Q_OBJECT
public:
MyModel();
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
};
QVariant MyModel::data(const QModelIndex& idx, int role)
{
QVariant var = QSqlTableModel::data(idx, role);
if (var.isNull())
var = QVariant(QString("NULL"));
return var;
}
bool MyModel::setData(const QModelIndex& idx, const QVariant& value, int role)
{
QVariant var(value);
if (var == QString("NULL"))
var == QVariant(record().field(idx.column()).type());
return QSqlTableModel::setData(idx, var, role);
}
I am fairly new to QT, and am having trouble understanding how the QTableView selection changed signal is handled. I have setup a window with an openGL widget and a QTableView. I have a data model class that is correctly populating the tableview, so I added a public slot to that class:
class APartsTableModel : public QAbstractTableModel
{
public:
AVehicleModel *vehicle;
explicit APartsTableModel(QObject *parent = 0);
//MVC functions
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &paret) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
public slots:
void selectionChangedSlot(const QItemSelection &newSelection,
const QItemSelection &oldSelection);
};
When I am ready to show the window with the table view, I allocate/initialize it like this:
//create the display view
AStarModelView *displayWindow = new AStarModelView(this,
starModel->vehicle);
//create the datamodel for the table view
APartsTableModel *dataModel = new APartsTableModel(displayWindow);
dataModel->vehicle = starModel->vehicle;
//create selection model for table view
QItemSelectionModel *selModel = new QItemSelectionModel(dataModel);
displayWindow->materialsTable->setSelectionModel(selModel);
//setup model and signal
displayWindow->materialsTable->setModel(dataModel);
connect(selModel,
SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
dataModel,
SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
//show the view
displayWindow->show();
When I set a breakpoint in the implementation of the slot function, I never hit it. I've also tried not allocating a new QItemSelectionModel, but that didn't work either. I'm really not sure what I'm doing wrong here.
When you call setModel() on the view, your locally allocated QItemSelectionModel is getting replaced by one created by the view. You shouldn't have to create your own selection model anyway. Just change your connect to
connect(displayWindow->materialsTable->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
dataModel,
SLOT(selectionChangedSlot(const QItemSelection&, const QItemSelection&)));
What's the first thing you should check in QT when signals/slots don't seem to be working correctly? That your class has the Q_OBJECT macro in it. Added this to the APartsTable class definition, and now I'm hitting the breakpoint.
When does Friday get here?
Just to pull the answer out of the discussion:
What's the first thing you should check in QT when signals/slots don't
seem to be working correctly? That your class has the Q_OBJECT macro
in it. Added this to the APartsTable class definition, and now I'm
hitting the breakpoint
.
virtual Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
must return Qt::ItemIsSelectable | otherFlags
I've cooked up a QAbstractListModel whose model indexes contain a pointer I absolutely needed in order to process data. I add the data like so:
void PointListModel::addPoint(int frameNumber, QPoint const& pos)
{
PointItem *pointItem = new PointItem( frameNumber, pos );
QModelIndex newRow = this->createIndex( m_points.count(), 0, pointItem );
qDebug() << newRow.internalPointer();
beginInsertRows( newRow, m_points.count(), m_points.count() );
m_points.insert( m_points.count( ), pointItem );
endInsertRows();
emit pointAdded( pointItem, pos );
}
It was only later that I realized that the argument to beginInsertRows is asking for the parent model index of the new row, not the new row's actual model index.
So, at this point in time, Qt has given me no way of supplying a QModelIndex to associate with this particular row. How do I create my own model index for this new row?
Okay, I'm rewriting my answer as after some research I've found out that I got it wrong.
You shouldn't do anything special to create a new index when you add new data. You code should look like this:
PointItem *pointItem = new PointItem( frameNumber, pos );
// assume you insert a top level row, having no parent
beginInsertRows( QModelIndex(), m_points.count(), m_points.count() );
m_points.insert( m_points.count( ), pointItem );
endInsertRows();
Then you should implement the index() method which will create indexes on demand and the parent() method which will determine the parent of some index, but since you have a list model, it should probably always just return QModelIndex(). Here is a good article about creating custom models.
Here is a complete example of a working QAbstractListModel:
class MyModel: public QAbstractListModel {
Q_OBJECT
public:
virtual QModelIndex index(int row, int column = 0,
const QModelIndex &parent = QModelIndex()) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void add(int i);
private:
QList<int> list;
};
void MyModel::add(int i)
{
beginInsertRows(QModelIndex(), list.size(), list.size());
list.append(i);
endInsertRows();
}
QModelIndex MyModel::index(int row, int column,
const QModelIndex &parent) const
{
return hasIndex(row, column, parent) ? createIndex(row, column, (void*)&list[row])
: QModelIndex();
}
int MyModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return list.size();
}
QVariant MyModel::data(const QModelIndex &index,
int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
return QVariant(QString::number(*static_cast<int*>(index.internalPointer())));
}
I've cooked up a QAbstractListModel whose model indexes contain a pointer I absolutely needed in order to process data.
If you start with wrong requirements, you end up with wrong solutions :)
A list model is simple enough so that you don't need more than the QModelIndex's row() to uniquely define the data the index addresses.
So, given a QModelIndex mi, when you before did
PointItem * item = static_cast<PointItem*>(mi.internalPointer());
you can instead do
PointItem * item = plm->pointItemFromIndex(mi);
where plm is your PointListModel. If you don't have a pointer to it lying around when you need to access the PointItem, you can reconstruct it like this:
PointItemModel * plm = qobject_cast<PointItemModel*>(mi.model());
// check for !plm here (!mi.isValid() || qobject_cast fails)
In turn, PointListMode::pointItemFromIndex() would do the actual work:
PointItem * PointListMode::pointItemFromindex(const QModelIndex &mi) const {
return mi.isValid() ? m_points[mi.row()] : 0 ;
}
This is the most important thing to realize when working with QAbstractListModel in Qt: Mentally replace QModelIndex with int row, ignore everything else it has (an invalid QModelIndex has row() == -1).
Same for QAbstractTableModel: mentally reduce the QModelIndex to int row, int column. Forget everything else.
The only time you need the full QModelIndex (including its internalPointer() or internalId() is when you implement a tree model (QAbstractItemModel).