Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Always blinking cursor and act like readonly on QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Always blinking cursor and act like readonly on QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kocka
    wrote on last edited by kocka
    #1

    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?

    mrjjM 1 Reply Last reply
    0
    • CP71C CP71

      @mrjj
      Did you read this?

      https://forum.qt.io/topic/105953/force-always-blinking-cursor-on-qlineedit

      Maybe it could be helpful

      K Offline
      K Offline
      kocka
      wrote on last edited by kocka
      #6

      @CP71

      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);
      
      1 Reply Last reply
      2
      • K kocka

        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?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @kocka

        Hi
        You can try override the
        mousePressEvent(QMouseEvent *e)

        https://doc.qt.io/qt-5/qwidget.html#mousePressEvent

        and do nothing.

        CP71C 1 Reply Last reply
        1
        • K Offline
          K Offline
          kocka
          wrote on last edited by
          #3
          virtual void mousePressEvent(QMouseEvent *event) override {
                  event->ignore();
              }
          

          That is working for me but there is some problem.

          Double left-click highlight the whole text and I also would like to disable right-click on the text area.

          qt.png

          1 Reply Last reply
          1
          • mrjjM mrjj

            @kocka

            Hi
            You can try override the
            mousePressEvent(QMouseEvent *e)

            https://doc.qt.io/qt-5/qwidget.html#mousePressEvent

            and do nothing.

            CP71C Offline
            CP71C Offline
            CP71
            wrote on last edited by
            #4

            @mrjj
            Did you read this?

            https://forum.qt.io/topic/105953/force-always-blinking-cursor-on-qlineedit

            Maybe it could be helpful

            K 1 Reply Last reply
            0
            • K Offline
              K Offline
              kocka
              wrote on last edited by kocka
              #5

              @CP71

              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();
                  }
              
              1 Reply Last reply
              0
              • CP71C CP71

                @mrjj
                Did you read this?

                https://forum.qt.io/topic/105953/force-always-blinking-cursor-on-qlineedit

                Maybe it could be helpful

                K Offline
                K Offline
                kocka
                wrote on last edited by kocka
                #6

                @CP71

                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);
                
                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved