gpt4 book ai didi

qt - QML TableView 从特定单元格(选定行 + 特定列)获取数据

转载 作者:行者123 更新时间:2023-12-03 19:56:03 25 4
gpt4 key购买 nike

我有 QML TableViewQSqlQueryModel .我需要选择任何行并从表的每一列中获取数据以分隔 TextField .

这是 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;
}

表.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
}
}

还有一些文本字段:
TextField {
id: leftText
}

TextField {
id: centerText
}

TextField {
id: rightText
}

这个 sqlite 表有 4 列,我需要从选定行获取数据到这些文本字段:2 列向左,1 列居中,1 列向右。

最佳答案

当用户单击有效行时,它会发出 clicked(int row)信号。然后,您可以获得该 row 的值。 , 根据您的要求格式化文本并设置三个 TextField 中的值.例如,在您的情况下:

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;
}
}

请让我用我的 own answer向您展示一个完整的示例:
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
}
}

关于qt - QML TableView 从特定单元格(选定行 + 特定列)获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32956108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com