Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem mouse Press event return
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem mouse Press event return

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Ducky
    wrote on last edited by
    #1

    Hi all,

    I am stuck at a peace of coding.

    I have created two square's using QRectF in a QGraphicsview.

    What i am trying to do is when a square is pressed a label outside the QGraphicsview shows a identification item of that square.

    so if I press the right square it sets the label to "2 or right or 1234".

    My square.CPP is
    @#include "square.h"

    Square::Square(int x, int y)
    {
    X= x;
    Y= y;
    Pressed = false;
    setFlag(ItemIsSelectable);
    }

    QRectF Square::boundingRect() const
    {
    return QRectF(X,Y,100,100);

    }

    void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {

    QRectF rec = boundingRect();
    QBrush brush(Qt::blue);
    
    if(Pressed)
    

    {
    brush.setColor(Qt::red);
    }
    else
    {
    brush.setColor(Qt::green);
    }

    painter->fillRect(rec,brush);
    painter->drawRect(rec);
    

    }

    void Square::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {

    Pressed = true;
    update();
    QGraphicsItem::mousePressEvent(event);
    

    }

    void Square::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
    Pressed = false;
    update();
    QGraphicsItem::mouseReleaseEvent(event);
    }@

    my mainwindow.ccp is.

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "square.h"
    #include <QString>
    #include <QtGui>
    #include <QtCore>
    #include <QObject>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    
    sq = new Square(0,0);
    scene->addItem(sq);
    sq = new Square(100,0);
    scene->addItem(sq);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    //void MainWindow::setLabel(QString value)
    //{
    // ui->label->setText(value);
    //}
    @

    Well I used to write code in Autoit and work on Windows.
    And now i am trying to write code in QT and work on Ubuntu

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BlastDV
      wrote on last edited by
      #2

      Hi there,

      I see you're implementing your own "square" class. If you want something to happen everytime one of them is picked up, you may want to declare that class as a Q_OBJECT instead of just a plain C++ class. That will allow you to write signals for that object, and if you connect that signal to a slot on your main class, you will be able to detect stuff on your square class, like mouse picking. Then, it's only a matter of setting your label text to what you want to show.

      That's an overview of how it could be done and how I would try to solve it.
      If you're not familiar with Qt Objects, you may want to look at the docs.

      Tell us what you did :)

      (8) Just live your life blind like me (8)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Ducky
        wrote on last edited by
        #3

        HI thanx for the replay.

        It got me thinking and solved it in a way :).

        As far did I only added singles that can be emitted white parameters.

        is there some ware a list of mouse events that i can use for a QGraphicsItem.

        Like :
        right mouse button pressed.
        moved into QGraphicsItem.
        moved out QGraphicsItem.

        so this is my square.cpp now

        @#include "square.h"

        square::square(int x, int y, QGraphicsObject *parent) :
        QGraphicsObject(parent)
        {
        Pressed = false;
        setFlag(ItemIsSelectable);
        X = x;
        Y = y;

        }

        QRectF square::boundingRect() const
        {
        return QRectF(X,Y,30,30);
        }

        void square::mousePressEvent(QGraphicsSceneMouseEvent *ev)
        {
        Pressed = true;
        update();
        QGraphicsItem::mousePressEvent(ev);
        emit Mouse_Pressed(X,Y);
        }

        void square::mouseReleaseEvent(QGraphicsSceneMouseEvent *ev)
        {
        Pressed = false;
        update();
        QGraphicsItem::mousePressEvent(ev);
        emit Mouse_Released(X,Y);
        }

        void square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        {
        QRectF rec = boundingRect();
        QBrush brush(Qt::blue);
        if(Pressed)
        {
        brush.setColor(Qt::red);
        }
        else
        {
        brush.setColor(Qt::blue);
        }

            painter->fillRect(rec,brush);
            painter->drawRect(rec);
        

        }@

        and mij Dialog.cpp looks like.

        @#include "dialog.h"
        #include "ui_dialog.h"
        #include "square.h"
        #include <QLabel>

        Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
        {
        ui->setupUi(this);
        scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(scene);
        LineCounter = 0;
        for (int i = 0; i < 25; ++i) {

            if (i==0){
               Hoog = i*30;
            }
            else {
                Hoog = Hoog +30;
            }
        
            if (LineCounter == 5){
               links = links+30;
               Hoog = 0;
               LineCounter = 1;
                    }
            else{
               LineCounter = LineCounter +1;
            }
        sq = new square(links,Hoog);
        scene->addItem(sq);
        connect(sq,SIGNAL(Mouse_Pressed(int,int)),this,SLOT(Mouse_Pressed(int,int)));
        connect(sq,SIGNAL(Mouse_Released(int,int)),this,SLOT(Mouse_Released(int,int)));
        
        }
        

        }

        Dialog::~Dialog()
        {
        delete ui;
        }

        void Dialog::Mouse_Pressed(int x,int y)
        {
        ui->label->setText(QString("Pressed X= %1, Y= %2 ").arg(x).arg(y));

        }

        void Dialog::Mouse_Released(int x, int y)
        {
        ui->label->setText(QString("Released X= %1, Y= %2 ").arg(x).arg(y));
        }@

        Well I used to write code in Autoit and work on Windows.
        And now i am trying to write code in QT and work on Ubuntu

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved