0

I'd like to do something like this, even though I understand this is not how it works. I'm not really good with QMouseEvent,

void MainWindow::on_addState_triggered(QMouseEvent* event)
{
    qDebug() << event->pos();

    if (event->buttons() == Qt::LeftButton)
    {
      newBoard.drawState(event->x(),event->y());
    }

}

So I want to click on the button addState in my interface (made with the Designer Mode, ui file) then on the second click on my scene it calls drawState() with the mouse (on click) position.

How I'm I supposed to do this ?

Right now the code compile but while running I get this error

QMetaObject::connectSlotsByName: No matching signal for on_addState_triggered(QMouseEvent*)
DevOops
  • 87
  • 9
  • You should generally avoid the magic "on_*foo*_*signalname*" slot function signatures. 1) the connections are not compile time checked. 2) they can clash with other function names. 3) they use the old string based signal/slot connection mechanism, which is slow compared to the modern member function pointer based option. 4) they often hide in .ui files and can be tricky to spot for other people. – Jesper Juhl Jun 07 '20 at 13:32
  • The question is a bit unclear. However, if it's about what I think it is... I assume the scene is shown in a widget which provides some kind of canvas. The usual way to add custom mouse event handling in Qt -> inherit the widget and override the mouse events. Based on this, the addState button could activate a specific mode in your canvas widget. The mouse event of the canvas widget adds the state e.g. in `mousePressEvent()` if in this specific mode (or does something else or nothing if not). – Scheff's Cat Jun 07 '20 at 13:40
  • FYI: Qt doc. provides the [**Scribble Example**](https://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html) which is quite close to what you intend to do (IMHO). My answer to [SO: how can I draw a circle and a line at the same time in an interface?](https://stackoverflow.com/a/56524039/7478597). My answer to [SO: How to paint / deform a QImage in 2D?](https://stackoverflow.com/a/56970955/7478597). – Scheff's Cat Jun 07 '20 at 13:43
  • Thanks for your comments, I understand the magic slot function signatures is not really good for advanced stuff, I will probably change that. However, reading your answers guys, I'm still confused about how I'm going to do what I want : _When addState button triggered, if Qt::LeftButton then call drawState(mouse pos)_ – DevOops Jun 07 '20 at 14:58

1 Answers1

0

Alright, the way I imagine I can do it is a little bit ugly but it "works".

Maybe there is a cleaner way to do the following :

    void MainWindow::on_addState_triggered()
    {

          drawAddState=true;

    }

  void MainWindow::mousePressEvent(QMouseEvent *event)
    {
        if(drawAddState==true)
        {
            if (event->button() == Qt::LeftButton) {
                newBoard.drawState(event->localPos().x(),event->localPos().y());
                drawAddState=false;
            }
        }

    }

Unfortunately the event->localPos(), pos() or even GlobalPos is not relative to my GraphicsScene, I can't place the state item exactly where I click. Any idea on this ?

Update : I replied myself here : https://stackoverflow.com/a/62278962/13621190

DevOops
  • 87
  • 9
  • Your code snippet seems to resemble what I tried recommend. :-) Concerning _Unfortunately the event->localPos(), pos() or even GlobalPos is not relative to my GraphicsScene_: The mouse event represents pixel coordinates (related to widgets). Your `QGraphicsScene` might be subject of additional transformations. You have to apply these transformations to your mouse position as well to get correct scene coordinates. Please, have a look at [The Graphics View Coordinate System](https://doc.qt.io/qt-5/graphicsview.html#the-graphics-view-coordinate-system) to find an intro for this topic. – Scheff's Cat Jun 08 '20 at 15:31
  • You even can remark your answer as solution: [Can I answer my own question?](https://stackoverflow.com/help/self-answer). – Scheff's Cat Jun 09 '20 at 10:14