Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9111073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:25:13+00:00 2026-06-17T03:25:13+00:00

Situation : I have a Dialog class in Qt on which I draw a

  • 0

Situation:
I have a Dialog class in Qt on which I draw a raster of squares. The squares are implemented in the MySquare class (MySquare: QGraphicsItem).

Question:
I want to signal the Dialog slot setTarget() that a square was clicked (and obviously I want to give it some information about that square like for example it’s x and y coordinates with it later). However I get an ‘undefined reference to ‘MySquare::targetChanged()‘ error. I have looked for a solution but have not found any; anybody an idea?

edit: I have added a Q_OBJECT macro inside the MySquare however the error does not dissapear and I get an additional “‘undefined reference to ‘vtable for MySquare()‘ error

dialog.h

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);

  ~Dialog();


public slots:
    void setTarget();

private:
    Ui::Dialog *ui;
    QGraphicsScene *scene;
};

dialog.cpp

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

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    MySquare *item;

    item = new MySquare(30,30,30,30);
    QObject::connect(item,SIGNAL(targetChanged()),this,SLOT(setTarget()));
}

void Dialog::setTarget(){
    qDebug() << "signal recieved" << endl;
}

mysquare.h

#include <QGraphicsItem>
#include <QPainter>
#include <QDebug>
#include <QKeyEvent>
#include <QObject>

class MySquare : public QGraphicsItem, public QObject
{

Q_OBJECT

public:
    MySquare(int x,int y,int h, int w);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    int x,y,h,w;

signals:
    void targetChanged();

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

mysquare.cpp

#include "mysquare.h"
#include <QGraphicsSceneDragDropEvent>
#include <QWidget>

MySquare::MySquare(int _x,int _y, int _w, int _h)
{

    setAcceptDrops(true);
    color=Qt::red;
    color_pressed = Qt::green;
    x = _x;
    y = _y;
    w = _w;
    h = _h;
}


QRectF MySquare::boundingRect() const
{
    return QRectF(x,y,w,h); 
}


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

        if (Pressed){
            brush.setColor(color);
        } else {
            brush.setColor(color_pressed);
        }

    painter->fillRect(rec,brush);
    painter->drawRect(rec);
}


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

    emit targetChanged();
}



void MySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Pressed=false;
    update();
    QGraphicsItem::mouseReleaseEvent(event);
    qDebug() << "mouse Released";
}

void MySquare::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    qDebug() << "mouse Moved";
    QDrag *drag = new QDrag(event->widget());
    QMimeData *mime = new QMimeData;
    drag->setMimeData(mime);
    drag->exec();
}


void MySquare::keyPressEvent(QKeyEvent *event){
    //out of bounds check?
    int x = pos().x();
    int y = pos().y();

    QGraphicsItem::keyPressEvent(event);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:25:15+00:00Added an answer on June 17, 2026 at 3:25 am

    Edit: If you have an undefined reference to a vtable, then it’s probably because you did not implement certain virtual functions. Have you implemented the mouse event handlers of the MySquare class somewhere? Have you also implemented the boundingRect() and paint() functions of the MySquare class?

    Old answer:
    You need to write the Q_OBJECT macro after the opening ‘{‘ in the MySquare class. Also Qt will complain about multiple inheritance at some point. Therefore, instead of inheriting from QGraphicsItem and QObject, inherit from QGraphicsObject instead.

    The actual reason, the linker complains about a missing function definition is the following: When you put a Q_OBJECT macro into the right place, then the Qt Meta Object Compiler (MOC) will generate a new cpp file for the project which contains the definition of the signal functions of the respective class. So Qt implements a function for every signal for you. If you don’t insert the Q_OBJECT macro, then MOC won’t do anything for you and the function definition for the signal will be missing to the linker.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation : I have a Dialog class in QT on which I draw a
I have following situation - Have a MongoService class, which reads host, port, database
I have this situation: http://jsfiddle.net/bRDgK/3/ In this situation I have a modal dialog with
Following situation: I have a JFrame and call JOptionPane.showInputDialog(test) . The modal dialog will
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
Situation: I have an index page which loads pages into it with ajax as
i have situation like this: class IData { virtual void get() = 0; virtual
I have a situation where I want to confirm a user is still allowed
So, I'll explain my situation. I have a Service which is catching the events
I am in a situation. I have a dropdownlist inside jquery dialog. On click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.