Hello!
Currently I'm developing a program with accesses a MySQL-database and with this I encountered one little Problem:
The program displays one table by using a QSqlQueryModel and a QTableView. There is one column which contains doubles. I want these doubles to be displayed via a QItemDelegate which contains a QLabel.
Here is my code:
Header
{
Q_OBJECT
public:
LabelDelegate
(QObject* parent
= 0);
QWidget* createEditor
(QWidget *parent,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const;
void setEditorData
(QWidget* editor,
const QModelIndex
& index
) const;
};
class LabelDelegate : public QItemDelegate
{
Q_OBJECT
public:
LabelDelegate(QObject* parent = 0);
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
void setEditorData(QWidget* editor, const QModelIndex& index) const;
};
To copy to clipboard, switch view to plain text mode
Code
QWidget* LabelDelegate
::createEditor(QWidget* parent,
const QStyleOptionViewItem
&,
const QModelIndex
&) const {
return label;
}
void LabelDelegate
::setEditorData(QWidget* editor,
const QModelIndex
& index
) const {
double value = index.model()->data(index, Qt::EditRole).toInt();
QLabel* label
= static_cast<QLabel
*>
(editor
);
label
->setText
(QString::number(value
) + " €");
}
QWidget* LabelDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
QLabel* label = new QLabel(parent);
return label;
}
void LabelDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
double value = index.model()->data(index, Qt::EditRole).toInt();
QLabel* label = static_cast<QLabel*>(editor);
label->setText(QString::number(value) + " €");
}
To copy to clipboard, switch view to plain text mode
Now I do this:
tableview->setItemDelegateForColumn(1, new LabelDelegate);
tableview->setItemDelegateForColumn(1, new LabelDelegate);
To copy to clipboard, switch view to plain text mode
But the values are still displayed normally, I can't see the '€' Symbol.
Does anybody have an idea how to fix this?
Thanks!
Bookmarks