Search code examples
c++qtqplaintextedit

Enable text zoom via Ctrl+Wheel in QPlainTextEdit


The documentation mentions that Ctrl+Wheel key binding for zooming in/out is supported for QPlainTextEdit in both the editing key bindings and the read-only key bindings entries.

This made me assume that this feature is there out of the box. However, when I do Ctrl+Wheel, nothing happens. Is there something in particular that I need to do to turn on that feature?


Solution

  • You can do it yourself. I wrote code snippet which can zoom in or out when you press Ctrl and use wheel

    In my case, I use eventFilter

    if(obj == ui->plainTextEdit && event->type() == QEvent::Wheel )
    {
        QWheelEvent *wheel = static_cast<QWheelEvent*>(event);
        if( wheel->modifiers() == Qt::ControlModifier )
            if(wheel->delta() > 0)
                ui->plainTextEdit->zoomIn(2);
            else
                ui->plainTextEdit->zoomOut(2);
    }
    

    Or simply make your textEdit readOnly

    ui->plainTextEdit->setReadOnly(true);
    

    Now you have choice: zooming with blocked QPlainTextEdit or zooming when user wants it(without blocking).