Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to know if QTimeEdit field clicked?

How to know if QTimeEdit field clicked?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 494 Views 3 Watching
  • 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.
  • M Offline
    M Offline
    masa4
    wrote on last edited by masa4
    #1

    I need to listen click events of line edits&time edits. Line edit works but time edit does not work:

    MyClass::MyClass(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MyClass)
    {
        ui->setupUi(this);
    
        ui->lineEdit->installEventFilter(this);
        ui->timeEdit->installEventFilter(this);
    }
    bool MyClass::eventFilter(QObject* object, QEvent* event)
    {
        if((object == ui->lineEdit || object == ui->timeEdit) && event->type() == QEvent::MouseButtonPress) {
            qDebug() << "mouse clicked";
            return false;
        }
        return false;
    }
    

    Any idea?

    If I click the arrows near the timeEdit, the event is occuring but if i click text part, it doesnt occur.

    JonBJ M C 3 Replies Last reply
    0
    • M masa4

      I need to listen click events of line edits&time edits. Line edit works but time edit does not work:

      MyClass::MyClass(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::MyClass)
      {
          ui->setupUi(this);
      
          ui->lineEdit->installEventFilter(this);
          ui->timeEdit->installEventFilter(this);
      }
      bool MyClass::eventFilter(QObject* object, QEvent* event)
      {
          if((object == ui->lineEdit || object == ui->timeEdit) && event->type() == QEvent::MouseButtonPress) {
              qDebug() << "mouse clicked";
              return false;
          }
          return false;
      }
      

      Any idea?

      If I click the arrows near the timeEdit, the event is occuring but if i click text part, it doesnt occur.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @masa4
      A QTimeEdit may have some other widget for the text editing field, and events for that may not filtered by the QTimeEdit level, I don't know. Try a ui->timeedit->findChildren<QWidget *>(), if it has child widgets maybe you need to install an eventFilter() on those or change your if (object == ... conditions?

      1 Reply Last reply
      0
      • M masa4

        I need to listen click events of line edits&time edits. Line edit works but time edit does not work:

        MyClass::MyClass(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::MyClass)
        {
            ui->setupUi(this);
        
            ui->lineEdit->installEventFilter(this);
            ui->timeEdit->installEventFilter(this);
        }
        bool MyClass::eventFilter(QObject* object, QEvent* event)
        {
            if((object == ui->lineEdit || object == ui->timeEdit) && event->type() == QEvent::MouseButtonPress) {
                qDebug() << "mouse clicked";
                return false;
            }
            return false;
        }
        

        Any idea?

        If I click the arrows near the timeEdit, the event is occuring but if i click text part, it doesnt occur.

        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        @masa4 said in How to know if QTimeEdit field clicked?:

        If I click the arrows near the timeEdit, the event is occuring but if i click text part, it doesnt occur.

        Try with QEvent::FocusIn instead.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CPPUIX
          wrote on last edited by CPPUIX
          #4

          The problem is with QAbstractSpinBox, it does not take mouse press events on its QLineEdit.

          So I looked it up and found this solution, basically just sub-class QTimeEdit, and use its lineEdit(it's its child), and install your event filter on it. Which is what @JonB told you to try as well.

          Here's my implementation:
          mainwindow.cpp

          #include "mainwindow.h"
          #include "./ui_mainwindow.h"
          #include <QLayout>
          
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
              MyTimeEdit *timee = new MyTimeEdit(this);
              //find TimeEdit's child, just like JonB says, but specify that it's a QLineEdit 
              editor = timee->findChild<QLineEdit *>();
              editor->installEventFilter(this);
              this->layout()->addWidget(timee);
              this->timee->setGeometry(20,20,50,50);
          
          
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          bool MainWindow::eventFilter(QObject* object, QEvent* event)
          {
          
              if(object == editor && event->type() == QEvent::MouseButtonPress)
              {
                  fprintf(stderr,"you have clicked\n");
                  return false;
              }
              
              return false;
          }
          

          mainwindow.h:

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          #include <QWidget>
          #include <QLineEdit>
          #include <QTimeEdit>
          
          class MyTimeEdit : public QTimeEdit
          {
              Q_OBJECT
          
          public:
              MyTimeEdit(QWidget *parent = nullptr){};
              QLineEdit* lineEdit(){};
          };
          
          QT_BEGIN_NAMESPACE
          namespace Ui { class MainWindow; }
          QT_END_NAMESPACE
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
              MyTimeEdit *timee = new MyTimeEdit(this);
              QLineEdit *editor;
          
          protected:
              bool eventFilter(QObject* object, QEvent* event) override;
          
          private:
              Ui::MainWindow *ui;
          
          };
          #endif // MAINWINDOW_H
          
          
          1 Reply Last reply
          1
          • M masa4

            I need to listen click events of line edits&time edits. Line edit works but time edit does not work:

            MyClass::MyClass(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::MyClass)
            {
                ui->setupUi(this);
            
                ui->lineEdit->installEventFilter(this);
                ui->timeEdit->installEventFilter(this);
            }
            bool MyClass::eventFilter(QObject* object, QEvent* event)
            {
                if((object == ui->lineEdit || object == ui->timeEdit) && event->type() == QEvent::MouseButtonPress) {
                    qDebug() << "mouse clicked";
                    return false;
                }
                return false;
            }
            

            Any idea?

            If I click the arrows near the timeEdit, the event is occuring but if i click text part, it doesnt occur.

            C Offline
            C Offline
            CPPUIX
            wrote on last edited by
            #5

            @masa4 Don't forget to mark this topic as solved, it would help future readers to know that there's a solution.

            1 Reply Last reply
            0

            • Login

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