QTableView alignment items
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.
Re: QTableView alignment items
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()
Re: QTableView alignment items
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
Re: QTableView alignment items
Quote:
Originally Posted by
repaco
I'd like to align data in the grid (and also apply a numeric format).
Implement a custom delegate then.
Quote:
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.
Re: QTableView alignment items
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.
[Solved] QTableView alignment items
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..
Code:
#include <QItemDelegate>
...more code
Code:
{
Q_OBJECT
public:
};
In voci.cpp:
...more code...
Code:
void Voci::lista() /* function retreving data from DB */
{
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...
Code:
mycoldelegate
::mycoldelegate(QObject *parent
){
}
void mycoldelegate
::paint(QPainter *painter,
{
QString text
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect,
text);
drawFocus(painter, myOption, myOption.rect);
}
Thank for help.
Bye