Always blinking cursor and act like readonly on QLineEdit
-
I would like to create a LineEdit with an always blinking cursor. I also have two push-button which can move the cursor back and forth. If I click on the mainwindow I also would like to keep the blinking cursor and also if I am clicking on the push-button. I would like to also disable clicking into the text area.
So basically LineEdit with setReadyOnly true, with always blinking cursor.
I have created a custom LineEdit which inherit from QLineEdit:
class lineEdit : public QLineEdit { Q_OBJECT public: lineEdit(QWidget* parent=nullptr) : QLineEdit(parent) {} virtual void keyPressEvent(QKeyEvent* event) { event->ignore(); } };
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QWidget *window = new QWidget; lineEdit* line = new lineEdit(window); line->setGeometry(0,0,300,100); line->setStyleSheet("font-size: 24px"); line->setText("asdfgh"); QPushButton* buttonBackward = new QPushButton(window); buttonBackward->setGeometry(0,100,50,50); buttonBackward->setText("<-"); //Keep blinking if I click on the push-button buttonBackward->setFocusPolicy(Qt::NoFocus); connect(buttonBackward, &QPushButton::clicked, this, [=]{MainWindow::moveCursorBackward(line);}); QPushButton* buttonForward = new QPushButton(window); buttonForward->setGeometry(50,100,50,50); buttonForward->setText("->"); buttonForward->setFocusPolicy(Qt::NoFocus); connect(buttonForward, &QPushButton::clicked, this, [=]{MainWindow::moveCursorForward(line);}); setCentralWidget(window); } MainWindow::~MainWindow() { delete ui; } void MainWindow::moveCursorBackward(lineEdit* l) { l->cursorBackward(false,1); } void MainWindow::moveCursorForward(lineEdit *l) { l->cursorForward(false,1); }
Now I cannot add text to the LineEdit, the blinking cursor never stops and I also can move the cursor back and forth.
How could I disable clicking into the text area, disable highlight the text, disable move the cursor by clicking into the text area, disable copying the text? -
right-clicking problem:
line->setContextMenuPolicy(Qt::NoContextMenu);
mouse problem:
virtual void mousePressEvent(QMouseEvent *event) override { event->ignore(); }
highlight problem :
QPalette palette; palette.setColor(QPalette::Highlight, lineEdit->palette().color(QPalette::Base)); palette.setColor(QPalette::HighlightedText, lineEdit->palette().color(QPalette::Text)); lineEdit->setPalette(palette);
-
Hi
You can try override the
mousePressEvent(QMouseEvent *e)https://doc.qt.io/qt-5/qwidget.html#mousePressEvent
and do nothing.
-
Yes, I read that.
Now I finally find a solution to the right-clicking problem
line->setContextMenuPolicy(Qt::NoContextMenu);
But unfortunately, I cannot find the solution to the highlights problem. I have also tried qt forum link, but it does not work for me.
virtual void focusInEvent(QFocusEvent *e) override { QLineEdit::focusInEvent(e); deselect(); }
-
right-clicking problem:
line->setContextMenuPolicy(Qt::NoContextMenu);
mouse problem:
virtual void mousePressEvent(QMouseEvent *event) override { event->ignore(); }
highlight problem :
QPalette palette; palette.setColor(QPalette::Highlight, lineEdit->palette().color(QPalette::Base)); palette.setColor(QPalette::HighlightedText, lineEdit->palette().color(QPalette::Text)); lineEdit->setPalette(palette);