Unable to call Qrect class into my main dialog



  • dialog.obj:-1: error: LNK2001: unresolved external symbol "public: __thiscall MyCell::MyCell(void)" (??0MyCell@@QAE@XZ)

    [code]#ifndef MYCELL_H
    #define MYCELL_H
    #include <QDialog>
    #include <QtCore>
    #include <QtGui>
    #include <QPainter>
    #include <QGraphicsItem>
    #include <QGraphicsScene>
    #include <QWidget>
    #include <QPaintDevice>

    class MyCell : public QGraphicsItem
    {
    public:
    MyCell();
    QRectF boundingRect()const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

    protected:
    //how many phases or step to advance
    void advance(int phase);

    private:
    qreal angle;
    qreal speed;
    void DoCollision();

    };[/code]

    that's the header

    and this is the main dialog

    [code]#include "dialog.h"
    #include "ui_dialog.h"

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

    scene = new QGraphicsScene(this);
    ui ->graphicsView->setScene(scene);
    
    
    //Anti Aliasing   -- smooths it out.
    ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    
    // set size of the scene within the view
    // 0 is centere
    scene->setSceneRect(-200,-200,300,300);
    
    
    QPen mypen = QPen(Qt::red);
    

    // sets a line that goes around the scene rect
    // the 4 will make a box.
    QLineF TopLine(scene->sceneRect().topLeft(),scene->sceneRect().topRight());
    QLineF LeftLine(scene->sceneRect().topLeft(),scene->sceneRect().bottomLeft());
    QLineF RightLine(scene->sceneRect().topRight(),scene->sceneRect().bottomRight());
    QLineF BottomLine(scene->sceneRect().bottomLeft(),scene->sceneRect().bottomRight());

    // makes the above lines show
    
    scene->addLine(TopLine,mypen);
    scene->addLine(LeftLine,mypen);
    scene->addLine(RightLine,mypen);
    scene->addLine(BottomLine,mypen);
    
    MyCell *item = new MyCell();
    
    
    timer = new QTimer(this);
    //scenes advance slot notifys objects its time to advance ( noo need to click)
    connect(timer, SIGNAL(timeout()),scene,SLOT(advance()));
    //milliseconds
    timer->start(100);
    

    }

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

    [/code]

    error comes because of this bit of code

    [code] MyCell *item = new MyCell();
    [/code]


  • Moderators

    You didn't show two important parts of the code - mycell.cpp and dialog.h

    First - linker can't find the definition of MyClell constructor. Do you have it in mycell.cpp or did you forgot it?

    Second - since you're using MyCell in Dialog, you should have an include for "mycell.h" somewhere. You don't have it in dialog.cpp so do you have it in the dialog.h or did you forgot it?

    Btw. Why do you have so many includes in the mycell.h? You're not using all of them and some of the things like class pointers you can forward-declare. Keeping includes clean will greatly benefit your compile times when your project grows.



  • [code]#ifndef DIALOG_H
    #define DIALOG_H
    #include <QDialog>
    #include <QtCore>
    #include <QtGui>
    #include <QPainter>
    #include <QGraphicsItem>
    #include <QGraphicsScene>
    #include <QWidget>
    #include <QDebug>
    #include "mycell.h"

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

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

    private:
    Ui::Dialog *ui;
    QGraphicsScene *scene;
    QTimer *timer;
    MyCell *item;

    };

    #endif // DIALOG_H
    [/code]
    that's dialog

    [code]
    #include "mycell.h"

    MyCell::MyCell()
    {
    //rndom rotation
    // q rand makes a random number
    // modulus of upto 360

        angle = (qrand() % 360);
        setRotation(angle);
    
        //set speed
        // how many pixels at a time per advance.
        speed = 5;
    
        //randm position
        // x position
        int StartX = 0;
        // y pos
        int StartY = 0;
        // randomly true or false.
        if ((qrand() % 1)){
            // random x and y between 0 and 200
            StartX = (qrand() % 200);
             StartY = (qrand() % 200);
        }
        else{
            StartX = (qrand() % -100);
            StartY = (qrand() % -100);
    
        }
    
        setPos(mapToParent(StartX,StartY));
    

    }

    QRectF MyCell::boundingRect() const
    {
    //max size of the rect;
    return QRect(0,0,20,20);
    }

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

        // collision detection
        //scene tells you which items are collidiing
        //if empty means no collision
        if (scene()->collidingItems(this).isEmpty()){
    
            // no collision
            Brush.setColor(Qt::yellow);
        }
        else  {
            // there is collision
            Brush.setColor(Qt::red);
            // call do colision
            DoCollision();
        }
    
        painter->fillRect(rec,Brush);
        painter->drawRect(rec);
    

    }

    void MyCell::advance(int phase)
    {
    if(!phase) return;

        //this tells us where the object is.
        QPointF location = this->pos();
        //maps current coords to the scenes x and y coordinates.
        setPos(mapToParent(0,-(speed)));
    

    }

    void MyCell::DoCollision()
    {
    //Get a new position

    //Change the angle with a little randomness
    if(((qrand() %1)))
    {
        setRotation(rotation() + (180 + (qrand() % 10)));
    }
    else
    {
        setRotation(rotation() + (180 + (qrand() % -10)));
    }
    
    //see if the new position is in bounds
    QPointF newpoint = mapToParent(-(boundingRect().width()), -(boundingRect().width() + 2));
    
    if(!scene()->sceneRect().contains((newpoint)))
    {
        //move it back in bounds
        newpoint = mapToParent(0,0);
    }
    else
    {
        //set the new position
        setPos(newpoint);
    }
    

    }
    [/code]



  • I do not see Q_OBJECT macro in the code.
    Also do not see #include <QStyleOptionGraphicsItem> in the source.



  • what is the q object macro?



  • also none of this has gotten rid of the error



  • Sorry you do not need the Q_OBJECT macro in MyCell as QGraphicsItem is NOT derived from QObject. (Dialog is derived from QObject -as QDialog is - and the definition of the class begins with the Q_OBJECT macro).
    I have tried to compile and link your code with no error.
    Is there more code?, if it is the case try just what you have sent, (you should also include the a ui file that include a QGraphicView).
    If the code still do not compile then you should try an example from Qt examples (one with QWidgets and QGraphicsView should be fine). If it do not compiles then check your Qt installation. If it compiles ok then it should be something in your code.



  • this code is more or less from copy and paste from an online tutorial. im pretty sure its something to do with my qt creator as I had the same problem with another file where I tried to use my own qgraphics item class but it didn't work and then I made a new one in a different folder d drive and it worked but for this it wont? if any of that makes sense



  • btw im using windows 8 if that may have anything to do with it .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.