我知道如果我使用 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();
...
所以很明显我在连接到插槽时遇到了错误。有人可以帮忙吗?谢谢你。