在 QSqlQueryModel 中交换标头(转置表) [英] Swapping headers in QSqlQueryModel (transpose table)

查看:59
本文介绍了在 QSqlQueryModel 中交换标头(转置表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正确转置从 db 收到的表时遇到问题.我遵循了在 here 中找到的路径,并最终将 aQAbstractProxyModel - 如此处所述.不幸的是,它不能完全正常工作,问题出在这里:

I am having a problem to properly transpose the table I recieve from db. I followed the path found here , and ended up in subclassing a QAbstractProxyModel - like described here . unfortunatelly, it doesn't fully work, here's where the problem is:

我有什么:

X | A  | B
----------
1 | A1 | B1
2 | A2 | B2

我想要的:

X | 1  | 2
----------
A | A1 | A2
B | B1 | B2

我得到了什么:

X | 1  | 1
----------
A | A1 | A2
A | B1 | B2

如您所见,数据已正确转置,但标题变坏了......我真的需要它们:(

So as you can see, the data is correctly transposed, but the headers get bad... And I really need them :(

我尝试手动设置标题数据,但也失败了:

I tried to manually set header data, but it also failed:

origModel = new QSqlQueryModel; // set query and so on
transposedModel = new TransposeProxyModel;
transposedModel->setSourceModel(origModel );
for (int i = 0; i < origModel->columnCount(); i++) {
    qDebug() << "origModel->Qt::Horizontal(" << i << ")" << origModel->headerData(i, Qt::Horizontal, Qt::DisplayRole);
    //transposedModel->setHeaderData(i, Qt::Vertical, origModel->headerData(i, Qt::Horizontal, Qt::DisplayRole), Qt::DisplayRole); //#try1
    transposedModel->setHeaderData(i, Qt::Vertical, QVariant( "abc" ), Qt::DisplayRole); // #try2
}

无论我尝试 #1 还是 #2 - 对 setHeaderData 的调用计算结果为 false...

No matter if I try #1, or #2 - call to setHeaderData evaluates to false...

有什么想法吗?

修正为@Vinícius Gobbo A. de Oliveira 指出

fixed as @Vinícius Gobbo A. de Oliveira pointed

推荐答案

好吧,如果您使用链接的示例代码,您应该覆盖 TransposeProxyModel 类的 headerData 方法的默认定义,就像这样:

Well, if you used the example code you linked you should override the default definition for the headerData method of the TransposeProxyModel class, just like this:

QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const {
    return sourceModel()->headerData(section, (orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal), role);
}

忘记 setHeaderData 和 setData 方法:您需要一个依赖于原始模型的代理模型!

Forget about the setHeaderData and setData methods: you want a proxy model that rely on the original one!

好吧,不知道到底出了什么问题,您使用的是哪个 Qt 版本?试试这个代码,它在这里完美运行:

Well, don't know exactly what's wrong, which Qt release are you using? Try this code, it works perfectly here:

#include <QtCore>
#include <QtWidgets>

class TransposeProxyModel: public QAbstractProxyModel {
public:
    TransposeProxyModel(QObject *p = 0):
        QAbstractProxyModel(p)
    {
    }
    QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const{
        return index(sourceIndex.column(), sourceIndex.row());
    }
    QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const{
        return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
    }
    QModelIndex index(int r, int c, const QModelIndex &ind=QModelIndex()) const{
        return createIndex(r,c);
    }
    QModelIndex parent(const QModelIndex&) const {
        return QModelIndex();
    }
    int rowCount(const QModelIndex &) const{
        return sourceModel()->columnCount();
    }
    int columnCount(const QModelIndex &) const{
        return sourceModel()->rowCount();
    }
    QVariant data(const QModelIndex &ind, int role) const {
        return sourceModel()->data(mapToSource(ind), role);
    }
    QVariant headerData(int section, Qt::Orientation orientation,
        int role = Qt::DisplayRole) const {
        return sourceModel()->headerData(section,
            (orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal),
            role);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QStandardItemModel model(3,3);
    model.setData(model.index(0,0), "1");
    model.setData(model.index(0,1), "2");
    model.setData(model.index(0,2), "3");
    model.setData(model.index(1,0), "4");
    model.setData(model.index(1,1), "5");
    model.setData(model.index(1,2), "6");
    model.setData(model.index(2,0), "7");
    model.setData(model.index(2,1), "8");
    model.setData(model.index(2,2), "9");
    model.setHeaderData(0, Qt::Horizontal, "a");
    model.setHeaderData(1, Qt::Horizontal, "b");
    model.setHeaderData(2, Qt::Horizontal, "c");
    TransposeProxyModel trans;
    trans.setSourceModel(&model);
    QSplitter split;
    QTableView *t1 = new QTableView(&split);
    t1->setModel(&model);
    QTableView *t2 = new QTableView(&split);
    t2->setModel(&trans);
    split.show();
    return a.exec();
}

大部分代码来自你提供的链接,我只是写了 headerData 方法.

Most of the code came from the link you provided, I just wrote the headerData method.

这篇关于在 QSqlQueryModel 中交换标头(转置表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆