2

I have a single QMainWindow, which has a number of widgets, they have their frames, labels etc... ,but most importantly one QGraphicsView called test. I want it to respond when being clicked on, as I plan on creating "Paint"-like program. I can make mousePressEvent work for MainWindow:

void MainWindow::mousePressEvent(QMouseEvent *event)
{         
        //stuff    
}

problem is, that it when I click anywhere within the program, but I cannot get to work same with QGraphicsView called test instead of MainWindow eg.:

void test::mousePressEvent(QMouseEvent *event)
{         
        //stuff    
}

I get "test is not a class or namespace name" error. I have looked for solutions everywhere and my constructor DOES have ui->setupUi(this) at the very beggining and I also DO have ui included

#include "ui_mainwindow.h"
// some imore includes

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

I have no idea what am I missing here. I hope Im not doomed to checking where did mouse click through event pos() each time it clicks and whether it is in QGraphicsView boundaries, as I also plan to let user click on a few other QWidgets.

Thanks in advance

EDIT: declaration of test (auto-created by Qt, was in ui_mainwindow.h):

    test = new QGraphicsView(centralWidget);
    test->setObjectName(QStringLiteral("test"));
    test->setDragMode(QGraphicsView::NoDrag);

also, centralWidget is child of MainWindow, so its: MainWindow->centralWidget->test

EDIT2: I also have Q_OBJECT in header and I have tryied using Q_SLOTS

  • Where is the declaration of `test`? Is it being included by the source file which attempts to define `test::mousePressEvent`? – Taylor Brandstetter Dec 30 '13 at 21:47
  • Right click on your QGraphicsView in Designer and add a slot for your event. It will create everything for you automatically. – cen Dec 30 '13 at 22:39
  • In right-click -> go to slot... There are only five signals I can send and none of them seem to have anything to do with what I want. I have selected one of them and then rewritten it to mousePressEvent manually both in .cpp and .h. This way I can compile, but its not functioning (wherever I click, nothing happens). It also writes `QMetaObject::connectSlotsByName: No matching signal for on_Map_GraphicsView_mousePressEvent(QMouseEvent*)` in ApplicationOutput –  Dec 30 '13 at 22:59
  • @cen: I think you are confusing signals and slots with events... – László Papp Dec 31 '13 at 03:11

2 Answers2

0

You are having an object called test, and not a class. See how you define an object here in your code:

test = new QGraphicsView(centralWidget);

Your code is wrong here, respectively:

void test::mousePressEvent(QMouseEvent *event)

You would need to use the following line instead of the above:

void MyGraphicsView::mousePressEvent(QMouseEvent *event)

Note that this will only fix the compilation issue at hand. You will probably have other troubles with the runtime behavior based on this question...

If you are also asking about the real time operation, I would probably look into the following Qt example dealing with the mouse press event:

Diagram Scene Example

You can see the exact implementation details from their code:

header

class DiagramScene : public QGraphicsScene
{
    Q_OBJECT
    ...

    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    ...
}

source

> The mousePressEvent() function handles mouse press event's different depending on which mode the DiagramScene is in. We examine its implementation for each mode:

void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    ...
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • do I HAVE to create it as a class? I created the test::QGraphicsView through drag&drop in designer view and when I tamper with declaration of these objects, Qt just pops-up a reminder that it will be rewritten when I compile and that it is outside of project-directory. –  Dec 31 '13 at 18:48
  • @user3147616: right, so you only have a poor naming schema then. :) Do not call a class and its object the same. Not sure what you mean by class. If you mean the DiagramScene, then yes, as per the official example. – László Papp Dec 31 '13 at 18:51
  • no, I meant whether do I have to create a custom class, which will inherit from ,... to achieve anything. In other words: Can I just make QGraphicView that will send signal when clicked on? –  Dec 31 '13 at 20:37
  • @user3147616: probably not, but try it and see. I would do what the official example does even if it was working with QGraphicsView. There is not much difference, and at least you would be consitent with the aforementioned example. – László Papp Dec 31 '13 at 20:38
0

IIRC Qt event delivery will deliver events to the specific child widget at the pointer location, thus you won't catch anything useful by reimplementing the mouse event methods in the parent widget like a QMainWindow. The simplest way to go is to catch the events in the QGraphicsScene. I have another answer that demonstrates how to make a simple paint-like application by reimplementing mousePressEvent in a class derived from QGraphicsScene. Doing it in the scene requires a bit less typing and seems to look cleaner.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313