Results 1 to 8 of 8

Thread: QTableView - alignment/selection problem

  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

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView - alignment/selection problem

    Does the following work better for you?

    Qt Code:
    1. #include <QStandardItemModel>
    2. #include <QItemDelegate>
    3. #include <QLineEdit>
    4. #include <QTableView>
    5. #include <QApplication>
    6.  
    7.  
    8. int main(int argc,char* argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12.  
    13. for (int row = 0; row < 4; ++row) {
    14. for (int column = 0; column < 3; ++column) {
    15. QStandardItem *item = new QStandardItem(QString("row%1col%2").arg(row).arg(column));
    16. if (column == 1)
    17. item->setTextAlignment(Qt::AlignRight);
    18. model->setItem(row, column, item);
    19. }
    20.  
    21. }
    22.  
    23. QTableView* tv = new QTableView();
    24. tv->resize(400, 300);
    25. tv->setModel(model);
    26. tv->setEditTriggers(QAbstractItemView::AllEditTriggers);
    27.  
    28. tv->show();
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView - alignment/selection problem

    Thanks but it deosn't work at all. After clicking on the right alligned cell the text is left alignment. That is why I have created my own delegate.

  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView - alignment/selection problem

    but it goes back to being right aligned once you finish editing - right?

    See if the following works better:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5.  
    6. ui->tw->setColumnCount(4);
    7. ui->tw->setRowCount(4);
    8.  
    9. for(int row=0; row < ui->tw->rowCount(); row++)
    10. {
    11. for(int col=0; col < ui->tw->columnCount(); col++)
    12. {
    13. QTableWidgetItem *twi = new QTableWidgetItem(QString("row%1 col%2").arg(row+1).arg(col+1));
    14. if(col == 2)
    15. twi->setTextAlignment(Qt::AlignRight);
    16. ui->tw->setItem(row,col,twi);
    17.  
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    I'm attaching the project just in case.

    hope this helps. I'm also still learning.
    Attached Files Attached Files
    Last edited by schnitzel; 29th November 2009 at 06:19. Reason: updated contents

  5. #5
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView - alignment/selection problem

    Quote Originally Posted by schnitzel View Post
    but it goes back to being right aligned once you finish editing - right?
    No. That is not right. Is should be right aligned all the time. That is why I have created my own delegate.

    I have no problem with alignment. The problem is that text selection in right aligned cell doesn't work. See my example.

  6. #6
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView - alignment/selection problem

    sorry, but maybe I'm not getting it. See attached picture.
    In both cases, when I type text it is indeed left aligned, but once you hit enter, the new text is right aligned. Just curious why that is such a problem for you.
    Attached Images Attached Images

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

    Default Re: QTableView - alignment/selection problem

    Quote Originally Posted by schnitzel View Post
    sorry, but maybe I'm not getting it. See attached picture.
    In both cases, when I type text it is indeed left aligned, but once you hit enter, the new text is right aligned. Just curious why that is such a problem for you.
    Ok. One more time:
    I have no problem with alignment. The problem is that text selection in right aligned cell doesn't work. Please see my example. Copy it, paste to main.cpp, complie and run. You will see that there is no problem with alignment.

    Notice also that I have used AllEditTriggers so there is no need to double click on the cell.

    Run my program move cursor to the first cell in the middle column (which is right aligned), place mouse cursor bewteen letters "o" and "w" and then click. This selects whole word and text cursor is at the end of the word.

    This should select only "ro" part of the word and the text cursor should be between "o" and "w" (text cursor should be under mouse cursor).

    Now do the same in left aligned column and you will see the difference.

  8. #8
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView - alignment/selection problem

    Yes, I can even reproduce it in my example once I select the 'AllEditKeys'.

    Is there a particular reason why you want to set it to AllEditKeys?

    By default it is set to DoubleClicked|EditKeyPressed|AnyKeyPressed, which I find works reasonable.

    Have you tried a different input widget instead of line edit? Maybe those are better for customizing the behavior you want.

    sorry I can't be of more help.

Similar Threads

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