Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to catch mouse click in QDoubleSpinbox



  • Hello I have a QDoubleSpinbox and I want to make it so that when a user clicks its value, it automatically selects the value so the user can type a new value to it.

    I used the following event filter. But during my test, the function is only called when I click on the main framework (anywhere outside those spinboxes). What should I do?

    bool MainWindow::eventFilter(QObject* obj, QEvent* event)
    {
        if (event->type() == QEvent::MouseButtonPress){
            if (obj == ui->input){
                ui->input->selectAll();
                return true;
            }
            if (obj == ui->error){
                ui->error->selectAll();
                return true;
            }
        }
        return QObject::eventFilter(obj, event);
    }
    


  • @MasterBlade
    I don't know, but would it work if you sub-classed the QDoubleSpinbox and overrode its eventFilter() instead? You can have an eventFilter() on every QWidget (and then it is self-contained/standalone, does not rely on writing it in container), you don't have to have it on just your QMainWindow or whatever.


  • Lifetime Qt Champion

    Hi,

    You likely want to apply that to the QLineEdit used in the QDoubleSpinBox. See the lineEdit method to get it.


Log in to reply