PDA

View Full Version : QTableView - alignment/selection problem



Nesbitt
27th November 2009, 18:29
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


#include <QStandardItemModel>
#include <QItemDelegate>
#include <QLineEdit>
#include <QTableView>
#include <QApplication>

class MyDelegate: public QItemDelegate
{
public:
MyDelegate (QObject* parent = 0):QItemDelegate(parent){}
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QWidget* w = QItemDelegate::createEditor(parent, option, index);
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w);
if(lineEdit && index.data(Qt::TextAlignmentRole) == Qt::AlignRight)
lineEdit->setAlignment(Qt::AlignRight);
return w;
}
};

int main(int argc,char* argv[])
{
QApplication app(argc, argv);

QStandardItemModel* model = new QStandardItemModel(4, 3);

for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 3; ++column) {
QStandardItem *item = new QStandardItem(QString("row%0col%1").arg(row).arg(column));
model->setItem(row, column, item);
}
model->setData(model->index(row, 1), Qt::AlignRight, Qt::TextAlignmentRole);
}
QTableView* tv = new QTableView();
tv->resize(400, 300);
tv->setModel(model);
tv->setItemDelegate(new MyDelegate(tv));
tv->setEditTriggers(QAbstractItemView::AllEditTriggers );

tv->show();
return app.exec();
}


Example screenshot is in the attachement.

Regards

schnitzel
28th November 2009, 00:00
Does the following work better for you?



#include <QStandardItemModel>
#include <QItemDelegate>
#include <QLineEdit>
#include <QTableView>
#include <QApplication>


int main(int argc,char* argv[])
{
QApplication app(argc, argv);

QStandardItemModel* model = new QStandardItemModel(4, 3);

for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 3; ++column) {
QStandardItem *item = new QStandardItem(QString("row%1col%2").arg(row).arg(column));
if (column == 1)
item->setTextAlignment(Qt::AlignRight);
model->setItem(row, column, item);
}

}

QTableView* tv = new QTableView();
tv->resize(400, 300);
tv->setModel(model);
tv->setEditTriggers(QAbstractItemView::AllEditTriggers );

tv->show();
return app.exec();
}

Nesbitt
28th November 2009, 11:04
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.

schnitzel
29th November 2009, 03:18
but it goes back to being right aligned once you finish editing - right?

See if the following works better:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);

ui->tw->setColumnCount(4);
ui->tw->setRowCount(4);

for(int row=0; row < ui->tw->rowCount(); row++)
{
for(int col=0; col < ui->tw->columnCount(); col++)
{
QTableWidgetItem *twi = new QTableWidgetItem(QString("row%1 col%2").arg(row+1).arg(col+1));
if(col == 2)
twi->setTextAlignment(Qt::AlignRight);
ui->tw->setItem(row,col,twi);

}
}
}


I'm attaching the project just in case.

hope this helps. I'm also still learning.

Nesbitt
29th November 2009, 13:47
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.

schnitzel
29th November 2009, 19:59
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.

Nesbitt
29th November 2009, 23:56
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.

schnitzel
30th November 2009, 03:37
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.