Tuesday, 9 August 2016

c++ - Correct usage of a static function to return QList

I'm new to Qt (as well as to C++) and I don't want to ask a stupid question, but this one is rather to find out the best way of usage (which I'm probably missing at this point). The idea of my code is the following:





  • To have a static function which can be called anytime

  • The idea of this function is to return QList

  • A class Logic shouldn't store the data, instead, the function will create this list (as well as objects inside) based on file content and store it inside a list created in a different part of code



For example:



logic.h




#ifndef LOGIC_H
#define LOGIC_H

#include
#include
#include

class Disk : public QObject {
Q_OBJECT
Q_PROPERTY(QString diskUuid READ diskUuid WRITE setDiskUuid NOTIFY diskUuidChanged)

Q_PROPERTY(ulong diskSizeBytes READ diskSizeBytes WRITE setDiskSizeBytes NOTIFY diskSizeBytesChanged)

public:
Disk (QObject *parent=nullptr);
Disk (const QString &diskUuid,
const ulong &diskSizeBytes,
QObject *parent=nullptr);

QString diskUuid() const;
ulong diskSizeBytes() const;

void setDiskUuid(const QString &diskUuid);
void setDiskSizeBytes (const ulong &diskSizeBytes);

signals:
void diskUuidChanged();
void diskSizeBytesChanged();

private:
QString m_diskUuid;
ulong m_diskSizeBytes;

};

class Logic : public QObject {
Q_OBJECT
public:
explicit Logic(QObject *parent = nullptr);
~Logic();
static QList buildDiskList(QUrl fileUrl);
};


#endif // LOGIC_H


logic.cpp



#include "logic.h"

Disk::Disk(QObject *parent)
: QObject(parent)
{


}

Disk::Disk(const QString &diskUuid,
const ulong &diskSizeBytes,
QObject *parent):QObject(parent),m_diskUuid(diskUuid),m_diskSizeBytes(diskSizeBytes)
{

}


Logic::Logic(QObject *parent) : QObject(parent) {

}

Logic::~Logic() {

}

QString Disk::diskUuid() const{
return m_diskUuid;

}

ulong Disk::diskSizeBytes() const{
return m_diskSizeBytes;
}

void Disk::setDiskUuid(const QString &diskUuid){
if (diskUuid != m_diskUuid) {
m_diskUuid = diskUuid;
emit diskUuidChanged();

}
}

void Disk::setDiskSizeBytes(const ulong &diskSizeBytes){
if (diskSizeBytes != m_diskSizeBytes) {
m_diskSizeBytes = diskSizeBytes;
emit diskSizeBytesChanged();
}
}


QList buildDiskList(QUrl fileUrl) {
QList list;
list.append(new Disk("",0));
return list;
}


But, when I'm trying to do something like this in another part of the code (inside a void of a different .cpp), like this:



QList list;

list = Logic::buildDiskList(logUrl);





chaos.obj:-1: error: LNK2019: unresolved external symbol "public: static class QList __cdecl Logic::buildDiskList(class QUrl)" (?buildDiskList@Logic@@SA?AV?$QList@PEAVDisk@@@@VQUrl@@@Z) referenced in function "public: void __cdecl LogsModel::appendLogs(class QString,class QList)" (?appendLogs@LogsModel@@QEAAXVQString@@V?$QList@VQUrl@@@@@Z)
release\clc.exe:-1: error:
LNK1120: 1 unresolved externals



Looks like I do it wrong. Is there a way to do something like this, or do I need to create object for Logic class explicitly? Thanks in advance!

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...