QGraphicsSceneContextMenuEvent中的QActions

时间:2015-01-08 16:43:36

标签: c++ qt qgraphicsitem

我想通过在我使用QGraphicsSceneContextMenuEvent创建的上下文菜单中单击选项“info”来创建一个显示QGraphicsItem(宽度和高度)信息的程序。现在我只是试图用一个名为info的qDebug调用一个函数。

这是我的代码

dialog.h:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "QtCore"
#include "QtGui"
#include "mysquare.h"
namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

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

private:
    Ui::Dialog *ui;
    QGraphicsScene *scene;
    MySquare *square;

};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QWidget>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    square=new MySquare();
    scene->addItem(square);

}

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

mysquare.h

#ifndef MYSQUARE_H
#define MYSQUARE_H
#include <QPainter>
#include <QGraphicsItem>
#include <QDebug>
#include <QMenu>

class MySquare: public QGraphicsItem
{

public:
    MySquare();
    QRectF boundingRect() const;
    void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QMenu *menu;
    QAction *heightAct;
    QAction *widthAct;
    QAction *color;

private slots:
    void info();

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);

};

#endif // MYSQUARE_H

mysquare.cpp

#include "mysquare.h"
#include<QMenu>
#include <string>
using namespace std;
MySquare::MySquare()
{

}

QRectF MySquare::boundingRect() const
{
    return QRectF(100,100,50,50);
}

void MySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF rec  = boundingRect();
    QBrush brushy(Qt::green);
    painter->fillRect(rec,brushy);
    painter->drawRect(rec);

}

void MySquare::info()
{
    qDebug()<<"HERE'S MY INFO!!!";
}


void MySquare::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{

    QString height = "Height: "+QString::number(boundingRect().height());
    QString width = "Width: "+QString::number(boundingRect().width());

    menu = new QMenu();
    heightAct = menu->addAction(height);
    widthAct = menu->addAction(width);
    color = menu->addAction("Change color");

    menu->exec(QCursor::pos());

}

1 个答案:

答案 0 :(得分:0)

要在点击菜单中的操作和插槽信息()之间建立“链接”,您需要使用信号/插槽机制。

您只需在QMenu初始化中添加QObject :: connect(...)。

void MySquare::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    QString height = "Height: "+QString::number(boundingRect().height());
    QString width = "Width: "+QString::number(boundingRect().width());

    menu = new QMenu();
    heightAct = menu->addAction(height);
    widthAct = menu->addAction(width);
    color = menu->addAction("Change color");

    QObject::connect(heightAct, SIGNAL(triggered()), this, SLOT(info()));

    menu->exec(QCursor::pos());
}

但是,上面的代码不会编译,因为信号/槽机制只能用于QObject的子类,而QGraphicsItem则不能。所以你必须将它改为QGraphicsObject。 (不要忘记Q_OBJECT宏)

此外,还有一点超出主题

使用

#include <QtCore>

而不是

#include "QtCore"

但我真的建议你只包括你需要的东西,而不是整个QtCore库。

此外,将您的包含在源文件而不是头文件中,除非它不可能。

不要混用标准C ++库,例如

#include <string>

除非绝对需要。在这种情况下,您必须包括

#include <QString>

只需删除

即可
using namespace std;