Qt之属性树

在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;
}

懒得粘贴了,源码放这吧,需要的自行下载
源码下载

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,可以使用QDomDocument类来创建和操作XML文档。下面是一个示例,演示如何使用QDomDocument创建一个XML文档并将其保存为树形结构: ```cpp #include <QtXml> int main() { QDomDocument doc("mydocument"); // 创建根元素 QDomElement root = doc.createElement("root"); doc.appendChild(root); // 创建子元素1 QDomElement child1 = doc.createElement("child1"); root.appendChild(child1); // 创建子元素2 QDomElement child2 = doc.createElement("child2"); root.appendChild(child2); // 创建子元素3 QDomElement child3 = doc.createElement("child3"); root.appendChild(child3); // 创建子元素4 QDomElement child4 = doc.createElement("child4"); child3.appendChild(child4); // 添加属性 child1.setAttribute("name", "child1"); child2.setAttribute("name", "child2"); child3.setAttribute("name", "child3"); child4.setAttribute("name", "child4"); // 将XML文档保存为树形结构 QString xml = doc.toString(); qDebug() << xml; return 0; } ``` 在上面的示例中,我们首先创建了一个QDomDocument对象,然后创建了一个根元素。接下来,我们创建了四个子元素,并将它们添加到根元素中。其中,子元素3还包含了一个子元素4。最后,我们为每个元素设置了一个名称和一个属性,并将XML文档保存为树形结构。 运行上面的程序,将会输出以下内容: ``` <?xml version="1.0" encoding="UTF-8"?> <root> <child1 name="child1"/> <child2 name="child2"/> <child3 name="child3"> <child4 name="child4"/> </child3> </root> ``` 这就是一个简单的Qt XML树形结构的示例。如果你想了解更多关于Qt的XML操作,请查看Qt文档中的QDomDocument类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值