How to disable the cursor in QTextEdit?
Asked Answered
Z

2

7

I am now using QTextEdit with qt virtual keyboard, and I face an issue with QTextEdit

I want to disable the textcursor in QTextEdit. I tried to use

setCursorWidth(0);

The textcursor does disappear. But when I use arabic keybaord, there will be a small arrow blinking up there

like this:

enter image description here

Is there any way to disable that blinking cursor? thanks a lot!

Zillion answered 6/3, 2019 at 10:24 Comment(0)
W
4

Actually this is a Qt bug which is reported here. As a workaround you can have your custom class which inherits from QTextEdit and re-implement keyPressEvent event:

class TextEdit : public QTextEdit
{
public:
    TextEdit(QWidget* parent = nullptr) : QTextEdit(parent) {
        setReadOnly(true);
    }
    void keyPressEvent(QKeyEvent* event) {
        setReadOnly(false);
        QTextEdit::keyPressEvent(event);
        setReadOnly(true);
    }
};

This will also hide the cursor in Right to left languages.

Wheelhorse answered 6/3, 2019 at 10:47 Comment(1)
Hi Nejat, thanks to your comment. But I'm also using qt virtual keybaord now. I'm trying to use virtaul keybaord to type some arabic onto the QTextEdit. It seems that when I set "setReadOnly(true)", then I can not activate qt virtual keyboard.Zillion
V
1

A simple solution is to create a QProxyStyle, so all the widgets will be affected without the need to inherit from that class.

#include <QtWidgets>

class CursorStyle: public QProxyStyle
{
public:
    using QProxyStyle::QProxyStyle;
    int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override
    {
        if(metric == PM_TextCursorWidth)
            return 0;
        return  QProxyStyle::pixelMetric(metric, option, widget);
    }
};


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CursorStyle *style = new CursorStyle(a.style());
    a.setStyle(style);
    QWidget w;
    QVBoxLayout *lay = new QVBoxLayout(&w);
    lay->addWidget(new QLineEdit);
    lay->addWidget(new QTextEdit);
    w.show();
    return a.exec();
}
Vomer answered 6/3, 2019 at 11:8 Comment(1)
hi, thanks for your comment! I think the class you implemented did similar things as setCursorWidth(0) ? (if(metric == PM_TextCursorWidth) return 0;) However, I can still see the blinking arrow after using this. And by the way, I'm using qt virtual keyboard to type arabic, not the hardware keybaord on laptop/computer.Zillion

© 2022 - 2024 — McMap. All rights reserved.