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