C++ Using QT Creator

Hello I seem to keep getting random error messages when trying to compile and run my program.

1
2
3
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall mycell::mycell(void)" (??0mycell@@QAE@XZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)

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


this is the error im currently getting.

could anyone be kind enough as to explain what this means.
if it helps here is all my code including header files


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QtCore>
#include "mycell.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene;
    mycell *square;

};

#endif // MAINWINDOW_H 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MYCELL_H
#define MYCELL_H
#include <QPainter>
#include <QGraphicsItem>
#include "mainwindow.h"


class mycell : public QGraphicsItem

{
public:
    mycell();
    // outer edges of the object
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    bool Pressed;

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};

#endif // MYCELL_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MYCELL_H
#define MYCELL_H
#include <QPainter>
#include <QGraphicsItem>
#include "mainwindow.h"


class mycell : public QGraphicsItem

{
public:
    mycell();
    // outer edges of the object
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    bool Pressed;

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};

#endif // MYCELL_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "mycell.h"
#include "mainwindow.h"

mycell::mycell()
{
    Pressed = false;
    setFlag(ItemIsMovable);
}

QRectF mycell::boundingRect() const
{
    return QRectF(0,0,100,100);
}

void mycell::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF rec = boundingRect();
    QBrush brush(Qt::blue);


    if (Pressed){
        brush.setColor(Qt::red) ;
    }
    else{
        brush.setColor(Qt::blue) ;
    }
    painter->fillRect(rec,brush);
    painter->drawRect(rec);
}


void mycell::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Pressed = true;
    update();
    QGraphicsItem::mousePressEvent(event);
}

void mycell::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Pressed = false;
    update();
    QGraphicsItem::mousePressEvent(event);
}

it has something to do with me putting the class my cell into the mainwindow.h file

as it goes when I remove it from there but I don't understand why I cant put it in there?
anyone?
it has something to do with me putting the class my cell into the mainwindow.h file
No, it's a pointer anyway, so there shouldn't be a problem.

Did you add the 'mycell.cpp' or however you named properly to your project
usually, when you use single-inheritance approach for widgets created with Qt designer you would use Ui::MainWindow ui and NOT Ui::MainWindow * ui.

typical usage

1
2
3
4
MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent) {
  ui.setupUi(this);
}


PS: I never used "explicit" for a constructor.... I don't see any reason mainly for a QObject derived class (I remember that widgets are subclass of QObject... QObject subclasses are treated in a very special way so I am not sure it is a good idea to declare an explicit constructor).

----

I don't know if those hints can help you
Last edited on
still haven't managed to get rid of the error if anyone can help
the problem comes up when I call this

1
2
    item = new MyCell();
    scene->addItem(item);
anyone?
I suggest you to DON'T use directly QGraphicItem but to use one of the conveniences available

here is the list, ripped from QGraphicItem documentation page:


QGraphicsEllipseItem provides an ellipse item
QGraphicsLineItem provides a line item
QGraphicsPathItem provides an arbitrary path item
QGraphicsPixmapItem provides a pixmap item
QGraphicsPolygonItem provides a polygon item
QGraphicsRectItem provides a rectangular item
QGraphicsSimpleTextItem provides a simple text label item
QGraphicsTextItem provides an advanced text browser item


For example, in one of my program, I create a class derived from QGraphicPixmapItem to extend some functionalities I specific need for my program (like the chance to change easly the coords where to put my item and chance to change the image associated with it)
Last edited on
Make sure you have mycell.cpp added to your .pro file under the SOURCES: section.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#-------------------------------------------------
#
# Project created by QtCreator 2013-04-05T20:38:52
#
#-------------------------------------------------

QT       += core gui

TARGET = Qtest
TEMPLATE = app

SOURCES += main.cpp\
           mainwindow.cpp \
           mycell.cpp

HEADERS += mainwindow.h \
           mycell.h

FORMS   += mainwindow.ui \
           mycell.ui


If you've done this, what text is in your .pro file?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#-------------------------------------------------
#
# Project created by QtCreator 2013-04-20T20:36:36
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Game
TEMPLATE = app


SOURCES += main.cpp\
        dialog.cpp \
    mycell.cpp

HEADERS  += dialog.h \
    mycell.h

FORMS    += dialog.ui
that's the pro file


im still getting the error

Topic archived. No new replies allowed.