0

我知道如果我使用 QGraphicsPixmapItem,我需要从 QObject 派生才能连接到插槽,但我正在努力做到这一点。我尝试过其他方法来实现我想要的,我尝试过 onMousePress 和 isSelectable 即

run->setFlag(QGraphicsPixmapItem::ItemIsSelectable);

if (run->isSelected())
{
    qDebug() << "selected";
}
else if (!run->isSelected())
{
    qDebug() << "not selected";
}

虽然 run 是可选择的,但第一个参数永远不会是真的,它总是“未选择”

这是我的代码,我正在研究 slot 方法;

mainwindow.cpp

int MainWindow::sim()
{

...
    QGraphicsPixmapItem* run = new QGraphicsPixmapItem(QPixmap::fromImage(image6));
    run->scale(0.3,0.3);
    run->setPos(-200,-200);
    run->setFlag(QGraphicsPixmapItem::ItemIsSelectable);
    run->setCursor(Qt::PointingHandCursor);
    connect(run, SIGNAL(selectionChanged()), this, SLOT(runClicked()));
    scene->addItem(run);

    //pause 
    QGraphicsPixmapItem* pause = new QGraphicsPixmapItem(QPixmap::fromImage(image7));
    pause->scale(0.3,0.3);
    pause->setPos(-160,-197);
    pause->setFlag(QGraphicsPixmapItem::ItemIsSelectable);
    pause->setCursor(Qt::PointingHandCursor);
    connect(pause, SIGNAL(selectionChanged()), this, SLOT(pauseClicked()));
    scene->addItem(pause);
...

}

void MainWindow::runClicked()
{
    qDebug() << "run Clicked";
}

void MainWindow::pauseClicked()
{
    qDebug() << "pause Clicked";
}

mainwindow.h

#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();
    int sim();
...

public slots:
    void runClicked();
    void pauseClicked();
...

所以很明显我在连接到插槽时遇到了错误。有人可以帮忙吗?谢谢你。

4

2 回答 2

2

要确定您的项目是否被选中,请执行以下操作:

QVariant MyItem::itemChange( GraphicsItemChange change, const QVariant& value )
{
    if ( change == QGraphicsItem::ItemSelectedHasChanged ) {
        qDebug() << ( isSelected() ? "selected" : "not selected" );
    }

    return QGraphicsItem::itemChange( change, value );
}
于 2013-02-05T12:55:08.970 回答
1

如果你想使用信号和槽,你需要继承QObjectQGraphicsPixmapItem

因为QObject不包含clicked()信号,所以您也需要通过重新实现
void mousePressEvent ( QGraphicsSceneMouseEvent *e )and来实现它void mouseReleaseEvent ( QGraphicsSceneMouseEvent *e )

我的项目:

#pragma once

#include <QGraphicsPixmapItem>
#include <qobject.h>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>

class MyItem:  public QObject, public QGraphicsPixmapItem 
               /* moc.exe requires to derive from QObject first! */
{
    Q_OBJECT
public:
    MyItem(QGraphicsItem *parent = 0): QObject(), QGraphicsPixmapItem(parent)
    {

    }

    MyItem(const QPixmap & pixmap, QGraphicsItem * parent = 0 ): QObject(), 
                                                                 QGraphicsPixmapItem(pixmap, parent)
    {

    }

signals:
    void clicked();

protected:
    // re-implement processing of mouse events
    void mouseReleaseEvent ( QGraphicsSceneMouseEvent *e )
    {
        // check if cursor not moved since click beginning
        if ((m_mouseClick) && (e->pos() == m_lastPoint))
        {
            // do something: for example emit Click signal
            emit clicked();
        }
    }

    void mousePressEvent ( QGraphicsSceneMouseEvent *e )
    {
        // store click position
        m_lastPoint = e->pos();
        // set the flag meaning "click begin"
        m_mouseClick = true;
    }

private:
    bool m_mouseClick;
    QPointF m_lastPoint;
};

以及简单的用法示例:

#include <qgraphicsview.h>
#include <qgraphicsscene.h>
#include "reader.h"
#include <qdebug.h>
class MainAppClass: public QObject
{
    Q_OBJECT
public:
    MainAppClass()
    {
         QGraphicsScene *scene = new QGraphicsScene();;
         scene->setSceneRect( -100.0, -100.0, 200.0, 200.0 );

         MyItem *item = new MyItem(QPixmap("about.png"));
         connect(item, SIGNAL(clicked()), this, SLOT(pixmapClicked()));
         scene->addItem(item);

         QGraphicsView * view =  new QGraphicsView( scene );
         view->setRenderHints( QPainter::Antialiasing );
         view->show();
    }

public slots:
    void pixmapClicked()
    {
        qDebug() << "item clicked!" ;
    }
};
于 2013-02-05T13:22:34.907 回答