ich bin neu in der QT Gemeinde und habe schon direkt eine Frage
Also, eine MySQL Verbindung mit Anzeige in einer Tabelle aufzubauen war nicht sehr schwer. Nun brauche ich aber Checkboxen in der Tabelle. Jetzt habe ich in meinem QT4 Buch (das von Gallieo Press) gelesen, dass so etwas mit delegate lösbar wäre.
Mein Versuch dazu:
Im Konstruktor des Hauptfensters (in dem die Tabelle dargestellt wird):
Code: Alles auswählen
AufgabenDelegate delegate;
ui.tblAufgaben->setItemDelegate(&delegate);
Code: Alles auswählen
/*
* aufgabendelegate.h
*
* Created on: 09.09.2008
* Author: mcfloppy
*/
#ifndef AUFGABENDELEGATE_H_
#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QLineEdit>
class AufgabenDelegate : public QItemDelegate
{
Q_OBJECT
public:
AufgabenDelegate(QObject *parent = 0);
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;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
#define AUFGABENDELEGATE_H_
#endif /* AUFGABENDELEGATE_H_ */
Code: Alles auswählen
/*
* aufgabendelegate.cpp
*
* Created on: 09.09.2008
* Author: mcfloppy
*/
#include <QtGui>
#include "aufgabendelegate.h"
#include <QItemDelegate>
AufgabenDelegate::AufgabenDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *AufgabenDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QLineEdit *editor = new QLineEdit(parent);
return editor;
//return QItemDelegate::createEditor(parent, option, index);
}
void AufgabenDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::DisplayRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
//QItemDelegate::setEditorData(editor, index);
}
void AufgabenDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value);
//QItemDelegate::setModelData(editor, model, index);
}
void AufgabenDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
Könnt ihr mir weiterhelfen?