Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. EventFilter detecting object
Forum Updated to NodeBB v4.3 + New Features

EventFilter detecting object

Scheduled Pinned Locked Moved Unsolved C++ Gurus
2 Posts 2 Posters 722 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xcalzada
    wrote on last edited by
    #1

    I am using the eventFilter to detect the object clicked.

    I have two kind of differents object

    QLabel and QPushButton.

    I am using this piece of code:

    bool minesweeper::eventFilter(QObject watched, QEvent event) {
    if (event->type() == QEvent::MouseButtonPress)
    {
    const QMouseEvent
    const me = static_cast<const QMouseEvent
    >(event);
    const QPoint p = me->pos(); //...or ->globalPos();
    QWidget * const widget = childAt(p);

    	const QLabel * const label = qobject_cast<QLabel *>(widget);
    	const QPushButton * const label2 = qobject_cast<QPushButton *>(widget);
    			
    	if(label == null){
    	cout<< "it is Pushbutton" <<endl;
    	}else{
    	cout<< "it is Pushbutton" <<endl;
    	}		
    

    }

    Are there any way more elegance to do it?

    Is dangerous the condition label == null? Because you are doing n static cast and could be not always is null.

    jsulmJ 1 Reply Last reply
    0
    • X xcalzada

      I am using the eventFilter to detect the object clicked.

      I have two kind of differents object

      QLabel and QPushButton.

      I am using this piece of code:

      bool minesweeper::eventFilter(QObject watched, QEvent event) {
      if (event->type() == QEvent::MouseButtonPress)
      {
      const QMouseEvent
      const me = static_cast<const QMouseEvent
      >(event);
      const QPoint p = me->pos(); //...or ->globalPos();
      QWidget * const widget = childAt(p);

      	const QLabel * const label = qobject_cast<QLabel *>(widget);
      	const QPushButton * const label2 = qobject_cast<QPushButton *>(widget);
      			
      	if(label == null){
      	cout<< "it is Pushbutton" <<endl;
      	}else{
      	cout<< "it is Pushbutton" <<endl;
      	}		
      

      }

      Are there any way more elegance to do it?

      Is dangerous the condition label == null? Because you are doing n static cast and could be not always is null.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @xcalzada You could also use https://doc.qt.io/qt-5/qobject.html#inherits

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved