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