Results 1 to 6 of 6

Thread: QTableView alignment items

  1. #1
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView alignment items

    Quote Originally Posted by repaco View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  6. #6
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [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..

    Qt Code:
    1. #include <QItemDelegate>
    To copy to clipboard, switch view to plain text mode 

    ...more code

    Qt Code:
    1. class mycoldelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. mycoldelegate(QObject *parent);
    8.  
    9. void paint(QPainter *painter,
    10. const QStyleOptionViewItem &option,
    11. const QModelIndex &index) const;
    12. };
    To copy to clipboard, switch view to plain text mode 

    In voci.cpp:

    ...more code...

    Qt Code:
    1. void Voci::lista() /* function retreving data from DB */
    2. {
    3. QSqlQueryModel *mod_grid = new QSqlQueryModel;
    4.  
    5. mod_grid->setQuery("select * from voci order by ordine");
    6. mod_grid->setHeaderData(0, Qt::Horizontal, "Codice");
    7. mod_grid->setHeaderData(1, Qt::Horizontal, "Voce");
    8. mod_grid->setHeaderData(2, Qt::Horizontal, "Ordin.");
    9.  
    10. ui->g_lista->setModel(mod_grid);
    11. ui->g_lista->setColumnWidth(0, 60);
    12. ui->g_lista->setColumnWidth(1, 200);
    13. ui->g_lista->setColumnWidth(2, 50);
    14.  
    15. ui->g_lista->setItemDelegateForColumn(2, new mycoldelegate(this)); //Item Delegate
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    ...more code...

    Qt Code:
    1. mycoldelegate::mycoldelegate(QObject *parent)
    2. :QItemDelegate(parent)
    3. {
    4. }
    5.  
    6. void mycoldelegate::paint(QPainter *painter,
    7. const QStyleOptionViewItem & option,
    8. const QModelIndex & index) const
    9. {
    10.  
    11. QString text = index.model()->data(index, Qt::DisplayRole).toString();
    12. QStyleOptionViewItem myOption = option;
    13. myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
    14.  
    15. drawDisplay(painter, myOption, myOption.rect,
    16. text);
    17. drawFocus(painter, myOption, myOption.rect);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    Thank for help.
    Bye
    Last edited by wysota; 13th January 2010 at 22:57. Reason: Missing [code] tags

Similar Threads

  1. QTableView - alignment/selection problem
    By Nesbitt in forum Qt Programming
    Replies: 7
    Last Post: 30th November 2009, 02:37
  2. Replies: 2
    Last Post: 30th July 2009, 10:20
  3. QTableView + QSqlQueryModel column alignment
    By frido in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2008, 06:12
  4. checkable items in QTableView
    By cgorac in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2008, 22:45
  5. Scrolling items in QTableView
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2007, 12:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.