Qt中菜单使用xml内容进行创建

菜单是一种具有层次结构的控件,其子项同样适合用xml文件存储。
以下是我写的一个类XmlMenuBar,从QMenuBar和ReadWriteXml两个类中派生。
**注意:**由于ReadWriteXml中引用了读写xml的第三方类,文件较多,单独进行了上传,见读写xml的c++类

#ifndef XMLMENUBAR_H
#define XMLMENUBAR_H

#include <QMenuBar>
#include"./tinyxml/readwritexml.h"

class XmlMenuBar : public QMenuBar,public ReadWriteXml
{
    Q_OBJECT
public:
    explicit XmlMenuBar(QWidget *parent = nullptr);

    QAction* actionByName(const QString &name);
protected:
    void readXmlRootElement(const TiXmlElement* rootElement);
    void writeXmlRootElement(TiXmlElement* rootElement);
private:

signals:

};

#endif // XMLMENUBAR_H
#include "xmlmenubar.h"
#include<QDebug>

XmlMenuBar::XmlMenuBar(QWidget *parent)
    : QMenuBar(parent)
{

}

void XmlMenuBar::readXmlRootElement(const TiXmlElement *rootElement)
{
    //找第一个
    const TiXmlElement* element=rootElement->FirstChildElement("Control");
    while (element)
    {
        const char* menuName=element->Attribute("Name");
        if(menuName) //创建菜单
        {
            QMenu* menu=addMenu(menuName);
            //下拉菜单项
            const TiXmlElement* elementItem=element->FirstChildElement("Control");
            while (elementItem)
            {
                const char* actionName=elementItem->Attribute("Name");
                if(actionName)
                {
                    const char* objName=elementItem->Attribute("Object");
                    QAction *action=menu->addAction(actionName);
                    action->setObjectName(objName);
                }
                //继续读下一个
                elementItem=elementItem->NextSiblingElement("Control");
            }
        }
        //继续读下一个
        element=element->NextSiblingElement("Control");
    }
}

void XmlMenuBar::writeXmlRootElement(TiXmlElement *rootElement)
{
    qDebug()<<rootElement->Attribute("name");
}

QAction *XmlMenuBar::actionByName(const QString &name)
{
    return findChild<QAction*>(name);
}
要将QFileSystemModel保存为XML,可以按照以下步骤操作: 1. 在Qt Creator创建一个新的Qt Widgets应用程序项目。 2. 在项目添加一个QTreeView控件和一个QFileSystemModel模型。 3. 在QTreeView控件设置模型为QFileSystemModel模型。 4. 点击“文件”菜单并选择“新建文件”选项。在弹出的对话框,选择“Qt”类别并选择“XML文件”类型。命名文件并点击“下一步”。 5. 在“XML文件设置”对话框,选择“生成DOM模型”选项并点击“下一步”。 6. 在“XML文件内容”对话框,将以下代码添加到XML文件: ``` <?xml version="1.0" encoding="UTF-8"?> <FileSystemModel> </FileSystemModel> ``` 7. 在Qt Creator打开mainwindow.cpp文件并添加以下代码: ``` #include <QXmlStreamWriter> #include <QFile> void MainWindow::on_saveButton_clicked() { QFile file("fileSystemModel.xml"); file.open(QIODevice::WriteOnly); QXmlStreamWriter xmlWriter(&file); xmlWriter.setAutoFormatting(true); xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("FileSystemModel"); for (int i = 0; i < model->rowCount(); ++i) { QModelIndex index = model->index(i, 0); QString fileName = model->data(index, Qt::DisplayRole).toString(); xmlWriter.writeStartElement("File"); xmlWriter.writeTextElement("Name", fileName); xmlWriter.writeEndElement(); } xmlWriter.writeEndElement(); xmlWriter.writeEndDocument(); file.close(); } ``` 8. 在mainwindow.h文件添加以下代码: ``` private slots: void on_saveButton_clicked(); private: QFileSystemModel *model; ``` 9. 在mainwindow.cpp文件的构造函数添加以下代码: ``` model = new QFileSystemModel(this); model->setRootPath("/"); ui->treeView->setModel(model); ``` 10. 在Qt Creator编译并运行程序。单击保存按钮将QFileSystemModel保存为XML文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔡云辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值