#include <QStandardItemModel>
#include <QItemDelegate>
#include <QLineEdit>
#include <QTableView>
#include <QApplication>
{
public:
QWidget* createEditor
(QWidget* parent,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
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[])
{
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 3; ++column) {
model->setItem(row, column, item);
}
model->setData(model->index(row, 1), Qt::AlignRight, Qt::TextAlignmentRole);
}
tv->resize(400, 300);
tv->setModel(model);
tv->setItemDelegate(new MyDelegate(tv));
tv->show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks