class CEditLineButton : public QFrame{ Q_OBJECTpublic: explicit CEditLineButton(QWidget *parent = 0); ~CEditLineButton (); void setTextToEditLine (QString text);signals: void clickButton ();public slots: private slots: void clicked ();private: QLineEdit *lineEdit; QToolButton *button; QHBoxLayout *layout;};//реализация#include "ceditlinebutton.h"CEditLineButton::CEditLineButton(QWidget *parent) : QFrame(parent){ setFrameShape(QFrame::NoFrame); lineEdit = new QLineEdit (this); button = new QToolButton (this); layout = new QHBoxLayout (this); layout->setMargin(0); layout->setSpacing(0); layout->addWidget(lineEdit); layout->addWidget(button); button->setSizePolicy (QSizePolicy::Fixed,QSizePolicy::Minimum); lineEdit->setSizePolicy( QSizePolicy::Preferred,QSizePolicy::Preferred); connect (button, SIGNAL (clicked()), this, SLOT (clicked())); button->setText("..."); //lineEdit->setFocus(); lineEdit->setReadOnly(false); // lineEdit->setFocusProxy(button);}CEditLineButton::~CEditLineButton(){ delete lineEdit; delete button;}void CEditLineButton::clicked(){ emit clickButton();}void CEditLineButton::setTextToEditLine(QString text){ lineEdit->setText(text);}
// Класс-делегат, который реализует интерфейс доступа к справочнику// когда класс CCatalogPerson участвует как ссылочный (пример, класс Пользователи)class DelegatPerson : public QItemDelegate{ Q_OBJECTpublic: DelegatPerson (int id, 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; virtual void paint (QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;public slots: void showPesonCatalog ();private: CCatalogPerson catPerson; // ViewPerson *editor;};//реализацияDelegatPerson::DelegatPerson (int id, QObject *parent) : QItemDelegate (parent){ //TODO: в дальнейшем нужно реализовать чтобы //в форме справочника текущим был элемент с id}QWidget *DelegatPerson::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ CEditLineButton *editor = new CEditLineButton (parent); editor->setTextToEditLine(index.data(Qt::EditRole).toString()); // editor->installEventFilter(const_cast<DelegatPerson*> (this)); connect (editor, SIGNAL (clickButton()), this, SLOT (showPesonCatalog())); //connect (this, SIGNAL (commitData(editor)), this, SLOT (showPesonCatalog())); return editor;//layout->widget();}void DelegatPerson::setEditorData(QWidget *editor, const QModelIndex &index) const{ CEditLineButton *myEdit = static_cast<CEditLineButton*> (editor); myEdit->setTextToEditLine(index.data(Qt::EditRole).toString());}void DelegatPerson::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{}void DelegatPerson::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{ editor->setGeometry(option.rect);}void DelegatPerson::paint ( QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { QString text; QRect rect; QVariant value; QStyleOptionViewItemV2 opt = setOptions(index, option); value = index.data(Qt::DisplayRole); text = value.toString();//QLocale().toString(value.toString()); opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; // painter->drawText(); drawDisplay(painter, opt, opt.rect, text);}void DelegatPerson::showPesonCatalog(){ ViewPerson *personTable = new ViewPerson (); personTable->setModel(catPerson.getModel()); personTable->setItemDelegate(new QSqlRelationalDelegate (personTable)); personTable->setAlternatingRowColors(true); personTable->show();}
connect (personTable, SIGNAL (clicked (const QModelIndex & )), this, SLOT (setRelationData(const QModelIndex &)));