c++qtdrag-and-dropdropeditline

QT c++: drop in multi line edite


Im beginner in qt and i do my first project. I am encountering a problem.

I put somes edit line in a scroll area. All of this edit text should countains path to files. To make this app more userfriendly i decided to implement a drag and drop. By this way, users can just take a file from their explorer and drop it to line edit which will be fill with the path of the file.

My problem is: When i try to drop, all edit line where my mouse passed on, will be fill with the path of the file. If i change if statements by else if, its the first edit line that my mouse passed on which will be fill but not the one where my mouse is at the moment of the drop.

here the code:



    void MainWindow::dragEnterEvent(QDragEnterEvent *e)
    {
        e->accept()
    }
     
    void MainWindow::dropEvent(QDropEvent *e)
    {
        foreach (const QUrl &url, e->mimeData()->urls()) {
            QString fileName = StringManagement::getDir(url.toLocalFile());
     
            if(ui->lineEdit->underMouse())
                ui->lineEdit->setText(fileName);
            if(ui->lineEdit_2->underMouse())
                ui->lineEdit_2->setText(fileName);
            if(ui->lineEdit_5->underMouse())
                ui->lineEdit_5->setText(fileName);
            if(ui->lineEdit_9->underMouse())
                ui->lineEdit_9->setText(fileName);
            if(ui->lineEdit_10->underMouse())
                ui->lineEdit_10->setText(fileName);
            if(ui->lineEdit_11->underMouse())
                ui->lineEdit_11->setText(fileName);
        }
    }

On other point that i dont really understand is:

<pre><code>void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
    qInfo() << "enter";
    e->accept();
}
 
 
void MainWindow::dragLeaveEvent(QDragLeaveEvent *e){
    qInfo() << "leave";
    e->accept();
}

when i put my mouse on an edit line and i stay on it, i will see both message in the console... i expected to see the first one when my mouse enter in and the second one when my mouse leave it.

thank you in advance for your helps.


Solution

  • Ok i find a solution. I dont really like it because i dont find it really clean but that works. If others have cleaner solution im open to it. I put my solution here. Maybe that could help someone in the future. I finaly didn't use drop methode but i used an eventFilter which give me the posibility to have a better management of events.

    <pre><code>MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        {
        this->data = DataModel::GetInstance();
        ui->setupUi(this);
        setAcceptDrops(true);
        //ui->lineEdit->dragEnabled();
        //ui->lineEdit->setAcceptDrops();
        installEventFilter(this);
        ui->lineEdit->installEventFilter(this);
        ui->lineEdit_2->installEventFilter(this);
        ui->lineEdit_5->installEventFilter(this);
        ui->lineEdit_9->installEventFilter(this);
        ui->lineEdit_10->installEventFilter(this);
        ui->lineEdit_11->installEventFilter(this);
    
        }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::dragEnterEvent(QDragEnterEvent *e)
      {
          qInfo() << "enter";
          e->accept();
      }
    
    bool MainWindow::eventFilter(QObject* obj, QEvent* event){
    
    
        if(event->type() == QEvent::DragEnter){
            if(obj == ui->lineEdit){
                this->flag = 1;
            }
            else if(obj == ui->lineEdit_2){
                this->flag = 2;
            }
            else if(obj == ui->lineEdit_5){
                this->flag = 3;
            }
            else if(obj == ui->lineEdit_9){
                this->flag = 4;
            }
            else if(obj == ui->lineEdit_10){
                this->flag = 5;
            }
            else if(obj == ui->lineEdit_11){
                this->flag = 6;
            }
             qInfo()<<"flag" <<this->flag;
        }
    
    
        if(event->type() == QEvent::Drop){
            qInfo()<< obj;
            QDropEvent *drop = static_cast<QDropEvent *>(event);
            foreach (const QUrl &url, drop->mimeData()->urls()) {
                QString fileName = StringManagement::getDir(url.toLocalFile());
                qInfo()<<"flag drop" <<this->flag;
                if(this->flag == 1){
                    ui->lineEdit->setText(fileName);
                }
                else if(this->flag == 2){
                    ui->lineEdit_2->setText(fileName);
                }
                else if(this->flag == 3){
                    ui->lineEdit_5->setText(fileName);
                }
                else if(this->flag == 4){
                    ui->lineEdit_9->setText(fileName);
                }
                else if(this->flag == 5){
                    ui->lineEdit_10->setText(fileName);
                }
                else if(this->flag == 6){
                    ui->lineEdit_11->setText(fileName);
                }
                return true;
    
    
           }
    
    
        }
    
    }
    
    </code></pre>
    

    I dont manage return in the filter for now but the idea is here.