#include #include #include #include #include #include class CustomPushButton : public QPushButton { Q_OBJECT public: CustomPushButton(QWidget* pParent = nullptr) : QPushButton(pParent) { // Accept touch events to give visual feedback ob touch begin setAttribute(Qt::WA_AcceptTouchEvents); } bool event(QEvent* e) { if (e->type() == QEvent::MouseButtonPress) { std::cout << "PushButton MousePress - Source: " << dynamic_cast(e)->source() << std::endl; setText("pressed"); } else if (e->type() == QEvent::TouchBegin) { std::cout << "PushButton TouchBeginn" << std::endl; } else if (e->type() == QEvent::TouchCancel) { std::cout << "PushButton TouchCancel" << std::endl; } else if (e->type() == QEvent::TouchEnd) { std::cout << "PushButton TouchEnd" << std::endl; } else if (e->type() == QEvent::MouseButtonRelease) { std::cout << "PushButton MouseRelease - Source: " << dynamic_cast(e)->source() << std::endl; setText("pushMe"); } else if (e->type() == QEvent::MouseMove) { std::cout << "PushButton MouseMove" << std::endl; } return QPushButton::event(e); } }; class CustomWidget : public QWidget { public: CustomWidget(QWidget* parent) :QWidget(parent) { setAttribute(Qt::WA_AcceptTouchEvents); } bool event(QEvent* e) { if (e->type() == QEvent::MouseButtonPress) { std::cout << "CustomWidget MousePress" << std::endl; QPalette pal = QPalette(); pal.setColor(QPalette::Window, Qt::green); setPalette(pal); } else if (e->type() == QEvent::TouchBegin) { std::cout << "CustomWidget TouchBeginn" << std::endl; } else if (e->type() == QEvent::TouchCancel) { std::cout << "CustomWidget TouchCancel" << std::endl; } else if (e->type() == QEvent::TouchEnd) { std::cout << "CustomWidget TouchEnd" << std::endl; } else if (e->type() == QEvent::MouseButtonRelease) { std::cout << "CustomWidget MouseRelease" << std::endl; QPalette pal = QPalette(); pal.setColor(QPalette::Window, Qt::red); setPalette(pal); } else if (e->type() == QEvent::MouseMove) { std::cout << "CustomWidget MouseMove" << std::endl; } return QWidget::event(e); } }; class CustomEventFilter : public QObject { bool eventFilter(QObject* obj, QEvent* e) override { if (e->type() == QEvent::MouseButtonPress) { std::cout << "Filter MousePress" << std::endl; } else if (e->type() == QEvent::TouchBegin) { std::cout << "Filter TouchBeginn" << std::endl; } else if (e->type() == QEvent::TouchCancel) { std::cout << "Filter TouchCancel" << std::endl; } else if (e->type() == QEvent::TouchEnd) { std::cout << "Filter TouchEnd" << std::endl; } else if (e->type() == QEvent::MouseButtonRelease) { std::cout << "Filter MouseRelease" << std::endl; } return false; } }; int main(int argc, char* argv[]) { QCoreApplication::setOrganizationName("QtExamples"); QApplication app(argc, argv); CustomEventFilter filter; app.installEventFilter(&filter); QMainWindow* mainWindow = new QMainWindow(); //Create Parent Widget CustomWidget* customWidget = new CustomWidget(mainWindow); customWidget->setGeometry(10, 10, 300, 300); QPalette pal = QPalette(); pal.setColor(QPalette::Window, Qt::green); customWidget->setAutoFillBackground(true); customWidget->setPalette(pal); QMenu* menu = new QMenu("MyMenu", customWidget); menu->addAction("Action1"); menu->addAction("Action2"); // Button to open the popup / menu QPushButton* button = new QPushButton(customWidget); button->setText("menu"); button->setMenu(menu); // Button which stays "down" - only mousepress is arrived CustomPushButton* testButton = new CustomPushButton(customWidget); testButton->setText("pushMe"); testButton->move(150, 60); mainWindow->show(); return app.exec(); } #include "qtDemo.moc"