c++ - Qt 文件系统浏览器 : How to change root directory and update view

标签 c++ qt treeview qml qt5

我正在尝试扩展 qt filesystembrowser 示例(Qt-Creator -> Welcome -> Examples -> filesystembrowser)。我在 main.qml

中添加了一个按钮
Button {
    id: button
    x: 28
    y: 12
    text: qsTr("rootPath")
    onClicked: {
        view.model.setRoot("/home/myusername/test/")
        view.update()
    }
}

这应该改变根目录。为此,我还添加了以下功能

Q_INVOKABLE QModelIndex setRoot(QString newPath)  {
    qInfo() <<"root path "<< this->rootPath();
    newPath.replace(0,7,"");
    setRootPath(newPath);
}

点击按钮两次后,qInfo 告诉我根路径现在是 /home/myusername/test/ 但 View 没有更新。我在这里缺少什么?

最佳答案

问题是 TreeViewrootIndex 没有改变,因为它没有更新 View 。

一个解决方案是创建一个 rootIndex 属性,返回放置在 TreeView 中的索引,这必须在建立新路径时更改,因为它是将覆盖 setRootPath 方法并消除通过 setContextProperty() 发送的 rootPathIndex 属性:

main.cpp

...

class DisplayFileSystemModel : public QFileSystemModel {
    Q_OBJECT
    Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged)
public:
    ...    
    Q_INVOKABLE QModelIndex setRootPath(const QString &newPath){
       QModelIndex ix =  QFileSystemModel::setRootPath(newPath);
       setRootIndex(ix);
       return ix;
    }
    QModelIndex rootIndex() const{
        return mRootIndex;
    }
    void setRootIndex(const QModelIndex &rootIndex){
        if(mRootIndex == rootIndex)
            return;
        mRootIndex = rootIndex;
        Q_EMIT rootIndexChanged();
    }
    Q_SIGNAL void rootIndexChanged();
private:
    QModelIndex mRootIndex;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    qmlRegisterUncreatableType<DisplayFileSystemModel>("io.qt.examples.quick.controls.filesystembrowser", 1, 0,
                                                       "FileSystemModel", "Cannot create a FileSystemModel instance.");
    DisplayFileSystemModel *fsm = new DisplayFileSystemModel(&engine); // change
    fsm->setRootPath(QDir::homePath());
    fsm->setResolveSymlinks(true);
    engine.rootContext()->setContextProperty("fileSystemModel", fsm);
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

#include "main.moc"

ma​​in.qml

    ...
    Row {
        ...

        Repeater {
            model: [ "rootPath", "None", "Single", "Extended", "Multi", "Contig."]
            Button {
                text: modelData
                exclusiveGroup: eg
                checkable: modelData != "rootPath"
                checked: index === 1
                onClicked: {
                    if(modelData != "rootPath")
                        view.selectionMode = index
                    else{
                        view.model.setRootPath("/home/myusername/test/")
                    }
                }
            }
        }
    }
...    
TreeView {
    id: view
    anchors.fill: parent
    anchors.margins: 2 * 12 + row.height
    model: fileSystemModel
    rootIndex: fileSystemModel.rootIndex //change
    selection: sel
...

完整的例子可以在下面的link中找到.

关于c++ - Qt 文件系统浏览器 : How to change root directory and update view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50046645/

相关文章:

c++ - std::bind 和对基类方法的非虚拟调用

c++ - 如何在静态单例类中以编程方式从 exec 方法返回

wpf - 在 WPF 中展开 TreeView 时显示 "Please wait.."消息

c# - 如何避免在 TreeView 中闪烁

wpf - 在应用了 HierarchicalDataTemplate 的 WPF TreeView 中绑定(bind) SelectedItem

c++ - 这是迭代 2 个不同大小的单链表和一个静态数组的最佳方法吗?

C++ 指针机制 - 错误 2440

c++ - 为什么多线程更慢?

c++ - 如何让 QModelIndex 在 QAbstractTableModel 中插入新行?

qt - 如何使 QLabel 背景半透明?