简体   繁体   English

QT c++:插入多行编辑

[英]QT c++: drop in multi line edite

Im beginner in qt and i do my first project.我是 qt 的初学者,我做我的第一个项目。 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.如果我通过 else if 更改 if 语句,它是我的鼠标传递的第一个编辑行,它将被填充,但不是我的鼠标在下降时所在的编辑行。

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.提前感谢您的帮助。

Following your answer to my comment I'll try to help you.根据您对我的评论的回答,我会尽力帮助您。 I'm not an expert at Qt so I may be wrong but since there is no answer yet I'll try to give one.我不是 Qt 的专家,所以我可能是错的,但由于还没有答案,我会试着给出一个。

I tried to reproduce your code and for the second question:我试图重现您的代码和第二个问题:

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

I have the same behaviour if MainWindow and the lineEdits both manage drag and drop ( setAcceptDrops(true) ).如果MainWindow和 lineEdits 都管理拖放( setAcceptDrops(true) ),我有相同的行为。 I think that you "enter" when you enter the MainWindow and then "leave" when you enter the lineEdit since it manages itself the drag and drop.我认为您在进入MainWindow时“进入”,然后在进入lineEdit时“离开”,因为它自己管理拖放。

If you set:如果你设置:

    ui->lineEdit->setAcceptDrops(false);

Then you don't "leave" anymore.然后你就不再“离开”了。

For the first part对于第一部分

If I try to reproduce your code, I have a problem with the underMouse() function.如果我尝试重现您的代码,则underMouse() function 有问题。 Maybe your problem comes from here?也许你的问题来自这里? If I implement my own underMouse() then everything is fine.如果我实现自己的underMouse()那么一切都很好。

I hope someone else with better Qt knowledge will come to your help.我希望其他有更好的 Qt 知识的人会来帮助你。

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.我最终没有使用 drop 方法,但我使用了一个 eventFilter,它让我可以更好地管理事件。

<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.我现在不管理过滤器中的返回,但想法就在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM