[Qt-interest] QTableWidgetItem with Validator (only numbers) and Range
David Soler
beavies at gmail.com
Sat May 2 20:57:32 CEST 2009
Hi!
Well I think it could be done easily using QItemDelegate in order to do:
- When user clicks to edit a cell of the table, the program invoke your
delegate class that construct the QDoubleSpinBox with your preferences.
So at most you'll have to do:
- make a subclass of QItemDelegate
- Use the tableWidget->setItemDelegateForColumn(your_delegate) (or
setItemDelegateForRow or something)..
The "problem" is that your delegate will act in all cells of the column (or
row) of the table.
A custom delegate that i use is, for example, a date editor in tables:
#include "dateDelegate.h"
dateDelegate::dateDelegate(QObject *parent) : QItemDelegate(parent)
{
}
dateDelegate::~dateDelegate()
{
}
//here i create my editor
QWidget * dateDelegate::createEditor(QWidget *parent, const
QStyleOptionViewItem &option, const QModelIndex &index) const
{
QDateEdit *dateedit = new QDateEdit(parent);
dateedit->setDate(QDate::currentDate());
dateedit->setCalendarPopup(true);
connect(dateedit, SIGNAL(dateChanged(const QDate &)), this,
SLOT(commitAndCloseEditor(const QDate &)));
return dateedit;
}
// here i put a string (kk) from the table converted to a QDate for may editor
(_DATA function does that)
void dateDelegate::setEditorData(QWidget *editor, const QModelIndex &index)
const
{
CGlobal *g = CGlobal::getInstance();
QString kk = index.model()->data(index, Qt::DisplayRole).toString();
QDate d = g->_DATA(kk);
QDateEdit *dedit = qobject_cast<QDateEdit*>(editor);
dedit->setDate(d);
}
// here i convert the date of the editor to a string to be viewed in the cell
of the table when it isn't in edit mode
void dateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateEdit *dedit = qobject_cast<QDateEdit*>(editor);
model->setData(index, dedit->date().toString("dd/MM/yyyy"));
}
//Inform that the data of the cell has changed
void dateDelegate::commitAndCloseEditor(const QString & str)
{
QDateEdit *editor = qobject_cast<QDateEdit*>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
Hope it helps!
bye!
--
David
beavies at gmail.com
On Saturday 02 May 2009 20:25:34 Matthias Pospiech wrote:
> I want to have cells in a QTableWidget that accept only numbers in a
> specified range. Think of a QDoubleSpinBox without the buttons.
>
> I could not see from the docs that such functionality is built into the
> QTableWidgetItem. So I tried to add a QDoubleSpinBox instead of a QString
> for the constructor, but that fails.
>
> QDoubleSpinBox * spinbox = new QDoubleSpinBox(this);
> spinbox->setRange(0,20000);
> spinbox->setDecimals(2);
> spinbox->setValue(power);
> newItem = new QTableWidgetItem(spinbox);
>
> instead of
>
> newItem = new QTableWidgetItem(QString("%1").arg(power));
>
> So, how do I add the functionality of the QDoubleSpinBox into the
> QTableWidgetItem. The docs say something about subclassing, but I have
> no idea how that should possibly look like. Exept that I could reinvent
> the wheel and copy most of QDoubleSpinBox code into a subclassed
> class of QTableWidgetItem...
>
> Seems to me like a standard problem. Maybe someone has a solution for me?
>
> Matthias
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list