Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the cursor in QTextEdit?

Tags:

c++

qt

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!

like image 263
tako Avatar asked Mar 06 '19 10:03

tako


2 Answers

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.

like image 128
Nejat Avatar answered Sep 20 '22 00:09

Nejat


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();
}
like image 28
eyllanesc Avatar answered Sep 18 '22 00:09

eyllanesc