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. QLineEdit display cursos location and move forward and backward

QLineEdit display cursos location and move forward and backward

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 737 Views
  • 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.
  • E Offline
    E Offline
    ekato993
    wrote on last edited by
    #1

    I have created a line edit in the mainwindow ui, and there are push buttons as well, which are modify the text of the line edit. Each time I pressed the button I add the push button text to the vector and the cursor moves forward.

    I would like to make a cursor which could move move back and forth if I pressed the appropriate button and if the cursor is in the middle of the line edit text add the text to the proper vector position.

    I have completely no idea how to do it?

    ui->lineEdit->setReadOnly(true);
    
    connect(ui->pushButton_50, &QPushButton::clicked, this, &MainWindow::modLineEdit);
    connect(ui->pushButton_1, &QPushButton::clicked, this, &MainWindow::modLineEdit);
    connect(ui->pushButton_wer, &QPushButton::clicked, this, &MainWindow::modLineEdit);
    
    connect(ui->pushButton_forward, &QPushButton::clicked, this, &MainWindow::forward);
    connect(ui->pushButton_backward, &QPushButton::clicked, this, &MainWindow::backward);
    

    pushButton_50 means that the push button text = 50
    pushButton_1 means that the push button text = 1
    pushButton_wer means that the push button text = wer

    QVector<QString> pressButtons;
    
    void MainWindow::modLineEdit() {
    
        QString LEstr = ui->lineEdit->text();
    
        QPushButton* button = (QPushButton*)sender();
        QString buttonText = button->text();
    
        pressButtons.push_back(buttonText);
    
        LEstr += buttonText;
        ui->lineEdit->setText(LEstr);
    
    }
    
    void MainWindow::forward() {
    
    }
    
    void MainWindow::backward() {
    
    }
    
    jsulmJ 1 Reply Last reply
    0
    • E ekato993

      @jsulm

      I think you do not understand me, the cursor does NOT appear on the screen

      scr.PNG

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @ekato993 said in QLineEdit display cursos location and move forward and backward:

      cursor is do NOT appear on the screen

      Probably because you set the line edit read-only

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      1
      • E ekato993

        I have created a line edit in the mainwindow ui, and there are push buttons as well, which are modify the text of the line edit. Each time I pressed the button I add the push button text to the vector and the cursor moves forward.

        I would like to make a cursor which could move move back and forth if I pressed the appropriate button and if the cursor is in the middle of the line edit text add the text to the proper vector position.

        I have completely no idea how to do it?

        ui->lineEdit->setReadOnly(true);
        
        connect(ui->pushButton_50, &QPushButton::clicked, this, &MainWindow::modLineEdit);
        connect(ui->pushButton_1, &QPushButton::clicked, this, &MainWindow::modLineEdit);
        connect(ui->pushButton_wer, &QPushButton::clicked, this, &MainWindow::modLineEdit);
        
        connect(ui->pushButton_forward, &QPushButton::clicked, this, &MainWindow::forward);
        connect(ui->pushButton_backward, &QPushButton::clicked, this, &MainWindow::backward);
        

        pushButton_50 means that the push button text = 50
        pushButton_1 means that the push button text = 1
        pushButton_wer means that the push button text = wer

        QVector<QString> pressButtons;
        
        void MainWindow::modLineEdit() {
        
            QString LEstr = ui->lineEdit->text();
        
            QPushButton* button = (QPushButton*)sender();
            QString buttonText = button->text();
        
            pressButtons.push_back(buttonText);
        
            LEstr += buttonText;
            ui->lineEdit->setText(LEstr);
        
        }
        
        void MainWindow::forward() {
        
        }
        
        void MainWindow::backward() {
        
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @ekato993

        void MainWindow::forward()
        {
            if (currentIndex < pressButtons.size() - 1) {
                QString nextString = pressButtons[++currentIndex];
                ui->lineEdit->setText(nextString);
            }
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • E Offline
          E Offline
          ekato993
          wrote on last edited by
          #3
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
          
              ui->lineEdit->setReadOnly(true);
          
             ui->lineEdit->setCursorPosition(0);
          
              qDebug()  << ui->lineEdit->cursorPosition();
          
              connect(ui->pushButton_50, &QPushButton::clicked, this, &MainWindow::modLineEdit);
              connect(ui->pushButton_1, &QPushButton::clicked, this, &MainWindow::modLineEdit);
              connect(ui->pushButton_wer, &QPushButton::clicked, this, &MainWindow::modLineEdit);
          
              connect(ui->pushButton_forward, &QPushButton::clicked, this, &MainWindow::forward);
              connect(ui->pushButton_backward, &QPushButton::clicked, this, &MainWindow::backward);
          
          }
          

          ui->lineEdit->setCursorPosition(0); does not set the cursor. This is my first problem.

          jsulmJ 1 Reply Last reply
          0
          • E ekato993
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
            
                ui->lineEdit->setReadOnly(true);
            
               ui->lineEdit->setCursorPosition(0);
            
                qDebug()  << ui->lineEdit->cursorPosition();
            
                connect(ui->pushButton_50, &QPushButton::clicked, this, &MainWindow::modLineEdit);
                connect(ui->pushButton_1, &QPushButton::clicked, this, &MainWindow::modLineEdit);
                connect(ui->pushButton_wer, &QPushButton::clicked, this, &MainWindow::modLineEdit);
            
                connect(ui->pushButton_forward, &QPushButton::clicked, this, &MainWindow::forward);
                connect(ui->pushButton_backward, &QPushButton::clicked, this, &MainWindow::backward);
            
            }
            

            ui->lineEdit->setCursorPosition(0); does not set the cursor. This is my first problem.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @ekato993

            void MainWindow::forward()
            {
                if (currentIndex < pressButtons.size() - 1) {
                    QString nextString = pressButtons[++currentIndex];
                    ui->lineEdit->setText(nextString);
                    ui->lineEdit->setCursorPosition(nextString.length() / 2); // You want to set cursor in the middle, right?
                }
            }
            

            "ui->lineEdit->setCursorPosition(0); does not set the cursor. This is my first problem." - what do you mean? Does the lineEdit already have a text when you call setCursorPosition in the constructor?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            E 1 Reply Last reply
            0
            • jsulmJ jsulm

              @ekato993

              void MainWindow::forward()
              {
                  if (currentIndex < pressButtons.size() - 1) {
                      QString nextString = pressButtons[++currentIndex];
                      ui->lineEdit->setText(nextString);
                      ui->lineEdit->setCursorPosition(nextString.length() / 2); // You want to set cursor in the middle, right?
                  }
              }
              

              "ui->lineEdit->setCursorPosition(0); does not set the cursor. This is my first problem." - what do you mean? Does the lineEdit already have a text when you call setCursorPosition in the constructor?

              E Offline
              E Offline
              ekato993
              wrote on last edited by ekato993
              #5

              @jsulm

              I think you do not understand me, the cursor does NOT appear on the screen

              scr.PNG

              jsulmJ 1 Reply Last reply
              0
              • E ekato993

                @jsulm

                I think you do not understand me, the cursor does NOT appear on the screen

                scr.PNG

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @ekato993 said in QLineEdit display cursos location and move forward and backward:

                cursor is do NOT appear on the screen

                Probably because you set the line edit read-only

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                E 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @ekato993 said in QLineEdit display cursos location and move forward and backward:

                  cursor is do NOT appear on the screen

                  Probably because you set the line edit read-only

                  E Offline
                  E Offline
                  ekato993
                  wrote on last edited by
                  #7

                  @jsulm

                  Oh okey, thanks.

                  1 Reply Last reply
                  0

                  • Login

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