QTableView添加记录,首先是好的,其他所有都是空行

时间:2016-07-31 06:21:55

标签: qt

几天前,我发布了这个question。通过以下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
     */
    int rowCount(const QModelIndex& parent) const;

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

    /**
     * @brief data
     * @param index
     * @param role
     * @return data for index
     */
    QVariant data(const QModelIndex& index,
                  int role=Qt::DisplayRole) 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 setData
     * @param index
     * @param value
     * @param role
     * @return true if successful; otherwise returns false
     */
    bool setData(const QModelIndex &index,
                 const QVariant &value,
                 int role = Qt::EditRole);
};

#endif // UECONNECTEDCLIENTSTABLEMODEL_H

实施文件:

QVariant UeConnectedClientsTableModel::data(const QModelIndex& index,
                                            int role) const
{
    if(!index.isValid()||index.row()>this->ueConnectedClientData().size()||index.row()<0||index.column()>UeConnectedClientRecord::NUMBER_OF_FILEDS||index.column()<0)
        return QVariant();

    switch(role)
    {
        case Qt::DisplayRole:
        {
            switch(index.column())
            {
                case UeConnectedClientRecord::INDEX_CLIENT_NAME:
                {
                    return this->ueConnectedClientData().at(index.row())->ueClientName();
                }   // case

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

                case UeConnectedClientRecord::INDEX_CLIENT_PORT:
                {
                    return this->ueConnectedClientData().at(index.row())->ueClientPort();
                }   // case
            }   // switch
        }   // case
    }   // switch

    return QVariant();
}   // data

QVariant UeConnectedClientsTableModel::headerData(int section,
                                                  Qt::Orientation orientation,
                                                  int role) const
{
    if(role!=Qt::DisplayRole)
        return QVariant();

    switch(orientation)
    {
        case Qt::Horizontal:
        {
            switch(section)
            {
                case UeConnectedClientRecord::INDEX_CLIENT_NAME:
                {
                    return tr("Client name");
                }   // case

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

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

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

        case Qt::Vertical:
        {
            return QAbstractTableModel::headerData(section,
                                                   orientation,
                                                   role);
        }   // case
    }   // switch

    return QVariant();
}   // headerData

bool UeConnectedClientsTableModel::insertRows(int row,
                                              int count,
                                              const QModelIndex& parent)
{
    Q_UNUSED(parent)

    this->beginInsertRows(QModelIndex(),
                          row,
                          row+count-1);

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

    this->endInsertRows();

    return true;
}   // insertRows

bool UeConnectedClientsTableModel::setData(const QModelIndex& index,
                                           const QVariant& value,
                                           int role)
{
    if(!index.isValid()||index.row()>this->ueConnectedClientData().size()||index.row()<0||index.column()>UeConnectedClientRecord::NUMBER_OF_FILEDS||index.column()<0||role!=Qt::EditRole)
    {
        return false;
    }
    else
    {
        int row=index.row();
        int column=index.column();

        switch(column)
        {
            case UeConnectedClientRecord::INDEX_CLIENT_NAME:
            {
                this->ueConnectedClientData().at(row)->ueSetClientName(value.toString());

                break;
            }   // case

            case UeConnectedClientRecord::INDEX_CLIENT_ADDRESS:
            {
                this->ueConnectedClientData().at(row)->ueSetClientAddress(QHostAddress(value.toString()));

                break;
            }   // case

            case UeConnectedClientRecord::INDEX_CLIENT_PORT:
            {
                this->ueConnectedClientData().at(row)->ueSetClientPort(value.toString());

                break;
            }   // case
        }   // switch

        emit(dataChanged(index,
                         index));
    }   // if

    return true;
}   // setData

int UeConnectedClientsTableModel::rowCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent)

    return this->ueConnectedClientData().size();
}   // rowCount

int UeConnectedClientsTableModel::columnCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent)

    return UeConnectedClientRecord::NUMBER_OF_FILEDS;
}   // columnCount

现在,当我向QTableView添加行时,首先添加的行数据是正常的,其他所有行都是空行: QTableView empty rows
如果将实际数据输入setData()方法并且它们,我会进行三重检查,那么为什么我会坚持添加空行?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我错过了insertRows()的小细节:

  

如果row为0,则行将预先添加到其中的任何现有行   父节点。

     

如果row是rowCount(),则行将附加到其中的任何现有行   父母。

我是内部appendingQList数据:

bool UeConnectedClientsTableModel::insertRows(int row,
                                              int count,
                                              const QModelIndex& parent)
{
    Q_UNUSED(parent)

    this->beginInsertRows(QModelIndex()/*parent*/,
                          row,
                          row+count-1);

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

    this->endInsertRows();

    return true;
}   // insertRows

QList 的开头而不是inserting 在最后

bool UeConnectedClientsTableModel::insertRows(int row,
                                              int count,
                                              const QModelIndex& parent)
{
    Q_UNUSED(parent)

    this->beginInsertRows(QModelIndex()/*parent*/,
                          row,
                          row+count-1);

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

    this->endInsertRows();

    return true;
}   // insertRows

现在工作正常:
ueConnectedClientsTableWorks.png