Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:38:31+00:00 2026-06-18T08:38:31+00:00

I’m aware I need to derive from QObject in order to connect to a

  • 0

I’m aware I need to derive from QObject in order to connect to a slot if I am using QGraphicsPixmapItem, but I am struggling to do this. I have tried alternative ways to achieve what I want, I have tried onMousePress and isSelectable i.e.

run->setFlag(QGraphicsPixmapItem::ItemIsSelectable);

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

although run is selectable, the first argument is never true, it is always “not selected”

This is my code, I am working on the slot method;

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

So obviously I get the error when connecting to the slots. Could anyone help please? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:38:32+00:00Added an answer on June 18, 2026 at 8:38 am

    If you want to use signals and slots, you need to subclass both QObject and QGraphicsPixmapItem.

    Because QObject doesn’t contain clicked() signal, you need to implement that, too, by re-implementing
    void mousePressEvent ( QGraphicsSceneMouseEvent *e ) and void mouseReleaseEvent ( QGraphicsSceneMouseEvent *e ).

    MyItem:

    #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;
    };
    

    And simple example of usage:

    #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!" ;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.