Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QLineEdit:自动将反斜杠附加到目录名_Qt_C++11_Qlineedit_Qcompleter - Fatal编程技术网

Qt QLineEdit:自动将反斜杠附加到目录名

Qt QLineEdit:自动将反斜杠附加到目录名,qt,c++11,qlineedit,qcompleter,Qt,C++11,Qlineedit,Qcompleter,我试图在QLineEdit中自动向有效文件路径添加反斜杠,用于显示QFileSystemModel的当前路径 代码如下所示: fileSystem = new QFileSystemModel; fileSystem->setRootPath(QObject::tr("C:\\")); QCompleter* fileSystemCompleter = new QCompleter(fileSystem); fileSystemCompleter->setCaseSensit

我试图在QLineEdit中自动向有效文件路径添加反斜杠,用于显示QFileSystemModel的当前路径

代码如下所示:

fileSystem  =   new QFileSystemModel;
fileSystem->setRootPath(QObject::tr("C:\\"));

QCompleter* fileSystemCompleter = new QCompleter(fileSystem);
fileSystemCompleter->setCaseSensitivity(Qt::CaseInsensitive);

fileTree    =   new QDeselectableTreeView();
fileTree->setModel(fileSystem);
fileTree->setRootIndex(fileSystem->index(fileSystem->rootPath()));
connect(fileTree, &QTreeView::clicked, [&] (QModelIndex index) 
{
    QString toAppend("");
    if (fileSystem->isDir(index))
    {
        toAppend = '/';
    }
    fileSystemPathEdit->setText(fileSystem->filePath(index)+toAppend);
});

// path line edit
fileSystemPathEdit = new QLineEdit(fileSystem->rootPath());
fileSystemPathEdit->setPlaceholderText("Path...");
fileSystemPathEdit->setCompleter(fileSystemCompleter);
connect(fileSystemPathEdit, &QLineEdit::editingFinished, [&]()
{
    // jump to that location
    qDebug() << fileSystemPathEdit->text();
    QModelIndex index = fileSystem->index(fileSystemPathEdit->text());
    qDebug() << index;
    fileTree->setExpanded(index,true); 
    fileTree->setCurrentIndex(index);
    // CLOSE IF EMPTY
    if (fileSystemPathEdit->text().isEmpty())
    {
        fileTree->collapseAll();
        fileSystemPathEdit->setText(fileSystem->rootPath());
    }
    // append slashes to dirs
    else if (fileSystem->isDir(index) && index.isValid())
    {
        qDebug() << "it's a dir";
        if (!fileSystemPathEdit->text().endsWith('/',Qt::CaseInsensitive))
        {
            qDebug() << "added slash";
            fileSystemPathEdit->setText(fileSystemPathEdit->text().append('/'));
            qDebug() << fileSystemPathEdit->text();
        }
    }
    this->update();
});

当我在lineEdit中按下Enter键时,它工作正常,但是如果文本由QCompleter设置,我仍然会得到相同的调试输出,显示文本已更改,但斜杠不会出现在lineEdit中。QCompleter是否以某种方式取消了文本设置?

这是一种攻击,但将此连接添加到QCompleter会产生所需的行为。我认为在激活QCompleter的同时使用editingFinished时存在竞争条件,因此添加延迟允许在不被覆盖的情况下追加斜杠。另一方面,每次更改都会多次调用know函数。我仍然对更好的解决方案感兴趣

connect(fileSystemCompleter, activatedOverloadPtr, [&](QModelIndex index)
{
    QTimer* timer =  new QTimer;
    timer->setSingleShot(true);
    timer->setInterval(10);
    connect(timer, &QTimer::timeout, fileSystemPathEdit,  &QLineEdit::editingFinished);
    timer->start();
});

如果您在完成后按Enter键怎么办?那么斜杠会被追加吗?对不起,我知道这是无关的,但是你真的需要在最后的应用程序中翻译C:\\?Windows根路径与用户的语言无关。不,这只是我编写qt代码时的习惯。无论如何,c驱动器只是一个占位符,在最终的应用程序中将是一个配置设置。
connect(fileSystemCompleter, activatedOverloadPtr, [&](QModelIndex index)
{
    QTimer* timer =  new QTimer;
    timer->setSingleShot(true);
    timer->setInterval(10);
    connect(timer, &QTimer::timeout, fileSystemPathEdit,  &QLineEdit::editingFinished);
    timer->start();
});