QT6.8.0,Ubuntu 24.10。 我写了一个简单的代表,以在a
QSqlTableModel
中显示。
它以给定数量的小数为特定的浮点数格式化。
issue
当表将列的宽度调整为内容时,它会忽略新的内容大小(由于添加了新的小数,大于初始内容)。
工程和完整的mre
Mainwindow.h
QTableView
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlRelationalTableModel>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QSqlRelationalTableModel *_model = nullptr;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "delegatereal.h"
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSqlRelationalDelegate>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "mydb");
db.setDatabaseName("mydb.sql");
db.open();
QSqlQuery query("DROP TABLE IF EXISTS foo", db);
query.exec();
QString sql = "CREATE TABLE foo \
(name TEXT NOT NULL, \
v FLOAT NOT NULL, \
PRIMARY KEY (name));";
query.prepare(sql);
query.exec();
_model = new QSqlRelationalTableModel(this, db);
_model->setTable("foo");
_model->setEditStrategy(QSqlRelationalTableModel::OnRowChange);
ui->table->setSortingEnabled(false);
ui->table->setModel(_model);
ui->table->setItemDelegate(new QSqlRelationalDelegate(this));
ui->table->setItemDelegateForColumn(1, new DelegateReal(5));
ui->table->setFocusPolicy(Qt::TabFocus);
query.exec("INSERT INTO foo (name, v) VALUES ('aaa', 1.234);");
query.exec("INSERT INTO foo (name, v) VALUES ('bbb', 2.345);");
query.exec("INSERT INTO foo (name, v) VALUES ('ccc', 3.465);");
_model->select();
ui->table->resizeColumnsToContents();
}
MainWindow::~MainWindow()
{
delete ui;
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" rowspan="2">
<widget class="QTableView" name="table"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
trials
QRect
我还尝试设置
updateEditorGeometry()
函数内的宽度:
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QRect rect(option.rect);
rect.setWidth(100);
editor->setGeometry(rect);
}
实际上会扩大彩绘区域,但仍然错误地调整了圆柱:
问题为了更新内容的几何形状,我需要更改什么?
提出代表的点是能够设置小数的数量(或其他更改显示的文本),您可以重新进来paint()
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
opt.text = opt.locale.toString(opt.locale.toFloat(opt.text), 'f', _decimals);
opt.rect.setWidth(100);
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
}
。反过来,这是从默认实现中调用的。 反过来,QStyledItemDelegate::displayText()
paint()
中更改文本值,则出于相同的原因,即自动选择该值。