PDA

View Full Version : QTableView alignment items



repaco
11th January 2010, 23:54
Dear Sirs,

I'm newbie programmer. Linux platform.

I'd like to know how is possible to align (and formatting) items in QTableView , hoping without writing Kilotons of code just for alignment.

Data are from MySql.

Here is a peice of code I'm using:

***********

void Voci::lista() // function to retreive data from SQL and present into a Dialog Windows
{
QSqlQueryModel *mod_grid = new QSqlQueryModel;

mod_grid->setQuery("select * from voci order by ordine");
mod_grid->setHeaderData(0, Qt::Horizontal, "Codice"); // Primary Key Field char()
mod_grid->setHeaderData(1, Qt::Horizontal, "Descrizione"); // Descriptions char()
mod_grid->setHeaderData(2, Qt::Horizontal, "Importo", Qt::AlignRight); // Amount num() to be aligned right

ui->g_lista->setModel(mod_grid);
ui->g_lista->setColumnWidth(0, 60);
ui->g_lista->setColumnWidth(1, 200);
ui->g_lista->setColumnWidth(2, 50);

}

************

ui->g_lista is Tableview object name in Dialog windows.

Alignment does'n have effect. Plus I have no idea on formatting amount (from 1234,5 to 1'234,50) in tableview.

Googled a lot with no answers. Do you know where I can find siutable examples. In Trolltech Docs no matter.

Tks in advance

Ciao.

wysota
12th January 2010, 00:02
Do you want to align the header or the data?

Basically you'll have two choices - either providing a custom delegate that will do custom painting or subclassing the model and reimplementing QAbstractItemModel::data()

repaco
12th January 2010, 09:22
Hi wysota,
Thanks for quick reply.

No, do not intend to align header.

I'd like to align data in the grid (and also apply a numeric format).

I'm using Qt Designer to define Dialog Windows (and QtCreator for coding, but it doesn't matter).

In Dialog Windows (using Qt Designer) I've designed my windows (buttons, QLineEdit, etc) and I've placed a QTableView objet (object name g_lista) using designer tools.

To retrive data from SQL, in the *.cpp I'm using QSqlQueryModel creating a mod_grid object.

So in *.cpp I have two object: mod_grid (with data) and g_lista (to present data). If I subclass QAbstractItemModel witch kind of object I get? A view Object?

If so, how can I interact with my g_lista object in the form?

Many Tanks

wysota
12th January 2010, 10:31
I'd like to align data in the grid (and also apply a numeric format).
Implement a custom delegate then.


If I subclass QAbstractItemModel witch kind of object I get? A view Object?

If so, how can I interact with my g_lista object in the form?

Hmm.... these are basic C++ questions and have nothing to do with Qt. You should be able to answer them yourself. If you can't you should polish your C++ skills a bit.

repaco
12th January 2010, 11:58
Thanks.

It is what I'm going to do, improve my c++ knowledge ;-)).

This evening I try to apply your suggestion.

I'm trying to make an Open Source Application. I'm using QT because it seems to me better for my goal.

I hope to finish.

Ciao.

repaco
13th January 2010, 22:27
Dear Wysota,
solved with item delegate. I post for newbie like me.

With QT Designer I've created a windows named voci with QTableview object named g_lista.

Then in voci.h I coded:

...more code..


#include <QItemDelegate>

...more code


class mycoldelegate : public QItemDelegate
{
Q_OBJECT

public:

mycoldelegate(QObject *parent);

void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};

In voci.cpp:

...more code...


void Voci::lista() /* function retreving data from DB */
{
QSqlQueryModel *mod_grid = new QSqlQueryModel;

mod_grid->setQuery("select * from voci order by ordine");
mod_grid->setHeaderData(0, Qt::Horizontal, "Codice");
mod_grid->setHeaderData(1, Qt::Horizontal, "Voce");
mod_grid->setHeaderData(2, Qt::Horizontal, "Ordin.");

ui->g_lista->setModel(mod_grid);
ui->g_lista->setColumnWidth(0, 60);
ui->g_lista->setColumnWidth(1, 200);
ui->g_lista->setColumnWidth(2, 50);

ui->g_lista->setItemDelegateForColumn(2, new mycoldelegate(this)); //Item Delegate

}

...more code...


mycoldelegate::mycoldelegate(QObject *parent)
:QItemDelegate(parent)
{
}

void mycoldelegate::paint(QPainter *painter,
const QStyleOptionViewItem & option,
const QModelIndex & index) const
{

QString text = index.model()->data(index, Qt::DisplayRole).toString();
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;

drawDisplay(painter, myOption, myOption.rect,
text);
drawFocus(painter, myOption, myOption.rect);

}

Thank for help.
Bye