QPushButton delegate en QTableView
Hola,
Tengo una aplicación de escritorio que simula un CRUD y uso un buttondelegate
para actualizar y borrar. La questión es que para cada delegate me despliega un
botón de más.
1.- clase ButtonColumnDelegate
- {
- {
- myWidget = tableView;
- btn->show();
- myWidget->setMouseTracking(true);
- isOneCellInEditMode = false;
- }
- }
- ButtonColumnDelegate::~ButtonColumnDelegate()
- {
- }
- //createEditor
- {
- if ((header == "Update") || (header == "Insert"))
- {
- btn->setText(index.data().toString());
- return btn;
- } else {
- }
- }
- //setEditorData
- {
- if ((header == "Update") || (header == "Insert"))
- {
- btn->setProperty("data_value", index.data());
- } else {
- }
- }
- //setModelData
- {
- if ((header == "Update") || (header == "Insert"))
- {
- model->setData(index, btn->property("data_value"));
- } else {
- }
- }
- //paint
- void ButtonColumnDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- if((header == "Update") || (header == "Insert"))
- {
- btn->setGeometry(option.rect);
- btn->setText(index.data().toString());
- painter->fillRect(option.rect, option.palette.highlight());
- painter->drawPixmap(option.rect.x(),option.rect.y(),map);
- } else {
- }
- }
- //updateGeometry
- void ButtonColumnDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- editor->setGeometry(option.rect);
- }
- //cellEntered
- {
- if((header == "Update") || (header == "Insert"))
- {
- if(isOneCellInEditMode)
- {
- myWidget->closePersistentEditor(currentEditedCellIndex);
- }
- myWidget->openPersistentEditor(index);
- isOneCellInEditMode = true;
- currentEditedCellIndex = index;
- } else {
- if(isOneCellInEditMode)
- {
- isOneCellInEditMode = false;
- myWidget->closePersistentEditor(currentEditedCellIndex);
- }
- }
- }
Está tomado de internet adding-button-to-qviewtable [qtadventures.wordpress.com].
2.- clase Browser (la ventana de la aplicación)
- void Browser::exec()
- {
- if (myModel->query().isSelect()) {
- // Create the data model
- table->setModel(model);
- ButtonColumnDelegate *update = new ButtonColumnDelegate(table);
- ButtonColumnDelegate *insert = new ButtonColumnDelegate(table);
- table->setItemDelegateForColumn(2, update);
- table->setItemDelegateForColumn(3, insert);
- model->insertRows(0, myModel->query().size());
- model->insertColumns(0, 4);
- // Set the localized header captions
- // ...
- }
Y duplica el botón de la ultima fila.
Gracias de antemano,
oggie
2 replies
Creo que posiblemente la causa esté en la lógica de la función,
- //cellEntered
- {
- if((header == "Update") || (header == "Insert"))
- {
- if(isOneCellInEditMode)
- {
- myWidget->closePersistentEditor(currentEditedCellIndex);
- }
- myWidget->openPersistentEditor(index);
- isOneCellInEditMode = true;
- currentEditedCellIndex = index;
- } else {
- if(isOneCellInEditMode)
- {
- isOneCellInEditMode = false;
- myWidget->closePersistentEditor(currentEditedCellIndex);
- }
- }
- }
No estoy seguro dado que soy nuevo en esto pero posiblemente la variable booleana isOneCellInEditMode no esté bien controlada y habilite la opción de editar un botón de más.
Cualquier sugerencia será gratamente apreciada.
oggie
Al final preferí usar esta otra clase con las correciones que aparecen en el código y ya funciona.
customitemdelegate.h.-
- #ifndef CUSTOMITEMDELEGATE_H
- #define CUSTOMITEMDELEGATE_H
- #include <QItemDelegate>
- {
- Q_OBJECT
- public:
- bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
- signals:
- public slots:
- private:
- QRect oldRect;
- };
- #endif // CUSTOMITEMDELEGATE_H
customitemdelegate.cpp.-
- #include "customitemdelegate.h"
- #include <QModelIndex>
- #include <QStandardItemModel>
- #include <QStandardItem>
- #include <QPainter>
- #include <QStyleOptionButton>
- #include <QApplication>
- #include <QEvent>
- #include <QDebug>
- #include <QMouseEvent>
- {
- }
- {
- //const QStandardItemModel* model = static_cast<const QStandardItemModel*>(index.model());
- //QStandardItem* item = model->item(index.row());
- //QString text = item->text();
- //QRect textRect(rect);
- //textRect.setHeight(30);
- //painter->drawText(textRect,text);
- painter->drawText(rect,text);
- //buttonRect.setY(textRect.y() + 35);
- //buttonRect.setY(rect.y() + 35);
- buttonRect.setHeight(30);
- QStyleOptionButton button;
- button.rect = buttonRect;
- button.text = text;
- }
- {
- }
- bool CustomItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
- {
- qDebug() << "editorEvent on row: " << index.row();
- qDebug() << "editorEvent:: " << event->type();
- } else {
- return true;
- }
- //buttonRect.setY(option.rect.y() + 35);
- buttonRect.setHeight(30);
- if( !buttonRect.contains( mouseEvent->pos()) ) {
- return true;
- }
- if(index.data().toString().contains("Update"))
- emit updButtonClicked(index);
- else if(index.data().toString().contains("Delete"))
- emit delButtonClicked(index);
- else
- qDebug() << "¡¡¡ --ERROR-- !!!" << index.row();
- }
- return true;
- }
No duplicaba el ultimo botón sino que los había desplazado en la columna hacia abajo.
Gracias,
oggie
You must log in to post a reply. Not a member yet? Register here!
