【问题标题】:Display several roles in one column of TableView在 TableView 的一列中显示多个角色
【发布时间】:2015-12-17 09:32:30
【问题描述】:

我有一个带有 4 列的 SQLite 3 数据库和带有 TableView 的 QML 代码来显示它:

TableView {
    id: table
    ...

    TableViewColumn {
        role: "name"            
        width: 275
    }
    TableViewColumn {
        role: "surname"
        width: 300
    }
    TableViewColumn {
        role: "phone"
        width: 575
    }
    TableViewColumn {
        role: "ip_address"
        width: 525
    }
    model: abonents
}

它工作正常,但我需要将前两个角色namesurname 显示为TableView 中的唯一列。

这是我的模型的代码和main

abonentstable.h:

#ifndef ABONENTSTABLE
#define ABONENTSTABLE

#include <QObject>
#include <QSqlQueryModel>

class AbonentsSqlModel : public QSqlQueryModel
{
    Q_OBJECT

public:
    explicit AbonentsSqlModel(QObject *parent = 0);

    void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
    void setQuery(const QSqlQuery &query);
    QVariant data(const QModelIndex &index, int role) const;
    QHash<int, QByteArray> roleNames() const { return m_roleNames; }

signals:

public slots:

private:
    void generateRoleNames();
    QHash<int, QByteArray> m_roleNames;
};

#endif // ABONENTSTABLE

dbconnection.cpp:

#include "abonentstable.h"

#include <QSqlRecord>
#include <QSqlField>

AbonentsSqlModel::AbonentsSqlModel(QObject *parent) :
    QSqlQueryModel(parent)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("data_base.sqlite");
    db.open();
}

void AbonentsSqlModel::setQuery(const QString &query, const QSqlDatabase &db)
{
    QSqlQueryModel::setQuery(query, db);
    generateRoleNames();
}

void AbonentsSqlModel::setQuery(const QSqlQuery & query)
{
    QSqlQueryModel::setQuery(query);
    generateRoleNames();
}

void AbonentsSqlModel::generateRoleNames()
{
    m_roleNames.clear();
    for( int i = 0; i < record().count(); i ++) {
        m_roleNames.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
    }
}

QVariant AbonentsSqlModel::data(const QModelIndex &index, int role) const
{
    QVariant value;

    if(role < Qt::UserRole) {
        value = QSqlQueryModel::data(index, role);
    }

    else {
        int columnIdx = role - Qt::UserRole - 1;
        QModelIndex modelIndex = this->index(index.row(), columnIdx);
        value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    }

    return value;
}

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QVariant>
#include <QSql>
#include <QSqlQueryModel>
#include <QObject>
#include "abonentstable.h"


int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    AbonentsSqlModel *abonentsSqlModel = new AbonentsSqlModel(0);
    abonentsSqlModel->setQuery("SELECT * FROM abonents");

    QQmlContext *context = engine.rootContext();
    context->setContextProperty("abonents", abonentsSqlModel);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

【问题讨论】:

    标签: qt tableview qml roles qtquick2


    【解决方案1】:

    我认为您可以使用 delegate 组件连接这两个值。

    在你的情况下:

    TableView {
        id: table
        ...
    
        TableViewColumn {           
            width: 575
            delegate: Text { text: model.name + " "  + model.surname }
        }       
        TableViewColumn {
            role: "phone"
            width: 575
        }
        TableViewColumn {
            role: "ip_address"
            width: 525
        }
        model: abonents
    }
    

    这里有另一个例子,只是为了测试你是否想使用它。该示例基于此Qt example

    ma​​in.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    

    ma​​in.qml

    import QtQuick 2.2
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    ApplicationWindow {
        id: window
        visible: true
        title: "Table View Example"
    
        TableView {
            TableViewColumn {
                role: "title"
                title: "Title"
                width: 100
            }
            TableViewColumn {
                role: "author"
                title: "Author"
                width: 100
            }
    
            TableViewColumn{
                width: 300
                delegate: Text { text: model.title + " "  + model.author }
            }
    
            TableViewColumn{
                width: 300
                delegate: Text {
                    text: model.title + " "  + model.author
                    font.family: "Courier New"
                    font.pixelSize: 18
                    color: "red"
                }
            }
    
            model: libraryModel
    
            ListModel {
                id: libraryModel
                ListElement {
                    title: "A Masterpiece"
                    author: "Gabriel"
                }
                ListElement {
                    title: "Brilliance"
                    author: "Jens"
                }
                ListElement {
                    title: "Outstanding"
                    author: "Frederik"
                }
            }
        }
    
    }   
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 2012-02-21
      • 1970-01-01
      • 2014-08-31
      相关资源
      最近更新 更多