class OBJECTINSPECTORSHARED_EXPORT QPropertyEditWidget : public QWidget{ Q_OBJECT QToolButton * feditButton; QToolButton * fresetButton; QWidget * fwidget; QWidget * fcontainer; QHBoxLayout * flayout;protected: void resizeEvent(QResizeEvent *event);public: explicit QPropertyEditWidget(QWidget *parent = 0); QWidget * widget() const; void setEditable(bool Value = true); void setResatable(bool Value = true);signals: void editButtonClicked(); void resetButtonClicked(); public slots: void setWidget(QWidget * Widget); };
#include <QResizeEvent>#include "qpropertyeditwidget.h"QPropertyEditWidget::QPropertyEditWidget(QWidget *parent) : QWidget(parent){ fwidget = NULL; flayout = new QHBoxLayout(this); flayout->setContentsMargins(0,0,0,0); flayout->setSpacing(1); fcontainer = new QWidget(this); fcontainer->setAutoFillBackground(true); feditButton = new QToolButton(this); feditButton->setIcon(QIcon(":/icons/pencil")); feditButton->setMaximumSize(26,26); feditButton->setMinimumSize(10,10); feditButton->setVisible(false); fresetButton = new QToolButton(this); fresetButton->setText("*"); fresetButton->setMaximumSize(10,26); fresetButton->setMinimumSize(5,10); fresetButton->setVisible(false); flayout->addWidget(fcontainer,1); flayout->addWidget(feditButton); flayout->addWidget(fresetButton); setFocusProxy(feditButton); setLayout(flayout); connect(feditButton,SIGNAL(clicked()),this,SIGNAL(editButtonClicked())); connect(fresetButton,SIGNAL(clicked()),this,SIGNAL(resetButtonClicked()));}QWidget *QPropertyEditWidget::widget() const{ return fwidget;}void QPropertyEditWidget::setEditable(bool Value){ feditButton->setVisible(Value); updateGeometry();}void QPropertyEditWidget::setResatable(bool Value){ fresetButton->setVisible(Value); updateGeometry();}void QPropertyEditWidget::setWidget(QWidget *Widget){ if(fwidget!=Widget) { if(fwidget) { fwidget->setParent(NULL); } fwidget = Widget; if(fwidget) { fwidget->setParent(fcontainer); updateGeometry(); } }}//Это для отладки, так работает (не это же костыли... + не учитываются размеры 2х кнопок)void QPropertyEditWidget::resizeEvent(QResizeEvent * event){ if(fwidget) fwidget->setGeometry(0,0,event->size().width(),event->size().height());}
QWidget *CObjectInspectorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ Q_UNUSED(option)...if(currentProperty.isEnumType()) { QPropertyEditWidget * enumEditWidget = new QPropertyEditWidget(parent); enumEditWidget->setResatable(currentProperty.isResettable()); QMetaEnum currentEnumerator = currentProperty.enumerator(); QComboBox * comboBox = new QComboBox(); //comboBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); for(int i=0;i<currentEnumerator.keyCount();i++) { comboBox->addItem(currentEnumerator.key(i)); } connect(comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(setStringValue(QString))); connect(enumEditWidget,SIGNAL(resetButtonClicked()),this,SLOT(resetProperty())); enumEditWidget->setWidget(comboBox); return enumEditWidget; }
class OBJECTINSPECTORSHARED_EXPORT QPropertyEditWidget : public QWidget{ Q_OBJECT QToolButton * feditButton; QToolButton * fresetButton; QWidget * fwidget; QWidget * fcontainer; QHBoxLayout * flayout;protected: void resizeEvent(QResizeEvent *event); void recalculate();public: explicit QPropertyEditWidget(QWidget *parent = 0); QWidget * widget() const; void setEditable(bool Value = true); void setResatable(bool Value = true);signals: void editButtonClicked(); void resetButtonClicked(); public slots: void setWidget(QWidget * Widget); };
#include <QResizeEvent>#include "qpropertyeditwidget.h"QPropertyEditWidget::QPropertyEditWidget(QWidget *parent) : QWidget(parent){ fwidget = NULL; flayout = new QHBoxLayout(this); flayout->setContentsMargins(0,0,0,0); flayout->setSpacing(1); fcontainer = new QWidget(this); fcontainer->setAutoFillBackground(true); feditButton = new QToolButton(this); feditButton->setIcon(QIcon(":/icons/pencil")); feditButton->setMaximumSize(26,26); feditButton->setMinimumSize(10,10); feditButton->setVisible(false); fresetButton = new QToolButton(this); fresetButton->setText("*"); fresetButton->setMaximumSize(10,26); fresetButton->setMinimumSize(5,10); fresetButton->setVisible(false); flayout->addWidget(fcontainer,1); flayout->addWidget(feditButton); flayout->addWidget(fresetButton); setFocusProxy(feditButton); setLayout(flayout); connect(feditButton,SIGNAL(clicked()),this,SIGNAL(editButtonClicked())); connect(fresetButton,SIGNAL(clicked()),this,SIGNAL(resetButtonClicked())); recalculate();}QWidget *QPropertyEditWidget::widget() const{ return fwidget;}void QPropertyEditWidget::setEditable(bool Value){ feditButton->setVisible(Value); recalculate();}void QPropertyEditWidget::setResatable(bool Value){ fresetButton->setVisible(Value); recalculate();}void QPropertyEditWidget::setWidget(QWidget *Widget){ if(fwidget!=Widget) { if(fwidget) { fwidget->setParent(NULL); } fwidget = Widget; if(fwidget) { fwidget->setParent(fcontainer); recalculate(); } }}void QPropertyEditWidget::resizeEvent(QResizeEvent * event){ Q_UNUSED(event) recalculate();}void QPropertyEditWidget::recalculate(){ if(fwidget) { int w = width(); if(fresetButton->isVisibleTo(this)) w-=fresetButton->width(); if(feditButton->isVisibleTo(this)) w-=feditButton->width(); fwidget->resize(w,height()); }}
QComboBox * comboBox = new QComboBox();comboBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContentsOnFirstShow);