请教一个QTimer::singleShot的问题
使用QTimer::singleShot定义一个信号与槽:
QTimer::singleShot(3000,this,SLOT(ShowMenu()));
这里的意思是等待3妙过后,触发ShowMenu()这个函数。(当然这句不是我写的,我从别人拿拿来学习的)
使用的地方:
void MyGraphicsView::mousePressEvent(QMouseEvent *qme)
{
QGraphicsView::mousePressEvent(qme);
qDebug()<<"mousePressEvent";
point=this->cursor().pos();
QTimer::singleShot(3000,this,SLOT(ShowMenu()));
}
ShowMenu()这个函数也有定义和内容:
在h文件中:
private slots:
void ShowMenu();
在cpp文件中:
void MyGraphicsView::ShowMenu()
{
QCursor cur = this->cursor();
QPoint pp=cur.pos();
if(qAbs(pp.x()-point.x())>5 || qAbs(pp.y()-point.y())>5)
{
point=pp;
return;
}
menu->exec(cur.pos());
}
h和cpp的名字是 MyGraphicsView。
但是 运行时终端上会显示:
mousePressEvent (这句是 qDebug()输出)
Object::connect: No such slot QGraphicsView::ShowMenu()
大家帮看看 会是什么原因呢
=========================================
下面我把两个文件的源码贴上来:
mygraphicsview.h
#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H
#include <QTimer>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDebug>
#include <QMouseEvent>
#include <QMenu>
#include <QCursor>
#include <QPoint>
#include <QtGlobal>
#include <QTextCodec>
class MyGraphicsView:public QGraphicsView
{
public:
MyGraphicsView(QGraphicsScene *scene,QWidget *parent =0);
QMenu *menu;
QAction *actLoca;
QAction *actDest;
QAction *actNavg;
QAction *actCacl;
QPoint point;
protected:
void mouseDoubleClickEvent(QMouseEvent *qme);
void mousePressEvent(QMouseEvent *qme);
private slots:
void ShowMenu();
};
#endif // MYGRAPHICSVIEW_H
mygraphicsview.cpp
#include "mygraphicsview.h"
MyGraphicsView::MyGraphicsView(QGraphicsScene *scene,QWidget *parent):QGraphicsView(scene,parent)
{
menu = new QMenu(this);
actLoca = new QAction("定位",this);
actDest = new QAction("目标",this);
actNavg = new QAction("导航",this);
actCacl = new QAction("取消",this);
menu->addAction(actLoca);
menu->addAction(actDest);
menu->addAction(actNavg);
menu->addAction(actCacl);
}
void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent *qme)
{
QGraphicsView::mouseDoubleClickEvent(qme);
qDebug()<<"mouseDoubleClickEvent";
}
void MyGraphicsView::mousePressEvent(QMouseEvent *qme)
{
QGraphicsView::mousePressEvent(qme);
qDebug()<<"mousePressEvent";
point=this->cursor().pos();
QTimer::singleShot(3000,this,SLOT(ShowMenu()));
}
void MyGraphicsView::ShowMenu()
{
QCursor cur = this->cursor();
QPoint pp=cur.pos();
if(qAbs(pp.x()-point.x())>5 || qAbs(pp.y()-point.y())>5)
{
point=pp;
return;
}
menu->exec(cur.pos());
}
[解决办法]
头文件中丢掉了
Q_OBJECT
[解决办法]
Q_OBJECT