Results 1 to 8 of 8

Thread: QTableView - alignment/selection problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView - alignment/selection problem

    Hi

    Some columns in my QTableView are right aligned and for those column
    selection works wrong when editing cell (I have created my own
    ItemDelegate to handle alignment in edited cell). When I click on the
    left side of the cell (where there is no text) it selects text just like
    the cell is left aligned.

    Example code:
    main.cpp
    Qt Code:
    1. #include <QStandardItemModel>
    2. #include <QItemDelegate>
    3. #include <QLineEdit>
    4. #include <QTableView>
    5. #include <QApplication>
    6.  
    7. class MyDelegate: public QItemDelegate
    8. {
    9. public:
    10. MyDelegate (QObject* parent = 0):QItemDelegate(parent){}
    11. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    12. {
    13. QWidget* w = QItemDelegate::createEditor(parent, option, index);
    14. QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w);
    15. if(lineEdit && index.data(Qt::TextAlignmentRole) == Qt::AlignRight)
    16. lineEdit->setAlignment(Qt::AlignRight);
    17. return w;
    18. }
    19. };
    20.  
    21. int main(int argc,char* argv[])
    22. {
    23. QApplication app(argc, argv);
    24.  
    25.  
    26. for (int row = 0; row < 4; ++row) {
    27. for (int column = 0; column < 3; ++column) {
    28. QStandardItem *item = new QStandardItem(QString("row%0col%1").arg(row).arg(column));
    29. model->setItem(row, column, item);
    30. }
    31. model->setData(model->index(row, 1), Qt::AlignRight, Qt::TextAlignmentRole);
    32. }
    33. QTableView* tv = new QTableView();
    34. tv->resize(400, 300);
    35. tv->setModel(model);
    36. tv->setItemDelegate(new MyDelegate(tv));
    37. tv->setEditTriggers(QAbstractItemView::AllEditTriggers);
    38.  
    39. tv->show();
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

    Example screenshot is in the attachement.

    Regards
    Attached Images Attached Images

Similar Threads

  1. QTreeView problem scrolling to end.
    By seneca in forum Qt Programming
    Replies: 7
    Last Post: 22nd December 2015, 12:08
  2. Problem with KeyPress event and QTableView
    By ranna in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 20:01
  3. Replies: 1
    Last Post: 23rd December 2008, 15:42
  4. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Tags for this Thread

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.