QML TableView get data from specific cell (selected row + specific column)

Данила Колганов

I have QML TableView with QSqlQueryModel. I need to select any row and get data from every column of table to separate TextField.

Here is abonentstable.h:

#pragma once

#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());
    QVariant data(const QModelIndex &index, int role) const;
    QHash<int, QByteArray> roleNames() const { return m_roleNames; }

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

abonentstable.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::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;
}

Table.qml:

TableView {
    id: table

    model: abonents

    ....

    TableViewColumn {
        delegate: Text {
            text: " " + model.name + " " + model.surname
            font.pointSize: 20
        }    
        width: 575
    }

    TableViewColumn {
        delegate: Text {
            text: " " + model.phone
            font.pointSize: 20
        }    
        width: 575
    }

    TableViewColumn {
        delegate: Text {
            text: " " + model.ip_address
            font.pointSize: 20
        }    
        width: 525
    }
}

And some text fields:

TextField {
    id:  leftText
}

TextField {
     id:  centerText
}

TextField {
    id:  rightText
}

This sqlite table has 4 columns, and I need to get data from selected row to those text fields: 2 columns to left, 1 to center and 1 to right.

Tarod

When the user clicks a valid row it is emitted the clicked(int row) signal. Then, you can get the values for that row, format the text depending on your requirements and set the values in the three TextField. For example, in your case:

TableView {
    id: table

    model: abonents

    ... (your TableViewColumn components) ...    

    onClicked: {
        leftText.text = abonents.get(row).name + " " + libraryModel.get(row).surname;
        centerText.text = abonents.get(row).phone;
        rightText.text = abonents.get(row).ip_address;
    }
}

Please, let me please use my own answer to show you a complete example:

import QtQuick 2.2
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

ApplicationWindow {
    id: window
    visible: true
    title: "Table View Example"

    TableView {
        y: 70
        width: 500

        TableViewColumn {
            role: "title"
            title: "Title"
            width: 100
        }
        TableViewColumn {
            role: "author"
            title: "Author"
            width: 100
        }

        TableViewColumn{
            width: 300
            delegate: Text {
                text: model.title + " "  + model.author
                font.family: "Courier New"
                font.pixelSize: 18
                color: "red"
            }
        }

        onClicked: {
            leftText.text = libraryModel.get(row).title + " " + libraryModel.get(row).author;
            centerText.text = libraryModel.get(row).title;
            rightText.text = libraryModel.get(row).author;
        }

        model: libraryModel

        ListModel {
            id: libraryModel
            ListElement {
                title: "A Masterpiece"
                author: "Gabriel"
            }
            ListElement {
                title: "Brilliance"
                author: "Jens"
            }
            ListElement {
                title: "Outstanding"
                author: "Frederik"
            }
        }
    }

    TextField {
        id:  leftText
    }

    TextField {
        id:  centerText
        anchors.left: leftText.right
    }

    TextField {
        id:  rightText
        anchors.left: centerText.right
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get data from a specific column in a TableView QML

From Dev

Export specific data from a selected cell's row into a CSV file

From Dev

Export specific data from a selected cell's row into a CSV file

From Dev

Get data of specific cell from selected row in DataGrid WPF C#

From Dev

TableView Cell - mark specific row as selected when page loads

From Dev

Get a specific value from a specific column from specific row?

From Dev

Get Value from a specific cell in a specific column using jquery

From Dev

datatable get specific column from row

From Dev

Get specific column when Kendo Grid row is selected

From Dev

Formula to get cell values of specific column and a variable row number

From Dev

JavaFX How to get data of selected row from Tableview

From Dev

Get the value of a specific row and specific column from datagrid

From Dev

How to get specific data in a column by only using data from one cell

From Dev

Suming a specific TableView column/row in JavaFX

From Dev

Get row from a table by td class and specific value in the cell in jquery

From Dev

With XPath, get a button from a row, that has a cell with a specific text

From Dev

Get specific data from Excel column

From Dev

How do I get specific data from a specific column in MySQL?

From Dev

Copy formula from a specific sheet and cell and drag it down until specific column last row

From Dev

Replace data on specific column and row

From Dev

How to get the row index of last non-empty cell for a specific column name(passed as an argument) from an excel sheet using java?

From Dev

ECHO specific row from specific column

From Dev

I would like to click a button located on a specific row in a table and get data displayed in a specific cell of the table

From Dev

Loop through column of tableview and get cell data

From Dev

How to Get data of specific cell from DataGrid that fill by multiple tables

From Dev

A cell contains the number of row and I want to get the value of that row on specific column

From Dev

QML - get tableview cell content

From Dev

How to get specific column and row value from multiple text files?

From Dev

Get specific column from the last inserted row with PDO

Related Related

  1. 1

    Get data from a specific column in a TableView QML

  2. 2

    Export specific data from a selected cell's row into a CSV file

  3. 3

    Export specific data from a selected cell's row into a CSV file

  4. 4

    Get data of specific cell from selected row in DataGrid WPF C#

  5. 5

    TableView Cell - mark specific row as selected when page loads

  6. 6

    Get a specific value from a specific column from specific row?

  7. 7

    Get Value from a specific cell in a specific column using jquery

  8. 8

    datatable get specific column from row

  9. 9

    Get specific column when Kendo Grid row is selected

  10. 10

    Formula to get cell values of specific column and a variable row number

  11. 11

    JavaFX How to get data of selected row from Tableview

  12. 12

    Get the value of a specific row and specific column from datagrid

  13. 13

    How to get specific data in a column by only using data from one cell

  14. 14

    Suming a specific TableView column/row in JavaFX

  15. 15

    Get row from a table by td class and specific value in the cell in jquery

  16. 16

    With XPath, get a button from a row, that has a cell with a specific text

  17. 17

    Get specific data from Excel column

  18. 18

    How do I get specific data from a specific column in MySQL?

  19. 19

    Copy formula from a specific sheet and cell and drag it down until specific column last row

  20. 20

    Replace data on specific column and row

  21. 21

    How to get the row index of last non-empty cell for a specific column name(passed as an argument) from an excel sheet using java?

  22. 22

    ECHO specific row from specific column

  23. 23

    I would like to click a button located on a specific row in a table and get data displayed in a specific cell of the table

  24. 24

    Loop through column of tableview and get cell data

  25. 25

    How to Get data of specific cell from DataGrid that fill by multiple tables

  26. 26

    A cell contains the number of row and I want to get the value of that row on specific column

  27. 27

    QML - get tableview cell content

  28. 28

    How to get specific column and row value from multiple text files?

  29. 29

    Get specific column from the last inserted row with PDO

HotTag

Archive