Hi
i try to set QDateEdit in cell QTableWidget but doesn't work
//mainwindow
ui.tabela->setItemDelegate(new DateDelegate(5));
ui.tabela->setItem(idx, 5, nazwa5);
//mainwindow
ui.tabela->setItemDelegate(new DateDelegate(5));
QTableWidgetItem *nazwa5 = new QTableWidgetItem(obiekt[idx].item[5]);
ui.tabela->setItem(idx, 5, nazwa5);
To copy to clipboard, switch view to plain text mode
"obiekt[idx].item[5]" it's QString
class DateDelegate
// *.h
#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QDateEdit>
{
Q_OBJECT
public:
DateDelegate
(int durationColumn,
QObject *parent
= 0);
// void paint(QPainter *painter, const QStyleOptionViewItem &option,
// const QModelIndex &index) const;
private slots:
void commitAndCloseEditor();
private:
int durationColumn;
};
// *.h
#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QDateEdit>
class DateDelegate : public QItemDelegate
{
Q_OBJECT
public:
DateDelegate(int durationColumn, QObject *parent = 0);
// void paint(QPainter *painter, const QStyleOptionViewItem &option,
// const QModelIndex &index) const;
QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
private slots:
void commitAndCloseEditor();
private:
int durationColumn;
};
To copy to clipboard, switch view to plain text mode
// *.cpp
#include "datedelegate.h"
#include <QtGui>
DateDelegate
::DateDelegate(int durationColumn,
QObject *parent
){
this->durationColumn = durationColumn;
}
{
if(index.column() == 5 ){
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
if(index.column() == 6 ){
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
if(index.column() == 7 ){
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
}
void DateDelegate::commitAndCloseEditor()
{
QDateEdit *editor
= qobject_cast<QDateEdit
*>
(sender
());
emit commitData(editor);
emit closeEditor(editor);
}
void DateDelegate
::setEditorData(QWidget *editor,
{
QDateTimeEdit *dateEditor
= qobject_cast<QDateTimeEdit
*>
(editor
);
QString value
= index.
model()->data
(index, Qt
::EditRole).
toString();
dateEditor
->setDate
(QDate::fromString(value ,
"dd.MM.yyyy"));
}
void DateDelegate
::setModelData(QWidget *editor,
{
QDateTimeEdit *dateEditor
= qobject_cast<QDateTimeEdit
*>
(editor
);
QString value
= dateEditor
->date
().
toString("dd.MM.yyyy");
model->setData(index, value, Qt::EditRole);
}
// *.cpp
#include "datedelegate.h"
#include <QtGui>
DateDelegate::DateDelegate(int durationColumn, QObject *parent)
: QItemDelegate(parent)
{
this->durationColumn = durationColumn;
}
QWidget *DateDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.column() == 5 ){
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
if(index.column() == 6 ){
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
if(index.column() == 7 ){
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
}
void DateDelegate::commitAndCloseEditor()
{
QDateEdit *editor = qobject_cast<QDateEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void DateDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
QString value = index.model()->data(index, Qt::EditRole).toString();
dateEditor->setDate(QDate::fromString(value , "dd.MM.yyyy"));
}
void DateDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
QString value = dateEditor->date().toString("dd.MM.yyyy");
model->setData(index, value, Qt::EditRole);
}
To copy to clipboard, switch view to plain text mode
sorry for my english
thx for help
Bookmarks