title: Qt - Why can't I trigger mousePressEvent for my custom button in MainWindow
body: <p>This is my first time to ask on Stack Overflow. If any suggestion about asking question, please let me know.
  I am very new to Qt, and have some problems while using event. Hope someone can provide any thoughts.</p>
  
  <p><strong>Background:</strong></p>
  
  <p>I have my custom pushButton, which is <code>rxPushButton</code> in <code>rx.h</code> and <code>rx.cpp</code>. I use <code>on_rxPushButton_clicked</code> to change the image and it works pretty well.
  In <code>MainWindow</code>, I need to use some <code>rx</code> so I include the class <code>rx</code> and I want to detect if I press left button of the mouse, I need to know which <code>rx</code> has been pressed and record its id in <code>int</code> <code>rxId</code> in <code>MainWindow</code>.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>I tried two ways to achieve my goal, including <code>mousePressEvent</code> and <code>eventFilter</code>. I found that I can't detect the mouse pressed signal on any <code>rx</code>, but I can detect it outside <code>rx</code> in other places in <code>Mainwindow</code>. I wonder if the events will conflict, but when I comment <code>on_rxPushButton_clicked</code> in <code>rx.cpp</code>, <code>MainWindow</code> still doesn't work for the problem. So I presume that maybe the space in the screen occupied by <code>rx</code> will not be in control of <code>MainWindow</code> (I can get Debug message "test1" but not "test2" in my code, check below).
  How should I do to solve this problem if I need both things (change image in <code>rx</code> and modify one variable in <code>MainWindow</code>)? Or maybe it's just something wrong with my code and how to modify?</p>
  
  <p>I hope to separate them if possible because I still need to include many objects in <code>MainWindow</code> in the future.</p>
  
  <p>Here are some of my related codes:</p>
  
  <p>rx.cpp</p>
  
  <pre><code>void rx::on_rxPushButton_clicked(void)
  {
      startLoading();
  }
  
  void rx::startLoading(void)
  {
      state = 1;
      connect(timer, SIGNAL(timeout()), this, SLOT(loading1()));
      timer-&gt;start(LOADING_INTERVAL);
  }
  
  void rx::loading1(void)
  {
      if(state == 1)
      {
          state = 2;
          ui-&gt;rxPushButton-&gt;setStyleSheet("border-image: url(:/images/Rx/Rxloading1.png);");
          disconnect(timer, SIGNAL(timeout()), this, SLOT(loading1()));
      }
  }
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      for(int i = 0 ; i &lt; rxSize ; i++)
      {
          rxList &lt;&lt; new rx(this);
          int j = i/rxHorizontalCount;
          rxList[i]-&gt;setGeometry(500+(i-j*8)*110,10+j*90,100,90);
      }
      //rxList[0]-&gt;installEventFilter(this);
  }
  
  void MainWindow::mousePressEvent(QMouseEvent *event)
  {
      if(event-&gt;buttons() &amp; Qt::LeftButton)
      {
          qDebug() &lt;&lt; "test1";
          if(rxList[0]-&gt;rect().contains(event-&gt;x(), event-&gt;y()))
              qDebug() &lt;&lt; "test2";
      }
  }
  </code></pre>