Solved Compiling a lambda using a signal thinks signal is undefined?
-
In the constructor I define a lambda. That lambda when called needs to trigger a signal defined later in the class. Why would it think it is undefined? The
compilerlinker does not like this.#include <QAbstractProxyModel> #include <QAbstractItemModel> #include <QModelIndex> #include <QFileSystemModel> #ifndef TABLEPROXYMODEL_H #define TABLEPROXYMODEL_H class FileSystemTableProxyModel : public QAbstractProxyModel { Q_PROPERTY(QAbstractItemModel* model READ sourceModel WRITE setSourceModel NOTIFY sourceModelChanged) Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged) public: FileSystemTableProxyModel(){ connect(this, &QAbstractProxyModel::sourceModelChanged, [this](){ m_rootIndex = sourceModel()->index(0,0); emit rootIndexChanged(m_rootIndex); }); } QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { return QVariant(); } QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override { Q_UNUSED(parent) if(row < 0 || row >= rowCount()) return QModelIndex(); if(column < 0 || column >= columnCount()) return QModelIndex(); return createIndex(row, column); } QModelIndex parent(const QModelIndex &index) const override { Q_UNUSED(index) return QModelIndex(); } int rowCount(const QModelIndex &parent = QModelIndex()) const override { Q_UNUSED(parent) return sourceModel()->rowCount(m_rootIndex); } int columnCount(const QModelIndex &parent = QModelIndex()) const override { Q_UNUSED(parent) return 4; } QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override { if(sourceIndex.model() != sourceModel() || !sourceIndex.isValid()) return QModelIndex(); auto proxyindex = index(sourceIndex.row(), sourceIndex.column()); if(!proxyindex.isValid()) return QModelIndex(); return proxyindex; } QModelIndex mapToSource(const QModelIndex &proxyIndex) const override { if(proxyIndex.model() != this || !proxyIndex.isValid()) return QModelIndex(); auto sourceindex = sourceModel()->index(proxyIndex.row(), proxyIndex.column()); if(!sourceindex.isValid()) return QModelIndex(); return sourceindex; } QModelIndex rootIndex() const { return m_rootIndex; } public slots: void setRootIndex(QModelIndex rootindex) { if (m_rootIndex == rootindex) return; if(sourceModel() != rootindex.model()) return; m_rootIndex = rootindex; emit rootIndexChanged(m_rootIndex); } signals: void rootIndexChanged(QModelIndex rootIndex); private: QModelIndex m_rootIndex; }; #endif // TABLEPROXYMODEL_H
Edit:
The error:undefined reference to Makefile:263: recipe for target 'ProxyModelTesting' failed `FileSystemTableProxyModel::rootIndexChanged(QModelIndex)' collect2: error: ld returned 1 exit status
-
@fcarney You're missing
Q_OBJECT
macro so moc did not generate signal's body.Unrelated - You should use
const QModelIndex&
parameters and make your signalsconst
.