无法使用QTableView查看所有列,我的子类继承自QAbstractTableModel。

Can't view all Colums using QTableView and my subclass which inherits from QAbstractTableModel

本文关键字:子类 我的 继承 QAbstractTableModel QTableView      更新时间:2023-10-16

我正在学习Qt。

大部分代码来自一本书(The Art of Building Qt Applications)

我的目标:我试图创建一个简单的地址簿模型,从QAbstractTableModel继承。我已经实现了rowCount()、columnCount()、data()、headerData()、index()和parent()。

问题:只有第一列显示当我调用QTableView。我会张贴输出的图像,但我从来没有真正回答这里的问题…对不起。

But the output looks something like this:   
 __________________
|_____Lead________|
|Jim Grayson      |
|Prescilla Winston|
|Melissa Potter   |
|_________________|
It should look something like this:
_________________________________________________________
|_____Lead________|____Title_____|__Phone____|___Notes___|
|Jim Grayson      |     ...      |           |           |
|Prescilla Winston|              |   ....    |           |
|__Melissa Potter_|______________|___________|____...___ |

这是我试图运行的小。csv文件

db_small.csv:

Lead/Title/Phone/Notes
Jim Grayson/Senior Manager/(555)761-2385/"Spoke Tuesday, he's interested"
Prescilla Winston/Development Director/(555)218-3981/said to call again next week
Melissa Potter/Head of Accounts/(555)791-3471/"Not interested, gave referral"

AddresModel.h:

#ifndef ADDRESSMODEL_H
#define ADDRESSMODEL_H
#include <QtGui>
#include <QtCore>
class AddressModel : public QAbstractTableModel{
private:
    QList<QStringList> addressBook;
    void splitCVSLines(QString &);
public:
    AddressModel(QString& addresses, QObject* parent = 0);
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    int columnCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex & index) const;
};
#endif

AddressModel.cpp

#include "AddressModel.h"
#include <iostream>

AddressModel::AddressModel(QString & addresses, QObject* parent ) 
        : QAbstractTableModel(parent){
    splitCVSLines(addresses);
}

void AddressModel::splitCVSLines(QString & addresses){
    QStringList records = addresses.split('n');
    QStringList line;
    foreach(QString record, records){
        addressBook.append(record.split('/'));
    }
    foreach(QStringList strlist, addressBook){
        foreach(QString str, strlist){
            std::cout<<"..."<<str.toStdString()<<"...";
        }
        std::cout<<std::endl;
    }
}
int AddressModel::rowCount(const QModelIndex & parent) const{
    Q_UNUSED(parent);
    return addressBook.count() -2;
}
int AddressModel::columnCount(const QModelIndex & parent) const{
    Q_UNUSED(parent);
    return (addressBook[0].size());
}

QVariant AddressModel::data(const QModelIndex & index, int role) const{
    if(!index.isValid()){
        return QVariant();
    }
    //We are working with the row, always
    QStringList addressRecord = addressBook.at(index.row() +1);
    if(role == Qt::DisplayRole || role == Qt::EditRole){
        return addressRecord.at(index.column());
    }
    if(role == Qt::ToolTipRole){
        QString returnTip, key, value;
        returnTip = "<qt><table>";
        for (int i = 0; i < addressRecord.count(); ++i){
            key = this->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
            value = addressRecord.at(i);
            if(!value.isEmpty()){
                returnTip += QString("<tr><td><b>%1</b>: %2</td></tr>").arg(key, value);
            }
        }
        returnTip += "</table></qt>";
        return returnTip;
    }
    return QVariant();
}
QVariant AddressModel::headerData(int section, Qt::Orientation orientation,
        int role) const{
    if(orientation == Qt::Horizontal){
        if(role == Qt::DisplayRole){
            return (addressBook[0]).at(section);
        }
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}
QModelIndex AddressModel::index(int row, int column, const QModelIndex & parent) const{
    Q_UNUSED(parent);
    return QAbstractTableModel::createIndex(row, column);
}
QModelIndex AddressModel::parent(const QModelIndex & index) const{
    Q_UNUSED(index);
    return QModelIndex();
}

main.cpp

#include <QApplication>
#include <iostream>
#include <fstream>
#include <QtGui>
#include <QtCore>
#include "AddressModel.h"
using namespace std;
    int main(int argc, char *argv[]){
        if(argc != 2){
            return std::cerr<<"No!"<<std::endl;
        }
        QApplication a(argc,argv);
        ifstream file;
        file.open(argv[1]);
        string line;
        string alltext;
        while(file){
            getline(file, line);
            alltext += "n";
            alltext += line;
        }
        file.close();
        QString filetext(QString::fromStdString(alltext));
        AddressModel g(filetext);
        QTableView table;
        table.setModel(&g);
        table.show();
        return a.exec();
    }

解决方案:在main.cpp中,交换while循环中的行。

while(getline(file, line)){
  alltext += line;
  alltext += "n";
}

解释:

只显示第一列,因为您的columnCount函数当前返回1。

在main函数内部的while循环中,您将在alltext字符串的开头添加'n'字符。在splitCVSLines函数中,您首先通过'n'字符进行分割。因此,当循环遍历记录时,第一个记录的列数为1,而其余记录的列数为4。

通过交换while循环中的两行,第一个记录将不再是空行,因此columnCount函数将返回4,从而显示所有数据。

我还移动了getline作为while循环的条件。这有助于防止最后一条记录被显示两次。

希望这对你有帮助。