Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLineEdit display cursos location and move forward and backward



  • 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() {
    
    }
    

  • Lifetime Qt Champion

    @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


  • Lifetime Qt Champion

    @ekato993

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


  • 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.


  • Lifetime Qt Champion

    @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?



  • @jsulm

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

    scr.PNG


  • Lifetime Qt Champion

    @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



  • @jsulm

    Oh okey, thanks.


Log in to reply