PDA

View Full Version : Linking error: LNK2019 & LNK1120 - when creating/connecting a SIGNAL



Guett_31
25th February 2013, 00:17
My goal here is to create a signal "expandParentItem" in my QFileSystemModel subclass so that when a parent item is checked in the associated QtreeView, the QtreeView can automatically expand it to show its children.
I define and I emit the signal like this:

FileSysSelectModel.h

#ifndef FILESYSSELECTMODEL_H
#define FILESYSSELECTMODEL_H

#include<QFileSystemModel>
#include <QSet>
#include <QPersistentModelIndex>
#include <QVariant>

class FileSysSelectModel: public QFileSystemModel
{
private:
QSet<QPersistentModelIndex> m_checkTable;
QVariant m_noData;

void FileSysSelectModel::updateSubMembers(const QModelIndex &index, const QVariant &value);

public:
int FileSysSelectModel::columnCount(const QModelIndex &parent) const;
QVariant FileSysSelectModel::data(const QModelIndex &index, int role) const;
virtual Qt::ItemFlags FileSysSelectModel::flags(const QModelIndex &index) const;
virtual bool FileSysSelectModel::setData(const QModelIndex &index, const QVariant &value, int role);

signals:
void expandParentItem(const QModelIndex &index);
};

#endif // FILESYSSELECTMODEL_H
Function managing the signal in FileSysSelectModel.cpp

//if the current item has children:
// check all the child items if the parent item is checked.
// uncheck all the child items if the parent item is unchecked.
void FileSysSelectModel::updateSubMembers(const QModelIndex &index, const QVariant &value)
{
QModelIndex firstChild = index.child(0,0);
QModelIndex currentChild;
if (firstChild.isValid())
{
if(value == Qt::Checked) emit expandParentItem(index);

int i = 0;
while(1)
{
currentChild = index.child(i,0);
//Recursive call
FileSysSelectModel::updateSubMembers(currentChild, value);
if (!currentChild.isValid()) break;
if(value == Qt::Checked) m_checkTable.insert(currentChild); else m_checkTable.remove(currentChild);
i = i+1;
}
emit dataChanged(firstChild, currentChild);
}
}
I connect the signal to the QtreeView in my main window like this:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

m_fileSystemModel = new FileSysSelectModel;
m_fileSystemModel->setRootPath(QDir::currentPath());
ui->scriptFileView->setModel(m_fileSystemModel);

QObject::connect(m_fileSystemModel,SIGNAL(expandPa rentItem(QModelIndex)),ui->scriptFileView,SLOT(expand(QModelIndex)));
}

MainWindow::~MainWindow()
{
delete ui;
}

When I try to compile, I get the following error message:

FileSysSelectModel.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall FileSysSelectModel::expandParentItem(class QModelIndex const &)" (?expandParentItem@FileSysSelectModel@@QAEXABVQMod elIndex@@@Z) referenced in function "private: void __thiscall FileSysSelectModel::updateSubMembers(class QModelIndex const &,class QVariant const &)" (?updateSubMembers@FileSysSelectModel@@AAEXABVQMod elIndex@@ABVQVariant@@@Z)

debug\Designer_test.exe:-1: error: LNK1120: 1 unresolved externals

I'm new to Qt and I'm having a hard time understanding this error message.
Could someone help me with this issue?

ChrisW67
25th February 2013, 01:28
You need the Q_OBJECT macro in the class declaration. You must rerun qmake after adding this macro.

The code for signals (the bit your linker cannot find) is generated by moc, built and linked into your program courtesy of the Makefile qmake generates. These rules are triggered by the presence of the Q_OBJECT macro, which expands to include some support infrastructure for the Qt metaobject system and generated code.