2017-11-19 12 views
0

私はに関連付けられたQSqlTableModelを使用しています。QtableViewのQtでカスタムQProxyを使用して列をマージする方法は?

QTableViewの同じ列にfirstNamesecondNameを表示する必要があります。 私はまた別に、これらのフィールドで検索を行う必要があり、それが問題だ:私はちょうど私のSQLクエリでCONCAT(lastName, ' ', secondName)を使用することはできません、私は、現在、いくつかのデータ構造で別々に

class MainWindow(QMainWindow): 
    def __init__(self): 
     QMainWindow.__init__(self) 

     self.ui = Ui_MainWindow() 
     self.ui.setupUi(self) 

     db = QSqlDatabase.addDatabase("QMYSQL") 
     db.setHostName("...") 
     db.setDatabaseName("...") 
     db.setUserName("...") 
     db.setPassword("...") 
     db.open() 

     model.setQuery(u"""SELECT 
         CONCAT(c.firstName, ' ', c.lastName, ' ', c.patrName), 
         CONCAT(c.birthDate, '/', TIMESTAMPDIFF(YEAR, c.birthDate, NOW())), 
         IF(c.sex = 1, 'M', 'F'), 
         CONCAT(p.serial, '-', p.number), 
         CONCAT(d.serial, '-', d.number)       
         FROM client AS c 
         JOIN clientpolicy AS p ON c.id = p.client_id 
         JOIN clientdocument AS d ON c.id = d.client_id""", db) 

     self.ui.patientsTableView.setModel(model) 

firstNamesecondNameを維持する必要があります例えば、私はCであなたのテーブルモデルに

typedef struct sUserFullName 
{ 
    QString oFistName; 
    QString oLastName; 
}USER_FULLNAME; 
Q_DECLARE_METATYPE(USER_FULLNAME); 

class MyCusumeModel : public QSqlTableModel 
{ 
    Q_OBJECT 

private: 

    QList<USER_FULLNAME> m_oAllUsersNames; 

    const QString& GetFirstName(int row) const 
    { 
     return m_oAllUsersNames.at(row).oFistName; 
    } 
    const QString& GetLastName(int row) const 
    { 
     return m_oAllUsersNames.at(row).oLastName; 
    } 

    public: 

    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; 

}; 

ハンドル列データを、独自のデータ構造を作成secondName

+0

あなたはいくつかのコードを提供することはできますか?構造体をデータ構造体として使用できます。または[QPair](http://doc.qt.io/qt-5/qpair.html) – Simon

答えて

0

でテーブルをソートしたいですPP:姓でソートQSortFilterProxyModel

例でオーバーライドlessThanをソートするため

QVariant MyCusumeModel::data(const QModelIndex &index, int role) const 
{ 
    // Valid index ? 
    if (!index.isValid()) 
     return QVariant(); 

    // Validate size overflow. 
    if (index.row() >= m_oAllUsersNames.size()) { 
     return QVariant(); 
    } 

    // Handle column data 
    switch (index.column()) { 

    case 0: // Combine first + last Name 
    if (role == Qt::DisplayRole) 
     return QString("%1, %2").arg(GetFirstName(index.row())).arg(GetLastName(index.row())); 
    if (role == Qt::UserRole) 
     return m_oAllUsersNames(index.row()); // store data 
    break; 

    default: 
    break; 
} 
return QVariant(); 

} 

bool MyCustomeSortModel::lessThan(const QModelIndex &oLeft, const QModelIndex &oRight) const 
{ 
    USER_FULLNAME sLeftData = sourceModel()->data(oLeft, Qt::UserRole).value<USER_FULLNAME>(); 
    USER_FULLNAME sRightData = sourceModel()->data(oRight,Qt::UserRole).value<USER_FULLNAME>(); 

    switch (oLeft.column()) { 

    case 0: // Sort by last name 
     return sLeftData.oLastName < sRightData.oLastName; 
     break; 

    default: 
     break; 
    } 

    // Sort any other supported type 
    return sourceModel()->data(oLeft, Qt::DisplayRole) < sourceModel()->data(oRight, Qt::DisplayRole); 
} 
+0

私は 'QSqlTableModel'を持っていますので、私はそのためのプロキシを作りたいと思います。 –

+0

元のモデルの列と行を知っているプロキシの 'data'メソッドから' QModelIndex'を取得できますか? –

+0

右、 'class MyCusumeModel:public QSqlTableModel'をオーバーライドし、' data'関数をオーバーライドしてテーブルデータを処理します – Simon

関連する問題