QGraphicsSceneContextMenuEvent 中的 QAction

QActions in QGraphicsSceneContextMenuEvent

我想创建一个程序来显示 QGraphicsItem 的信息(宽度和高度),方法是单击我使用 QGraphicsSceneContextMenuEvent 创建的上下文菜单中的选项 "info"。现在我只是想调用一个带有 qDebug 的函数,名为 info。

这是我的代码

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());

}

要在单击菜单中的操作和插槽信息 () 之间创建 "link",您需要使用 signal/slot 机制。

您只需在 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());
}

但是,上面的代码将无法编译,因为 signal/slot 机制只适用于 QObject 的子类,而 QGraphicsItem 则不行。因此,您必须将其更改为 QGraphicsObject。 (不过不要忘记 Q_OBJECT 宏)

还有一点跑题了:

使用

#include <QtCore>

而不是

#include "QtCore"

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

此外,除非不可能,否则请将包含文件而不是头文件放在源文件中。

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

#include <string>

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

#include <QString>

只需删除

using namespace std;