Main question
Main question

Reputation: 45

Different behavior of events in different projects

In the last question, I had a problem with placing the cursor in the right place when clicking on the LineEdit with the mouse.

The problem was solved, I, with God's help, wrote an algorithm that solves my problem. I wrote it in an EMPTY project, where there is nothing but LineEdit. Everything worked great there. Here is the code:

if(object == ui->lineEdit_newClientPhone && event->type() == QEvent::MouseButtonRelease) 
{
    QString line = ui->lineEdit_newClientPhone->displayText();
    qDebug() << line;
    ui->lineEdit_newClientPhone->setFocus();
    bool isValid = true;
    for(int i = 0; i<=15; i++){
        if(line[i] == '_'){
            ui->lineEdit_newClientPhone->setCursorPosition(i);
            isValid = false;
            break;
        }
    }
    if(isValid){
        ui->lineEdit_newClientPhone->setCursorPosition(16);
    }
}

It worked great. You click on any place in lineEdit, the mouse cursor is placed on the first empty "_" character. I joyfully ran to transfer it to the main project.

But then I encountered a strange behavior - in the main project, exactly copied code does not give such behavior. When you click on lineEdit with the mouse, the cursor is placed in the place where you clicked. I debugged all the events associated with this LineEdit and found strange behavior - the order of the events is different. Excluding unnecessary events, which were the MOST:

if(object == ui->lineEdit_newClientPhone){
    if(event->type() != QEvent::Paint && event->type() != QEvent::MouseMove && event->type() != QEvent::HoverMove){
    qDebug() << event->type();
    }
 }

Got this result in "EMPTY" project:

enter image description here

But in the main project, the sequence turned out to be different:

enter image description here

I don't know if this is the case, but in this way, my logic does not work and the cursor remains in the place where you clicked. I was able to fix the situation by changing the event to QEvent::MouseButtonRelease, but this way I get mocking cursor travels first to the place where you clicked, then to the place I need. What could be the problem and how can it be fixed? Thanks to all!

Upvotes: 0

Views: 58

Answers (1)

Main question
Main question

Reputation: 45

I don't think this is the right decision, so I would like to hear more solutions, but QTimer::singleShot helped me

f(object == ui->lineEdit_newClientPhone && event->type() == QEvent::MouseButtonPress){
    QTimer::singleShot(0,ui->lineEdit_newClientPhone,[this]
    {
        QString line = ui->lineEdit_newClientPhone->displayText();
        qDebug() << line;
        ui->lineEdit_newClientPhone->setFocus();
        bool isValid = true;
        for(int i = 0; i<=15; i++){
            if(line[i] == '_'){
                ui->lineEdit_newClientPhone->setCursorPosition(i);
                isValid = false;
                break;
            }
        }
        if(isValid){
            ui->lineEdit_newClientPhone->setCursorPosition(16);
        }
    });
}

Upvotes: 0

Related Questions