在QT设计师中有一个很好用的部件属性设置窗口,qt却没有提供此控件也没有对应的例子,后来发现若安装qt时选择了安装源码,可以在源码中找到,在源码中搜索qtpropertybrowser即可。
由于之前不知道qt源码中有qtpropertybrowser的源码,因此自己实现了一个,效果如下
思路:
控件使用QTreewidget,设置属性的控件使用委托代理完成,根据不同的类型创建不同的代理控件,checkBox这类之间勾选的控件使用setItemWidget()方法设置到对应的列中,
当属性被改变时通过信号将属性名称和属性值发送出去.
代理类源码:继承至QItemDelegate
QWidget *TreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
QWidget *widget = creatrWidget(parent,index);
return widget;
}
void TreeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int type = index.data(PropertyTree::ROLETYPE).toInt();
switch (type)
{
case PropertyTree::COMBOX:
{
QComboBox *box = qobject_cast<QComboBox*>(editor);
box->setCurrentText(index.data(Qt::EditRole).toString());
break;
}
case PropertyTree::TEXT:
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
edit->setText(index.data(Qt::EditRole).toString());
break;
}
case PropertyTree::SPINBOX:
{
QSpinBox *spin = qobject_cast<QSpinBox*>(editor);
spin->setValue(index.data(Qt::EditRole).toInt());
break;
}
case PropertyTree::DOUBLESPINBOX:
{
QDoubleSpinBox *doublespin = qobject_cast<QDoubleSpinBox*>(editor);
doublespin->setValue(index.data(Qt::EditRole).toDouble());
break;
}
case PropertyTree::FILE:
{
fileTypeSetEditorData(editor,index);
break;
}
case PropertyTree::COLOR:
{
ColorDelegate *colordelegate = qobject_cast<ColorDelegate*>(editor);
colordelegate->setColor(qvariant_cast<QColor>(index.data()));
break;
}
case PropertyTree::FONT:
{
QFontComboBox *fontbox = qobject_cast<QFontComboBox*>(editor);
fontbox->setCurrentText(index.data(Qt::EditRole).toString());
break;
}
}
}
void TreeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
int type = index.data(PropertyTree::ROLETYPE).toInt();
switch (type)
{
case PropertyTree::COMBOX:
{
QComboBox *box = qobject_cast<QComboBox*>(editor);
model->setData(index,box->currentText());
break;
}
case PropertyTree::SPINBOX:
{
QSpinBox *spin = qobject_cast<QSpinBox*>(editor);
model->setData(index,spin->text());
break;
}
case PropertyTree::DOUBLESPINBOX:
{
QDoubleSpinBox *doublespin = qobject_cast<QDoubleSpinBox*>(editor);
model->setData(index,doublespin->text());
break;
}
case PropertyTree::TEXT:
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
model->setData(index,edit->text());
break;
}
case PropertyTree::FILE:
{
fileTypeSetModelData(editor,model,index);
break;
}
case PropertyTree::COLOR:
{
ColorDelegate *colordelegate = qobject_cast<ColorDelegate*>(editor);
model->setData(index,QVariant::fromValue(colordelegate->getColor()));
break;
}
case PropertyTree::FONT:
{
QFontComboBox *fontbox = qobject_cast<QFontComboBox*>(editor);
QString str = fontbox->currentText();
QTreeWidgetItem *item = static_cast<QTreeWidgetItem *>(index.internalPointer());
if(item)
{
item->setFont(DELEGATE_COLUMN,fontbox->currentFont());
item->setText(DELEGATE_COLUMN,str);
}
break;
}
}
}
void TreeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
/*====================================================================================*/
//根据单元格参数类型创建代理的widget
QWidget *TreeDelegate::creatrWidget(QWidget *parent, const QModelIndex &index) const
{
QWidget *widget = nullptr;
int type = index.data(PropertyTree::ROLETYPE).toInt();
switch (type)
{
case PropertyTree::COMBOX:
{
QComboBox *box = new QComboBox(parent);
QStringList list = index.data(PropertyTree::PARAMS).toStringList();
box->addItems(list);
widget = box;
break;
}
case PropertyTree::TEXT:
{
QLineEdit *edit = new QLineEdit(parent);
widget = edit;
break;
}
case PropertyTree::SPINBOX:
{
QSpinBox *spin = new QSpinBox(parent);
widget = spin;
break;
}
case PropertyTree::DOUBLESPINBOX:
{
QDoubleSpinBox *doublespin = new QDoubleSpinBox(parent);
widget = doublespin;
break;
}
case PropertyTree::FILE:
{
FileDelegate *filedelegate = new FileDelegate(parent);
//filedelegate->setFocusPolicy(Qt::WheelFocus);
connect(filedelegate,&FileDelegate::signal_finished,this,&TreeDelegate::slot_updateIndex);
widget=filedelegate;
break;
}
case PropertyTree::COLOR:
{
ColorDelegate *colordelegate = new ColorDelegate(parent);
connect(colordelegate,&ColorDelegate::signal_colorChanged,this,&TreeDelegate::slot_updateIndex);
widget = colordelegate;
break;
}
case PropertyTree::FONT:
{
QFontComboBox *fontbox = new QFontComboBox(parent);
widget = fontbox;
break;
}
}
return widget;
}
懒得粘贴了,源码放这吧,需要的自行下载
源码下载