找回密码
 立即注册
收起左侧

自己建立的新类加入Q_OBJECT想增加信号函数,出错?

15
回复
8942
查看
[复制链接]
累计签到:168 天
连续签到:1 天
来源: 2013-9-24 21:19:08 显示全部楼层 |阅读模式
1Qter豆

最佳答案

查看完整内容

是继承 比如 class Widget : public QObject 这样才是继承 你这样只是包含头文件而已
回复

使用道具 举报

累计签到:410 天
连续签到:1 天
2013-9-24 21:19:09 显示全部楼层
cll 发表于 2013-9-25 10:10
增加了#include 还是显示错误,是这个意思吗?

是继承
比如
class Widget : public QObject 这样才是继承
你这样只是包含头文件而已
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-24 21:20:54 显示全部楼层
shape.o: In function `Shape::Shape()':
shape.cpp.text+0x6): undefined reference to `vtable for Shape'
line.o.rodata._ZTI4Line[typeinfo for Line]+0x10): undefined reference to `typeinfo for Shape'
line.o.rodata._ZTV4Line[vtable for Line]+0x8): undefined reference to `Shape::metaObject() const'
line.o.rodata._ZTV4Line[vtable for Line]+0xc): undefined reference to `Shape::qt_metacast(char const*)'
line.o.rodata._ZTV4Line[vtable for Line]+0x10): undefined reference to `Shape::qt_metacall(QMetaObject::Call, int, void**)'
rect.o.rodata._ZTI4Rect[typeinfo for Rect]+0x10): undefined reference to `typeinfo for Shape'
rect.o.rodata._ZTV4Rect[vtable for Rect]+0x8): undefined reference to `Shape::metaObject() const'
rect.o.rodata._ZTV4Rect[vtable for Rect]+0xc): undefined reference to `Shape::qt_metacast(char const*)'
rect.o.rodata._ZTV4Rect[vtable for Rect]+0x10): undefined reference to `Shape::qt_metacall(QMetaObject::Call, int, void**)'
ellipseitem.o.rodata._ZTI11EllipseItem[typeinfo for EllipseItem]+0x10): undefined reference to `typeinfo for Shape'
ellipseitem.o:(.rodata._ZTV11EllipseItem[vtable for EllipseItem]+0x8): undefined reference to `Shape::metaObject() const'
ellipseitem.o:(.rodata._ZTV11EllipseItem[vtable for EllipseItem]+0xc): undefined reference to `Shape::qt_metacast(char const*)'
ellipseitem.o:(.rodata._ZTV11EllipseItem[vtable for EllipseItem]+0x10): undefined reference to `Shape::qt_metacall(QMetaObject::Call, int, void**)'
collect2: ld returned 1 exit status
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-24 21:22:41 显示全部楼层
这是错误的位置::
#ifndef SHAPE_H
#define SHAPE_H
#include <QtGui>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>

class Shape
{
Q_OBJECT
public:
    enum Code
    {
        Line,
        Rect,
        Ellipse

    };

    Shape();

    virtual void startDraw(QGraphicsSceneMouseEvent *event) = 0;
    virtual void drawing(QGraphicsSceneMouseEvent *event) = 0;
    virtual QPointF reTurnStartPoint() = 0;//返回起始点用于存于文件中
    virtual QPointF reTurnEndPoint() = 0;
    virtual int reTurenShape() = 0 ;
    virtual void storeStartPoint(int p1,int p2) = 0;//从文件中读取的信息
    virtual void storeEndPoint(int p1,int p2) = 0;
    virtual void reDrawing() = 0;
    virtual void setZvalue(int m_Zvalue) = 0;//设置Z值
//signals:
//   virtual void sendStartPointF(QPointF startPointF);
//   virtual void sendEndPointF(QPointF endPointF);


    //virtual void storeShape(int shape) = 0;

   // virtual void end(QGraphicsSceneMouseEvent)
};

#endif // SHAPE_H
就增加个Q_OBJECT,然后就出错了
回复

使用道具 举报

尚未签到

2013-9-24 23:10:47 显示全部楼层
继承QObject类
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-25 10:10:33 显示全部楼层
Joey_Chan 发表于 2013-9-24 23:10
继承QObject类

增加了#include <QObject>还是显示错误,是这个意思吗?

点评

是继承 比如 class Widget : public QObject 这样才是继承 你这样只是包含头文件而已  详情 回复 发表于 2013-9-25 10:35
回复

使用道具 举报

累计签到:410 天
连续签到:1 天
2013-9-25 10:34:50 显示全部楼层
你这个类 是纯虚类,不能直接用,必须继承,并且把纯虚函数都实现了才能用
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-25 10:39:58 显示全部楼层
a408815041 发表于 2013-9-25 10:34
你这个类 是纯虚类,不能直接用,必须继承,并且把纯虚函数都实现了才能用 ...

大神,具体点呗,我菜鸟呀,我不明白我要怎样去继承和要实现哪些虚函数,
下面这个就可以实现信号与槽机制,好像就多了个Q-OBJECT?
#ifndef PAINTWIDGET_H
#define PAINTWIDGET_H

#include <QGraphicsScene>
#include <QMouseEvent>
#include "shape.h"
#include "line.h"
#include "rect.h"
#include <QtGui>
#include <QtDebug>
#include <QGraphicsItem>
#include <QColor>
#include "ellipseitem.h"
#include "store.h"

class PaintWidget : public QGraphicsScene
{
    Q_OBJECT
public:
    PaintWidget(QWidget *parent = 0);
    enum addColor
    {
        red,
        black,
        white,
        blue,
        green,
        yellow
    };

    enum Contral
    {
        move,
        draw
    };
    enum Action
    {
        fangda,
        suoxiao,
        xuanzhuan,
        shanchu
    };

public slots:
    void setCurrentShape(Shape::Code s);
    void setmoveCotral(PaintWidget::Contral c);
    void setAction(PaintWidget::Action a);
    void getScenePos(QPointF p);
    void getSceneEndPos(QPointF endscenepos);
    void setAddColor(PaintWidget::addColor d);
    void getCunChuSLot();

    void getStartPointFfromItem(QPointF startPointF);//得到从item穿过来的两个点进行存储和再现
    void getEndPointFfromItem(QPointF endPointF);

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

private:
    Shape::Code currentShapeCode;
    Shape *currentItems;
    bool perm;
    bool contralmove;
  //  QPoint pot;
    QPointF scenePos;
    QPointF m_sceneEndPos;
    QColor tempColor;

    QPointF m_startPointFfromItem,m_endPointFfromItem;


    QList<Shape*> shapeList;

    store *cls_store;
    int m_desideShape;
   // QGraphicsItem *itemForcus;

};

#endif // PAINTWIDGET_H
回复

使用道具 举报

累计签到:1569 天
连续签到:1 天
2013-9-25 16:16:32 显示全部楼层
cll 发表于 2013-9-25 10:39
大神,具体点呗,我菜鸟呀,我不明白我要怎样去继承和要实现哪些虚函数,
下面这个就可以实现信号与槽机 ...

这个不是多一个QOBJECT那么简单,因为它继承自QGraphicsScene,而QGraphicsScene继承自QObject啊。
回复

使用道具 举报

累计签到:1569 天
连续签到:1 天
2013-9-25 16:22:12 显示全部楼层
你可以参考《Qt及Qt Quick开发实战精解》第二章的 方块游戏 源码。在下载页面可以下载。

里面就是自定义的item,而且实现了信号和槽。这个简单来说就是需要继承自QGraphicsObject
回复

使用道具 举报

累计签到:4 天
连续签到:1 天
2013-9-25 16:33:36 显示全部楼层
建议先去看c plus plus吧!
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-27 10:35:37 显示全部楼层
study_wen 发表于 2013-9-25 16:33
建议先去看c plus plus吧!

好多呀,具体点吧,我是应该看哪部分内容呢
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-27 10:41:02 显示全部楼层
yafeilinux 发表于 2013-9-25 16:22
你可以参考《Qt及Qt Quick开发实战精解》第二章的 方块游戏 源码。在下载页面可以下载。

里面就是自定义的 ...

thanks very much,我这就去看看
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-27 10:55:54 显示全部楼层
a408815041 发表于 2013-9-25 10:35
是继承
比如
class Widget : public QObject 这样才是继承

谢谢指导,I got your meaning!
回复

使用道具 举报

累计签到:4 天
连续签到:1 天
2013-9-27 13:49:45 显示全部楼层
cll 发表于 2013-9-27 10:35
好多呀,具体点吧,我是应该看哪部分内容呢

看最基本的吧...类继承!
回复

使用道具 举报

累计签到:168 天
连续签到:1 天
2013-9-27 20:16:59 显示全部楼层
study_wen 发表于 2013-9-27 13:49
看最基本的吧...类继承!

我知道了,是从最基本的看起。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了