Results 1 to 6 of 6

Thread: QTableView alignment items

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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
  •  
Qt is a trademark of The Qt Company.