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 "ResultTableModelUtil.h"
|
||||
#include "Pgsql_declare.h"
|
||||
#include "Pgsql_oids.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include <QBrush>
|
||||
#include <QColor>
|
||||
|
|
@ -9,7 +10,7 @@ using namespace Pgsql;
|
|||
|
||||
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r
|
||||
, std::shared_ptr<PgDatabaseCatalog> cat)
|
||||
: BaseTableModel(parent)
|
||||
: QAbstractTableModel(parent)
|
||||
, result(std::move(r))
|
||||
, catalog(cat)
|
||||
{}
|
||||
|
|
@ -27,48 +28,51 @@ int QueryResultModel::columnCount(const QModelIndex &) const
|
|||
return r;
|
||||
}
|
||||
|
||||
Oid QueryResultModel::getType(int column) const
|
||||
{
|
||||
return result->type(column);
|
||||
}
|
||||
//Oid QueryResultModel::getType(int column) const
|
||||
//{
|
||||
// return result->type(column);
|
||||
//}
|
||||
|
||||
QVariant QueryResultModel::getData(const QModelIndex &index) const
|
||||
//QVariant QueryResultModel::getData(const QModelIndex &index) const
|
||||
//{
|
||||
// QVariant r;
|
||||
// int rij = index.row();
|
||||
// int col = index.column();
|
||||
// return r;
|
||||
//}
|
||||
|
||||
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
QVariant r;
|
||||
int rij = index.row();
|
||||
int col = index.column();
|
||||
// static const QString null_str("null");
|
||||
QVariant v;
|
||||
|
||||
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)) {
|
||||
r = "null";
|
||||
//v = nullptr;
|
||||
}
|
||||
else {
|
||||
auto value = result->val(col, rij);
|
||||
if (oid == bool_oid) {
|
||||
bool b = value;
|
||||
v = b;
|
||||
}
|
||||
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;
|
||||
v = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
return r;
|
||||
else if (role == Qt::UserRole) {
|
||||
v = oid;
|
||||
}
|
||||
|
||||
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (role == Qt::ForegroundRole) {
|
||||
int rij = index.row();
|
||||
int col = index.column();
|
||||
if (result->null(col, rij)) {
|
||||
return QBrush(Qt::gray);
|
||||
}
|
||||
}
|
||||
return BaseTableModel::data(index, role);
|
||||
return v;
|
||||
}
|
||||
|
||||
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) {
|
||||
QString s(result->getColName(section));
|
||||
s += "\n";
|
||||
s += getTypeDisplayString(*catalog, getType(section));
|
||||
s += getTypeDisplayString(*catalog, result->type(section));
|
||||
r = s;
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
#ifndef QUERYRESULTMODEL_H
|
||||
#define QUERYRESULTMODEL_H
|
||||
|
||||
//#include <QAbstractTableModel>
|
||||
#include <QAbstractTableModel>
|
||||
#include "BaseTableModel.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
|
||||
class QueryResultModel : public BaseTableModel
|
||||
class QueryResultModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
|
@ -20,8 +20,8 @@ public:
|
|||
// virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
|
||||
protected:
|
||||
virtual Oid getType(int column) const override;
|
||||
virtual QVariant getData(const QModelIndex &index) const override;
|
||||
// virtual Oid getType(int column) const override;
|
||||
// virtual QVariant getData(const QModelIndex &index) const override;
|
||||
private:
|
||||
std::shared_ptr<Pgsql::Result> result;
|
||||
std::shared_ptr<PgDatabaseCatalog> catalog;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include "PgLabItemDelegate.h"
|
||||
|
||||
|
||||
TuplesResultWidget::TuplesResultWidget(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
|
|
@ -11,6 +13,9 @@ TuplesResultWidget::TuplesResultWidget(QWidget *parent) :
|
|||
{
|
||||
ui->setupUi(this);
|
||||
ui->lblRowCount->setText(QString());
|
||||
|
||||
auto delegate = new PgLabItemDelegate(ui->ResultView);
|
||||
ui->ResultView->setItemDelegate(delegate);
|
||||
}
|
||||
|
||||
TuplesResultWidget::~TuplesResultWidget()
|
||||
|
|
@ -22,6 +27,7 @@ void TuplesResultWidget::setResult(std::shared_ptr<QueryResultModel> res, float
|
|||
{
|
||||
resultModel = res;
|
||||
ui->ResultView->setModel(resultModel.get());
|
||||
|
||||
ui->ResultView->resizeColumnsToContents();
|
||||
|
||||
QString rowcount_str = QString("rows: %1").arg(resultModel->rowCount());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue