0

I'm trying to create a custom graphics item for my QGraphicsScene, and when I attempt to construct the item, I get the error mentioned in the title. Here's how I set things up:

Top level window.

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

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

    this->scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setSceneRect(0, 0, 640, 640);

    this->button = new EllipseButton::EllipseButton(20, 20);
    this->scene->addItem(this->button);

}

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

EllipseButton Header

#ifndef ELLIPSEBUTTON_H
#define ELLIPSEBUTTON_H

#include <QGraphicsEllipseItem>
#include <QPainter>

class EllipseButton : public QGraphicsEllipseItem
{
public:
    EllipseButton();
    EllipseButton(int x, int y);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    bool pressed;
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
    int x;
    int y;
};

#endif // ELLIPSEBUTTON_H

EllipseButton Class Definition

#include "ellipsebutton.h"

EllipseButton::EllipseButton(){

    this->x = 0;
    this->y = 0;
    this->pressed = false;

}

EllipseButton::EllipseButton(int x, int y){

    this->x = x;
    this->y = y;
    this->pressed = false;

}

void EllipseButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    QPen pen(Qt::red, 1);
    painter->setPen(pen);

    if (pressed){

        pen.setColor(Qt::blue);

    } else {

        pen.setColor(Qt::red);

    }

    painter->drawEllipse(this->x, this->y, 20, 20);

}

void EllipseButton::mousePressEvent(QGraphicsSceneMouseEvent *event){

    this->pressed = true;
    update();
    QGraphicsEllipseItem::mousePressEvent(event);

}

void EllipseButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){

    this->pressed = false;
    update();
    QGraphicsEllipseItem::mouseReleaseEvent(event);

}

So in the main window, I have a private member of the type EllipseButton, which I instatiate by calling

this->button = new EllipseButton::EllipseButton(20, 20);

but this throws the error, and I have also tried the constructor without the classname beforehand, and then I get the following:

Change:

this->button = new EllipseButton(20, 20);

Results in:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl EllipseButton::EllipseButton(int,int)" (??0EllipseButton@@QEAA@HH@Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)

In the end, I'm not quite sure what is wrong with my constructor, since that seems to be the issue.

Edit: Added mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include "ellipsebutton.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene;
    EllipseButton *button;
};

#endif // MAINWINDOW_H
Andrew Lalis
  • 894
  • 3
  • 15
  • 26
  • Have you tried: `new EllipseButton(20, 20);` instead? – Hayt Oct 05 '16 at 10:35
  • Yes, that's what I meant by saying I tried the constructor without the classname beforehand. It then throws the linker error at the bottom of my post. – Andrew Lalis Oct 05 '16 at 10:36
  • OK just for the future: you should post the error which matches the code you are posting. If you add errors for code which is not the one shown here that will confuse people. – Hayt Oct 05 '16 at 10:39
  • 1
    are you sure your ellipsebutton.cpp (or how it is called) does get compiled? – Hayt Oct 05 '16 at 10:41
  • What is the content of `mainwindow.h`? How are you compiling (tools, options, ...)? – rocambille Oct 05 '16 at 10:42
  • Actually, now that you mention it, I do not see an ellipsebutton.o in the project directory. – Andrew Lalis Oct 05 '16 at 10:43
  • I'm using MSVC_64bit 2015 as the compiler. – Andrew Lalis Oct 05 '16 at 10:44
  • Do you have `ellipsebutton.cpp` in the source files of your MSVC project? Did you create the project by yourself, or did you generate it using qmake, or cmake, or something else? – rocambille Oct 05 '16 at 10:45
  • @AndrewLalis then this is the problem of your undefined references. You class does not get compiled. – Hayt Oct 05 '16 at 10:46

0 Answers0