QueryResultModel inherits from QAbstractTableModel now instead of BaseTableModel and the tableview now gets a custom delegate.
This commit is contained in:
parent
3ac1d21728
commit
208883462c
3 changed files with 51 additions and 41 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
#include "QueryResultModel.h"
|
#include "QueryResultModel.h"
|
||||||
#include "ResultTableModelUtil.h"
|
#include "ResultTableModelUtil.h"
|
||||||
#include "Pgsql_declare.h"
|
#include "Pgsql_declare.h"
|
||||||
|
#include "Pgsql_oids.h"
|
||||||
#include "PgDatabaseCatalog.h"
|
#include "PgDatabaseCatalog.h"
|
||||||
#include <QBrush>
|
#include <QBrush>
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
|
|
@ -9,7 +10,7 @@ using namespace Pgsql;
|
||||||
|
|
||||||
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r
|
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r
|
||||||
, std::shared_ptr<PgDatabaseCatalog> cat)
|
, std::shared_ptr<PgDatabaseCatalog> cat)
|
||||||
: BaseTableModel(parent)
|
: QAbstractTableModel(parent)
|
||||||
, result(std::move(r))
|
, result(std::move(r))
|
||||||
, catalog(cat)
|
, catalog(cat)
|
||||||
{}
|
{}
|
||||||
|
|
@ -27,48 +28,51 @@ int QueryResultModel::columnCount(const QModelIndex &) const
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
Oid QueryResultModel::getType(int column) const
|
//Oid QueryResultModel::getType(int column) const
|
||||||
{
|
//{
|
||||||
return result->type(column);
|
// return result->type(column);
|
||||||
}
|
//}
|
||||||
|
|
||||||
QVariant QueryResultModel::getData(const QModelIndex &index) const
|
//QVariant QueryResultModel::getData(const QModelIndex &index) const
|
||||||
{
|
//{
|
||||||
QVariant r;
|
// QVariant r;
|
||||||
int rij = index.row();
|
// int rij = index.row();
|
||||||
int col = index.column();
|
// int col = index.column();
|
||||||
if (result->null(col, rij)) {
|
// return r;
|
||||||
r = "null";
|
//}
|
||||||
}
|
|
||||||
else {
|
|
||||||
Oid o = result->type(col);
|
|
||||||
auto value = result->get(col, rij);
|
|
||||||
switch (o) {
|
|
||||||
case BOOLOID:
|
|
||||||
r = bool(value); //s = (s == "t") ? "TRUE" : "FALSE";
|
|
||||||
break;
|
|
||||||
default: {
|
|
||||||
QString s = value;
|
|
||||||
if (s.length() > 256) {
|
|
||||||
s.truncate(256);
|
|
||||||
}
|
|
||||||
r = s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if (role == Qt::ForegroundRole) {
|
// static const QString null_str("null");
|
||||||
int rij = index.row();
|
QVariant v;
|
||||||
int col = index.column();
|
|
||||||
|
const int col = index.column();
|
||||||
|
const int rij = index.row();
|
||||||
|
const Oid oid = result->type(col);
|
||||||
|
if (role == Qt::DisplayRole) {
|
||||||
if (result->null(col, rij)) {
|
if (result->null(col, rij)) {
|
||||||
return QBrush(Qt::gray);
|
//v = nullptr;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
auto value = result->val(col, rij);
|
||||||
|
if (oid == bool_oid) {
|
||||||
|
bool b = value;
|
||||||
|
v = b;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
QString s = value;
|
||||||
|
if (s.length() > 256) {
|
||||||
|
s.truncate(256);
|
||||||
|
}
|
||||||
|
v = s;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return BaseTableModel::data(index, role);
|
else if (role == Qt::UserRole) {
|
||||||
|
v = oid;
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation, int role) const
|
QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
|
|
@ -78,7 +82,7 @@ QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation,
|
||||||
if (orientation == Qt::Horizontal) {
|
if (orientation == Qt::Horizontal) {
|
||||||
QString s(result->getColName(section));
|
QString s(result->getColName(section));
|
||||||
s += "\n";
|
s += "\n";
|
||||||
s += getTypeDisplayString(*catalog, getType(section));
|
s += getTypeDisplayString(*catalog, result->type(section));
|
||||||
r = s;
|
r = s;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
#ifndef QUERYRESULTMODEL_H
|
#ifndef QUERYRESULTMODEL_H
|
||||||
#define QUERYRESULTMODEL_H
|
#define QUERYRESULTMODEL_H
|
||||||
|
|
||||||
//#include <QAbstractTableModel>
|
#include <QAbstractTableModel>
|
||||||
#include "BaseTableModel.h"
|
#include "BaseTableModel.h"
|
||||||
#include "Pgsql_Connection.h"
|
#include "Pgsql_Connection.h"
|
||||||
|
|
||||||
class PgDatabaseCatalog;
|
class PgDatabaseCatalog;
|
||||||
|
|
||||||
class QueryResultModel : public BaseTableModel
|
class QueryResultModel : public QAbstractTableModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
|
@ -20,8 +20,8 @@ public:
|
||||||
// virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
// virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual Oid getType(int column) const override;
|
// virtual Oid getType(int column) const override;
|
||||||
virtual QVariant getData(const QModelIndex &index) const override;
|
// virtual QVariant getData(const QModelIndex &index) const override;
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Pgsql::Result> result;
|
std::shared_ptr<Pgsql::Result> result;
|
||||||
std::shared_ptr<PgDatabaseCatalog> catalog;
|
std::shared_ptr<PgDatabaseCatalog> catalog;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
#include "PgLabItemDelegate.h"
|
||||||
|
|
||||||
|
|
||||||
TuplesResultWidget::TuplesResultWidget(QWidget *parent) :
|
TuplesResultWidget::TuplesResultWidget(QWidget *parent) :
|
||||||
QWidget(parent),
|
QWidget(parent),
|
||||||
|
|
@ -11,6 +13,9 @@ TuplesResultWidget::TuplesResultWidget(QWidget *parent) :
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
ui->lblRowCount->setText(QString());
|
ui->lblRowCount->setText(QString());
|
||||||
|
|
||||||
|
auto delegate = new PgLabItemDelegate(ui->ResultView);
|
||||||
|
ui->ResultView->setItemDelegate(delegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
TuplesResultWidget::~TuplesResultWidget()
|
TuplesResultWidget::~TuplesResultWidget()
|
||||||
|
|
@ -22,6 +27,7 @@ void TuplesResultWidget::setResult(std::shared_ptr<QueryResultModel> res, float
|
||||||
{
|
{
|
||||||
resultModel = res;
|
resultModel = res;
|
||||||
ui->ResultView->setModel(resultModel.get());
|
ui->ResultView->setModel(resultModel.get());
|
||||||
|
|
||||||
ui->ResultView->resizeColumnsToContents();
|
ui->ResultView->resizeColumnsToContents();
|
||||||
|
|
||||||
QString rowcount_str = QString("rows: %1").arg(resultModel->rowCount());
|
QString rowcount_str = QString("rows: %1").arg(resultModel->rowCount());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue