我有以下类,它代表数据结构中的记录:

#ifndef UECONNECTEDCLIENTRECORD_H
#define UECONNECTEDCLIENTRECORD_H

#include <QtGlobal>
#include <QObject>
#include <QHostAddress>
#include <QString>

class UeConnectedClientRecord : public QObject
{
    Q_OBJECT

private:
    /**
     * @brief m_ueClientName
     */
    QString m_ueClientName;

    /**
     * @brief m_ueClientAddress
     */
    QHostAddress m_ueClientAddress;

    /**
     * @brief m_ueClientPort
     */
    QString m_ueClientPort;

public:
    /**
     * @brief UeConnectedClientRecord
     * @param parent
     */
    explicit UeConnectedClientRecord(const QString& clientName=QString(),
                                     const QHostAddress& clientAddress=QHostAddress(),
                                     const QString& clientPort=QString());

    /**
     * @brief ueSetClientName
     * @param name
     */
    inline void ueSetClientName(const QString& name)
        { this->m_ueClientName=name; }

    /**
     * @brief ueSetClientAddress
     * @param address
     */
    inline void ueSetClientAddress(const QHostAddress& address)
        { this->m_ueClientAddress=address; }

    /**
     * @brief ueSetClientPort
     * @param port
     */
    inline void ueSetClientPort(const QString& port)
        { this->m_ueClientPort=port; }

    /**
     * @brief ueClientName
     * @return client name
     */
    inline const QString& ueClientName() const
        { return this->m_ueClientName; }

    /**
     * @brief ueClientAddress
     * @return client address
     */
    inline const QHostAddress& ueClientAddress() const
        { return this->m_ueClientAddress; }

    /**
     * @brief ueClientPort
     * @return client port
     */
    inline const QString& ueClientPort() const
        { return this->m_ueClientPort; }
};

#endif // UECONNECTEDCLIENTRECORD_H

用于子类 QAbstractTableModel :
#ifndef UECONNECTEDCLIENTSTABLEMODEL_H
#define UECONNECTEDCLIENTSTABLEMODEL_H

#include <QObject>
#include <QAbstractTableModel>
#include <QList>
#include <QModelIndex>
#include <QVariant>
#include <QHash>
#include <QByteArray>

#include "ueconnectedclientrecord.h"

class UeConnectedClientsTableModel : public QAbstractTableModel
{
    Q_OBJECT

private:
    /**
     * @brief m_ueConnectedClientData
     */
    QList<UeConnectedClientRecord*> m_ueConnectedClientData;

protected:
    /**
     * @brief ueSetConnectedClientData
     * @param clientData
     */
    inline void ueSetConnectedClientData(const QList<UeConnectedClientRecord*>& clientData)
        { this->m_ueConnectedClientData=clientData; }

public:
    /**
     * @brief UeConnectedClientsTableModel
     * @param parent
     */
    UeConnectedClientsTableModel(QObject* parent=Q_NULLPTR);

    /**
     * @brief ueConnectedClientData
     * @return connected client data
     */
    inline const QList<UeConnectedClientRecord*>& ueConnectedClientData() const
        { return this->m_ueConnectedClientData; }

    /**
     * @brief rowCount
     * @param parent
     * @return number of rows in model
     */
    inline int rowCount(const QModelIndex& /*parent*/) const
        { return this->ueConnectedClientData().size(); }

    /**
     * @brief columnCount
     * @return number of columns in model
     */
    inline int columnCount(const QModelIndex& /*parent*/) const
        { return 3; }

    /**
     * @brief data
     * @param index
     * @param role
     * @return data for index
     */
    QVariant data(const QModelIndex& index,
                  int role=Qt::DisplayRole) const;

    /**
     * @brief roleNames
     * @return role names
     */
    QHash<int, QByteArray> roleNames() const;

    /**
     * @brief headerData
     * @param section
     * @param orientation
     * @param role
     * @return header data
     */
    QVariant headerData(int section,
                        Qt::Orientation orientation,
                        int role=Qt::DisplayRole) const;

    /**
     * @brief insertRows
     * @param row
     * @param count
     * @param parent
     * @return true if the rows were successfully inserted; otherwise returns false
     */
    bool insertRows(int row,
                    int count,
                    const QModelIndex& parent=QModelIndex());

    /**
     * @brief removeRows
     * @param row
     * @param count
     * @param parent
     * @return true if the rows were successfully removed; otherwise returns false
     */
    bool removeRows(int row,
                    int count,
                    const QModelIndex& parent=QModelIndex());

public:
    /**
     * @brief ueRoleClientName
     */
    static const int ueRoleClientName=Qt::UserRole+1;

    /**
     * @brief ueRoleClientAddress
     */
    static const int ueRoleClientAddress=Qt::UserRole+2;

    /**
     * @brief ueRoleCllientPort
     */
    static const int ueRoleCllientPort=Qt::UserRole+3;
};

#endif // UECONNECTEDCLIENTSTABLEMODEL_H

及其实现:
#include "ueconnectedclientstablemodel.h"

UeConnectedClientsTableModel::UeConnectedClientsTableModel(QObject* parent)
    : QAbstractTableModel(parent)
{
}   // constructor

QVariant UeConnectedClientsTableModel::data(const QModelIndex& index,
                                            int role) const
{
    switch(role)
    {
        case ueRoleClientName:
        {
            return this->ueConnectedClientData().at(index.row())->ueClientName();
        }   // case

        case ueRoleClientAddress:
        {
            return this->ueConnectedClientData().at(index.row())->ueClientAddress().toString();
        }   // case

        case ueRoleCllientPort:
        {
            return this->ueConnectedClientData().at(index.row())->ueClientPort().toInt();
        }   // case

        default:
        {
            return UeConnectedClientsTableModel::data(index,
                                                      role);
        }   // default
    }   // switch
}   // data

QHash<int, QByteArray> UeConnectedClientsTableModel::roleNames() const
{
    QHash<int, QByteArray> roles;

    const int roleClientName=UeConnectedClientsTableModel::ueRoleClientName;
    const int roleClientAddress=UeConnectedClientsTableModel::ueRoleClientAddress;
    const int roleCllientPort=UeConnectedClientsTableModel::ueRoleCllientPort;

    roles.insert(roleClientName,
                 "roleClientName");
    roles.insert(roleClientAddress,
                 "roleClientAddress");
    roles.insert(roleCllientPort,
                 "roleCllientPort");

    return roles;
}   // roleNames

QVariant UeConnectedClientsTableModel::headerData(int section,
                                                  Qt::Orientation orientation,
                                                  int role) const
{
    Q_UNUSED(orientation)

    if(role!=Qt::DisplayRole)
        return QVariant();

    switch(section)
    {
        case 0:
        {
            return tr("Client name");
        }   // case

        case 1:
        {
            return tr("Client address");
        }   // case

        case 2:
        {
            return tr("Client port");
        }   // case

        default:
        {
            return QVariant();
        }   // default
    }   // switch
}   // headerData

bool UeConnectedClientsTableModel::insertRows(int row,
                                              int count,
                                              const QModelIndex& parent)
{
    this->beginInsertRows(parent,
                          row,
                          row+count);

    for(int index=row; index<row+count; index++)
    {
        m_ueConnectedClientData.append(new UeConnectedClientRecord());
    }   // for

    this->endInsertRows();

    return true;
}   // insertRows

bool UeConnectedClientsTableModel::removeRows(int row,
                                              int count,
                                              const QModelIndex& parent)
{
    this->beginRemoveRows(parent,
                          row,
                          row+count);

    for(int index=row; index<row+count; index++)
    {
        m_ueConnectedClientData.removeLast();
    }   // for

    this->endRemoveRows();

    return true;
}   // removeRows

现在,我如何从代码中调用 insertRows() 以将带有真实数据(不是 UeConnectedClientRecord - 默认构造函数)的新 new UeConnectedClientRecord() 插入到内部 QList 中?我问的是因为我已经在互联网上搜索过示例并找到了 Qt Address Book Example ,但是, insertRows() from official docsinsertRows() from Address Book Example 的声明方式不同:
  • bool QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); 来自 docs
  • bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) Q_DECL_OVERRIDE; 来自 Address Book Example

  • 为什么?

    最佳答案

    从文档:



    方式 1. 使用 insertRows + setData 。 Qt 假设空记录是可能的,并且添加行不是添加数据。由于您的内部列表是指针的 Qlist,您可以在那里插入 nullptr 以防止默认构造函数调用。在这种情况下,您需要对每个 data 调用进行项目检查:如果项目是 nullptr ,则应返回默认值。

    方式2.您可以实现自己的插入功能。据我所知,insertRows 函数不被 View 使用,通常是手动调用的。但“通常”并非“总是”。例如,代理模型可以使用它。

    关于c++ - QAbstractTableModel::insertRows 将新数据附加到内部 QList,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38652031/

    10-13 02:49