HorizontalHeaderView не отображает данные заголовкаC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 HorizontalHeaderView не отображает данные заголовка

Сообщение Anonymous »

Я пытаюсь создать TableView в QML (Qt 6, Qt Quick Controls 2), который выглядит примерно так:

Изображение

В моей LatticeTableModel есть следующие методы:

Код: Выделить всё

QVariant LatticeTableModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole)
return {};

if (orientation == Qt::Horizontal) {
switch (section) {
case 0: return QStringLiteral("Lattice");
case 1: return QStringLiteral("");
default: return {};
}
}
return {};
}
и

Код: Выделить всё

QVariant LatticeTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole || !m_controller)
return {};

const int r = index.row();
const int c = index.column();

auto labelOrValue = [&](const char* label, const std::string &value) -> QVariant {
return (c == 0) ? QVariant(label) : QVariant(QString::fromStdString(value));
};

switch (r) {
case 0: return labelOrValue("a", std::format("{:.3f} Å", m_controller->a()));
case 1: return labelOrValue("b", std::format("{:.3f} Å", m_controller->b()));
case 2: return labelOrValue("c", std::format("{:.3f} Å", m_controller->c()));
case 3: return labelOrValue("α", std::format("{:.3f} °", m_controller->alpha()));
case 4: return labelOrValue("β", std::format("{:.3f} °", m_controller->beta()));
case 5: return labelOrValue("γ", std::format("{:.3f} °", m_controller->gamma()));
default: return {};
}
}
Мой QML выглядит так:

Код: Выделить всё

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Item {
required property var lController
required property var lModel

ColumnLayout {
anchors.fill: parent
spacing: 6

Label {
font.bold: true
text: "Lattice Information"
}
Frame {
Layout.fillHeight: false
Layout.fillWidth: true
Layout.preferredHeight: 240
padding: 8

Item {
anchors.fill: parent

HorizontalHeaderView {
id: horizontalHeader

anchors.left: tv.left
anchors.right: tv.right
anchors.top: parent.top
clip: true
height: 32
syncView: tv

delegate: Rectangle {
required property int section

border.color: "#00000022"
border.width: 1
color: "#efefef"
implicitHeight: 32
implicitWidth: 200

Text {
anchors.fill: parent
anchors.leftMargin: 8
anchors.rightMargin: 8
elide: Text.ElideRight
font.bold: true
horizontalAlignment: section === 0 ? Text.AlignLeft : Text.AlignRight

text: display
verticalAlignment: Text.AlignVCenter
}
}
}
TableView {
id: tv

anchors.bottom: parent.bottom
anchors.fill: parent
anchors.left: parent.left
anchors.right: parent.right
anchors.top: horizontalHeader.bottom
clip: true
columnSpacing: 0
columnWidthProvider: function (col) {
const first = 30;
const restCols = Math.max(1, tv.columns - 1);
if (col === 0)
return first;
return Math.max(80, (tv.width - first) / restCols);
}
model: lModel
rowHeightProvider: function (row) {
return 28;
}
rowSpacing: 0

delegate: Rectangle {
required property int column
required property var display
required property int row

border.color: "#00000022"
border.width: 1

color: column === 0 ? "#e8f2ff" : (row % 2 === 0 ? "#ffffff" : "#fafafa")
implicitHeight: 28

Text {
anchors.fill: parent
anchors.leftMargin: 8
anchors.rightMargin: 8
elide: Text.ElideRight
horizontalAlignment: column === 0 ? Text.AlignLeft : Text.AlignRight

// Zahlen hübsch formatieren
text: {
if (column === 0)
return display;
const n = Number(display);
return isNaN(n) ? display : n.toFixed(6);
}
verticalAlignment: Text.AlignVCenter
}
}
}
}
}// end Frame
} // end ColumnLayout
} // end Item
Но строки заголовка нет:
Изображение


Подробнее здесь: https://stackoverflow.com/questions/798 ... headerdata
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»