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:
Is there any way to disable that blinking cursor? thanks a lot!
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With