C++ Qt Widget app: catch mouse signals from a QPixmap?
-
I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?
I tried this approach:
MainWindow.h: protected: QPixmap pixmap_; private slots: void mySlot(QMouseEvent *event); MainWindow.cpp: pixmap_ = new QPixmap(width, height); // Connect QPixmap mouse press signal // to MainWindow slot connect(pixmap_, some-signal-here? this, &MainWindow::mySlot);
What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?
Thanks!
-
I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?
I tried this approach:
MainWindow.h: protected: QPixmap pixmap_; private slots: void mySlot(QMouseEvent *event); MainWindow.cpp: pixmap_ = new QPixmap(width, height); // Connect QPixmap mouse press signal // to MainWindow slot connect(pixmap_, some-signal-here? this, &MainWindow::mySlot);
What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?
Thanks!
Search for "Clickable Label" or something.
There is even an entry on Qt Wiki for that:
Then you just have to use
QLabel::setPixmap(yourPixmap)
to assign the image to it.Edit:
Usually there is no need to "
new
" aQPixmap
. Using a stack variable is sufficient most of the time. -
I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?
I tried this approach:
MainWindow.h: protected: QPixmap pixmap_; private slots: void mySlot(QMouseEvent *event); MainWindow.cpp: pixmap_ = new QPixmap(width, height); // Connect QPixmap mouse press signal // to MainWindow slot connect(pixmap_, some-signal-here? this, &MainWindow::mySlot);
What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?
Thanks!
@Tom-asso A QPixmap is an in-memory structure holding an image, not something that a user can click on. If you want a user to click on a displayed version of this image then you need to put it on a widget that a user can interact with (e.g. QLabel). The widget can handle mouse click events, map that to a point in the image, and emit a signal carrying that information.
-
I suspect this has a simple answer, but I'm missing something about signals and slots. My C++ MainWindow class creates a QPixmap - how can a member function receive signals emitted when the user clicks the mouse within the QPixmap?
I tried this approach:
MainWindow.h: protected: QPixmap pixmap_; private slots: void mySlot(QMouseEvent *event); MainWindow.cpp: pixmap_ = new QPixmap(width, height); // Connect QPixmap mouse press signal // to MainWindow slot connect(pixmap_, some-signal-here? this, &MainWindow::mySlot);
What signal should MainWindow catch? Do I have to subclass QPixmap to make this happen?
Thanks!
Search for "Clickable Label" or something.
There is even an entry on Qt Wiki for that:
Then you just have to use
QLabel::setPixmap(yourPixmap)
to assign the image to it.Edit:
Usually there is no need to "
new
" aQPixmap
. Using a stack variable is sufficient most of the time. -
T Tom asso has marked this topic as solved on