Unsolved Is a QWidget in a QStackedWidget not be able to receive MousePressEvent?
-
I want to pass a signal which is received by a QWidget in a QStackedWidget to QMainWindow (which would be the parent of QStackedWidget).
However, it seems that the QWidget does not receive the MousePressEvent because this simple code doesn't work.void Content::mousePressEvent(QMouseEvent*){ std::cout << "test\n"; }
How should I manage to QStackwidget in terms of a signal?
-
@Aaron-Kim Is the widget visible and the one you actually click?
-
Can you show the sample code piece ? Did you the stack widget as central widget in mainwindow object or just passed the mainwindow object as parent to stack widget ? In general as @jsulm said, if the widget is visible through stack widget it should recv the mouse press events.
-
Hi,
I have the same problem. I have a MainWindow and in the Mainwindow I have a QStackedWidget. When the mousepointer is on the QStackedWidget, I do not get any mouseevent.
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->stackedWidget->insertWidget(0, &sw1); ui->stackedWidget->insertWidget(1, &sw2); } MainWindow::~MainWindow() { delete ui; } void MainWindow::mousePressEvent(QMouseEvent *event) { qDebug() << "mouseevent from MainWindow"; }
mainwindow.h:
#include <QMainWindow> #include <QDebug> #include <QMouseEvent> #include "stacked_1.h" #include "stacked_2.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); protected: void mousePressEvent(QMouseEvent *event); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::MainWindow *ui; stacked_1 sw1; stacked_2 sw2; };
stacked_1.cpp:
stacked_1::stacked_1(QWidget *parent) : QWidget(parent), ui(new Ui::stacked_1) { ui->setupUi(this); } stacked_1::~stacked_1() { delete ui; } void stacked_1::mousePressEvent(QMouseEvent *event) { qDebug() << "stacked1: mousePressEvent"; }
stacked_1.h:
#include <QWidget> #include <QDebug> namespace Ui { class stacked_1; } class stacked_1 : public QWidget { Q_OBJECT public: explicit stacked_1(QWidget *parent = nullptr); ~stacked_1(); protected: void mousePressEvent(QMouseEvent *event); private: Ui::stacked_1 *ui; };
stacked_2.h and stacked_2.cpp are the same, except: void stacked_2::mousePressEvent(QMouseEvent *event) { qDebug() << "Stacked2: mouse Press Event"; }
When stacked_1 is active, the mouseevent should be "stacked1: mousePressEvent"; if stacked_2 is active, the output should be "Stacked2: mouse Press Event".
What should I do to get correct mouseevent from the stacked widgets?
Thanks