无法使用QTableView查看所有列,我的子类继承自QAbstractTableModel。
Can't view all Colums using QTableView and my subclass which inherits from QAbstractTableModel
我正在学习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循环的条件。这有助于防止最后一条记录被显示两次。
希望这对你有帮助。
相关文章:
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- 如何识别从不同子类覆盖的向量类?
- 运算符的歧义错误<<自定义 std::ostream 子类中的重载
- 父类和子类中的 C++ 同名成员
- 返回具体子类实例的 C++ 虚拟类函数
- 将方法参数类型更改为子类中的派生类
- 使用子类中的类型定义扩展模板类
- 如何在 BaseClass 指针数组中存储指向子类对象的指针?
- 子类QGraphicsScene的错误
- 基类中的虚拟方法和子类中的非 Virtula 方法.仍然如何调用子类方法,即使它是非虚拟的
- 如何在 getter 的父类中初始化变量的情况下访问子类中的变量
- 避免复制子类中的所有构造函数
- 来自子类 QGraphicsView 的信号在插槽中无法识别
- 从子类中的抽象类调用方法
- 如何在Qt中处理子类中的动态内存分配
- 我构造子类对象的方式有什么问题?
- 我如何命名父班的成员(库),以供父母和子类中的可读性
- 我可以从子类以外的另一个类调用抽象基类的公共赋值运算符吗?
- Qt子类化的图形场景不允许我选择/聚焦到它上面的图形项目