c++ - 模型 View 困难

标签 c++ qt model-view

我有 TreeView ,我想在其中显示用户通过 file_dialog.getOpenFileNames() 选择的文件; file_dialog 是 QFileDialog。 我确实创建了模型类:

class File_Display_Model : public QAbstractItemModel
{
    Q_OBJECT

private:
    QStringList* selected_files_;

public:
    explicit File_Display_Model(QObject *parent = nullptr,QStringList* selected_files = nullptr);

    int File_Display_Model::columnCount( const QModelIndex & parent ) const
    {
        selected_files_->count();
    }

    QVariant File_Display_Model::data(const QModelIndex & index, int role) const
    {
        if (!index.isValid())
        {
                return QVariant();
        }
        else
        {
            if (role == Qt::DisplayRole) {
                    if (index.row() == index.column())
                    {
                        return 0;
                    }
                    else
                    {
                        return selected_files_->at(role);
                    }
                }
                return QVariant();
        }
    }

    QModelIndex File_Display_Model::index(int row, int column, const QModelIndex & parent ) const
    {
         /*DUMMY - HERE I JUST DON'T KNOW WHAT TO RETURN*/
         return QModelIndex(); 
    }

    QModelIndex File_Display_Model::parent(const QModelIndex & index) const
    {
        return QModelIndex();
    }

    int File_Display_Model::rowCount( const QModelIndex & parent ) const
    {
        selected_files_->count();
    }
};

我还提供了这个类作为 TreeView 的模型。此类中的索引方法存在问题 - 我不知道要返回什么。
有人可以帮助我并指导我如何让它工作以便用户选择的文件显示在 TreeView 中吗?

最佳答案

首先没有理由使用QStringList* . Qt 使用 implicit sharing所以将它作为参数传递是有效的(不要忘记 QStringList 只不过是一个 QList<QString> )。

其次,您应该查看出色的 Qt 模型/ View 编程文档。

行数和列数

您正在尝试创建一个树模型,因此您应该仔细阅读 tree model example .请注意 rowCountcolumnCount函数以模型索引作为参数。

The rowCount() function simply returns the number of child items for the item that corresponds to a given model index, or the number of top-level items if an invalid index is specified

和列数

Since each item manages its own column data, the columnCount() function has to call the item's own columnCount() function to determine how many columns are present for a given model index. As with the rowCount() function, if an invalid model index is specified, the number of columns returned is determined from the root item

因此您必须考虑如何将您的字符串列表表示为树模型。您将拥有多少列以及每个级别将存储什么?行层次结构如何?你为什么使用 as column 来计算字符串的数量?

模型索引

当您重新实现 index()功能你只需要检查提供的行和列是否有效,如果是你应该调用 createIndex功能。同样,这完全取决于您的模型包含哪些数据以及您如何构建它们。由于您想要实现树模型,因此您还必须考虑父项。

When reimplementing this function in a subclass, call createIndex() to generate model indexes that other components can use to refer to items in your model.

关于c++ - 模型 View 困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8239065/

相关文章:

c++ - Qt 将主窗口拆分为四个小部件

c++ - 在 QTreeView 中异步加载数据

Android RoboBinding firePropertyChange() 未定义错误

c++ - c/c++ for windows 中的 DNS 服务器

c++ - 为什么点云库的 loadPCDFile 这么慢?

c++ - 如何创建自定义组件并将其添加到基于对话框的应用程序 (MFC)?

c++ - 初始化:当 value 类型为 T 时,T x(value) 与 T x = value

c++ - QSerialPort readLine() 与 readAll() 相比非常慢

qt - 摆脱 QScrollArea 中不必要的滚动条