
Originally Posted by
aekilic
I have tried this
connect(recentFileActs[i], SIGNAL(triggered()), this, SLOT(w->spinBox()->setValue(fiyat_tipleri.value(4).toDouble())));
connect(recentFileActs[i], SIGNAL(triggered()), this, SLOT(w->spinBox()->setValue(fiyat_tipleri.value(4).toDouble())));
To copy to clipboard, switch view to plain text mode
But it didnt work

pretty wrong code. 
ok, that's a new variant
h-file
class MyComplexEditor
: public QWidget{
Q_OBJECT
public:
{
m_toolButton->setText("...");
setFocusProxy(m_spinBox);
hbl->setMargin(0);
hbl->setSpacing(0);
hbl->addWidget(m_spinBox);
hbl->addWidget(m_toolButton);
for (int i = 0; i < 4; ++i) {
action->setData(i);
m_toolButton->addAction(action);
connect(action, SIGNAL(triggered()), SLOT(updateSpinBoxValue()));
}
}
QSpinBox *spinBox
() const { return m_spinBox;
} QToolButton *toolButton
() const { return m_toolButton;
}
private slots:
void updateSpinBoxValue()
{
const QAction *action
= qobject_cast<QAction
*>
(sender
());
if (!action)
return;
m_spinBox->setValue(action->data().toInt());
}
private:
};
class MyComplexEditor: public QWidget
{
Q_OBJECT
public:
MyComplexEditor(QWidget *parent = 0) : QWidget(parent)
{
m_spinBox = new QSpinBox();
m_toolButton = new QToolButton();
m_toolButton->setText("...");
QHBoxLayout *hbl = new QHBoxLayout(this);
m_spinBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
m_toolButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
setFocusProxy(m_spinBox);
hbl->setMargin(0);
hbl->setSpacing(0);
hbl->addWidget(m_spinBox);
hbl->addWidget(m_toolButton);
for (int i = 0; i < 4; ++i) {
QAction *action = new QAction(tr("action%1").arg(i), this);
action->setData(i);
m_toolButton->addAction(action);
connect(action, SIGNAL(triggered()), SLOT(updateSpinBoxValue()));
}
}
QSpinBox *spinBox() const { return m_spinBox; }
QToolButton *toolButton() const { return m_toolButton; }
private slots:
void updateSpinBoxValue()
{
const QAction *action = qobject_cast<QAction *>(sender());
if (!action)
return;
m_spinBox->setValue(action->data().toInt());
}
private:
QSpinBox *m_spinBox;
QToolButton *m_toolButton;
};
To copy to clipboard, switch view to plain text mode
cpp-file
{
Q_UNUSED(option);
Q_UNUSED(index);
return new MyComplexEditor(parent);
}
{
MyComplexEditor *mce = qobject_cast<MyComplexEditor *>(editor);
if (!mce)
return;
model->setData(index, mce->spinBox()->value());
}
QWidget *ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
return new MyComplexEditor(parent);
}
void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
MyComplexEditor *mce = qobject_cast<MyComplexEditor *>(editor);
if (!mce)
return;
model->setData(index, mce->spinBox()->value());
}
To copy to clipboard, switch view to plain text mode
select one of action and close editor (click on another item), data in cell will be stored to model.
Bookmarks