Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QGraphicsSceneContextMenuEvent中的QActions_C++_Qt_Qgraphicsitem - Fatal编程技术网

C++ QGraphicsSceneContextMenuEvent中的QActions

C++ QGraphicsSceneContextMenuEvent中的QActions,c++,qt,qgraphicsitem,C++,Qt,Qgraphicsitem,我想制作一个程序,通过单击我用QGraphicsCeneContextMenuEvent创建的上下文菜单中的选项信息,显示QGraphicsItem宽度和高度的信息。现在我正试图调用一个函数,其中包含一个名为info的qDebug 这是我的密码 dialog.h: #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include "QtCore" #include "QtGui" #include "mysquare.h"

我想制作一个程序,通过单击我用QGraphicsCeneContextMenuEvent创建的上下文菜单中的选项信息,显示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;
}
迈斯夸尔

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

}

要在菜单中单击操作和插槽信息之间建立链接,需要使用信号/插槽机制

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

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_对象宏

还有,有点离题:

使用

但我真的建议您只包含您需要的内容,而不是整个QtCore库

此外,将include放在源文件而不是头文件中,除非不可能

<>不要混合标准的C++库,如

#include <string>
#include "QtCore"
#include <string>
#include <QString>
using namespace std;