PDA

View Full Version : QSqlRelationalDelegate not display my QWidget, only onclick



patrik08
8th March 2007, 13:47
I want to display a button on my table ....

how i must set paint ?... to display direct QWidget ... not only on click.....:(

if (index.column() == 0) {
int xnummer = index.model()->data(index, Qt::DisplayRole).toInt();
this->createEditor(tas, option, index);

}


http://ppk.ciz.ch/qt_c++/editfield.JPG






BaseDelegate::BaseDelegate( QTableView *ta , QObject *parent )
:QSqlRelationalDelegate(parent)
{
tas = ta;
}

void BaseDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
/////////qDebug() << "### index-column " << index.column();

if (index.column() == 0) {
int xnummer = index.model()->data(index, Qt::DisplayRole).toInt();
this->createEditor(tas, option, index);

} else if (index.column() == 1) {
QString coder = index.model()->data(index, Qt::DisplayRole).toString();
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
myOption.palette.setColor( QPalette::Text , Qt::red );
drawDisplay(painter, myOption, myOption.rect,coder);
drawFocus(painter, myOption, myOption.rect);

} else if (index.column() == 8) {
QString anno = index.model()->data(index, Qt::DisplayRole).toString();
//////////////QString coda = BerufHuman(coder);
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
if (anno !="0") {
myOption.palette.setColor( QPalette::Text , Qt::gray );
} else {
myOption.palette.setColor( QPalette::Text , Qt::white );
}
drawDisplay(painter, myOption, myOption.rect,anno);
drawFocus(painter, myOption, myOption.rect);

} else{
QItemDelegate::paint(painter, option, index);
}
}

QWidget *BaseDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 11 || index.column() == 7 ) {
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
return editor;
} else if (index.column() == 0) {
Base_Button *editora = new Base_Button(parent);
return editora;
} else {
return QItemDelegate::createEditor(parent, option, index);
}
}

void BaseDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{

if (index.column() == 11 || index.column() == 7 ) {
QString dateuser = index.model()->data(index, Qt::DisplayRole).toString();
QDateTimeEdit *editorrun = qobject_cast<QDateTimeEdit *>(editor);
if (editorrun) {
editorrun->setDate(QDate::fromString(index.model()->data(index, Qt::EditRole).toString(), "dd.MM.yyyy"));
}

} else if (index.column() == 0) {
int xnummer = index.model()->data(index, Qt::DisplayRole).toInt();
Base_Button *editorax = qobject_cast<Base_Button *>(editor);
editorax->SetNummer(xnummer);
} else {
QItemDelegate::setEditorData(editor, index);
}
}

void BaseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.column() == 11 || index.column() == 7 ) {
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
if (dateEditor) {
model->setData(index,dateEditor->date().toString("dd.MM.yyyy"));
}
} else if (index.column() == 0) {
Base_Button *editorax = qobject_cast<Base_Button *>(editor);
model->setData(index,editorax->GetNummer());
} else {
QItemDelegate::setModelData(editor, model, index);
}
}


class Base_Button: public QWidget
{
Q_OBJECT
//
public:
QPushButton *pushButton;

Base_Button( QWidget *parent )
: QWidget(parent)
{
setAttribute(Qt::WA_WindowPropagation);
QVBoxLayout *widgetLayout = new QVBoxLayout(this);
widgetLayout->setMargin(0);
widgetLayout->setSpacing(0);
pushButton = new QPushButton(this);
pushButton->setText(tr("Edit Nr. 0"));
widgetLayout->addWidget(pushButton);
}
void SetNummer( int numero ) {
identificativo = numero;
pushButton->setText(tr("Edit Nr. %1").arg(identificativo));
}
int GetNummer() {
return identificativo;
}
private:
int identificativo;
QSize sizebase;
protected:
signals:
public slots:

};


class BaseDelegate : public QSqlRelationalDelegate
{
Q_OBJECT

public:
BaseDelegate( QTableView *ta , QObject *parent );
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:
private:
QTableView *tas;
QObject *sap;

};

wysota
8th March 2007, 15:02
Either use setIndexWidget or openPersistentEditor.

patrik08
8th March 2007, 15:15
Either use setIndexWidget or openPersistentEditor.


Super .:) ... You have answer for all qt question! if You have answer for all theme this is Danger!!! :eek:



void BaseDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{


if (index.column() == 0) { /* tas parent qtablewiev*/
tas->setColumnWidth (0,105);
tas->openPersistentEditor(index);
return;
} else if (index.column() == 1) {
QString coder = index.model()->data(index, Qt::DisplayRole).toString();
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
myOption.palette.setColor( QPalette::Text , Qt::red );
drawDispl..........................

wysota
8th March 2007, 15:54
Just please don't paste so much unrelevant code in your posts (like in the first post in this thread). Nobody will read it anyway, so there is no point posting it. If you want to include some code, then make it as short (and simple) as possible.