tako
tako

Reputation: 71

How to disable the cursor in QTextEdit?

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!

Upvotes: 7

Views: 2701

Answers (2)

eyllanesc
eyllanesc

Reputation: 243887

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();
}

Upvotes: 1

Nejat
Nejat

Reputation: 32635

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.

Upvotes: 4

Related Questions