Qt 利用XML文档,写一个程序集合 二

接上一篇文章https://www.cnblogs.com/DreamDog/p/9213915.html

XML文档的读写

一个根节点,下面每一个子节点代表一个子程序,内容为子程序名字,图标路径,exe路径

/*
 * 读取XML配置文档
 */
static int count_flag = 0;
void MainWindow::read_XML()
{
    QFile file("./subroutine/config.xml");                             //引用文件路径
    if(!file.open((QFile::ReadOnly | QFile::Text)))         //只读文本方式打开
    {
        QMessageBox::information(NULL,"Title","OpenFile false");        //如果打开失败则提示
    }
    /*
     * 装在XML文件内容到内存
     */
    QDomDocument doc;
    doc.setContent(&file);
    file.close();
    QDomElement root = doc.documentElement();
    QDomNode node = root.firstChild();
    QString name = "asd";
    QString iconpath;
    QString exepath;
    while (!node.isNull())
    {
        QDomElement element = node.toElement();
        if(!element.isNull())
        {
            QDomNamedNodeMap list = element.attributes();
            for(int i = 0;i<list.count();i++)
            {
                if(list.item(i).nodeName() == "name")
                {
                    name = list.item(i).nodeValue();
                }
                else if (list.item(i).nodeName() == "iconpath")
                {
                    iconpath = list.item(i).nodeValue();
                }
                else if (list.item(i).nodeName() == "exepath")
                {
                    exepath = list.item(i).nodeValue();
                }
            }
        }
        //        QPushButton *button = new QPushButton(name, this);
        //        button->setIcon(QIcon(iconpath));
        //        button->show();
        //        connect(button,&QPushButton::clicked,[=]()
        //        {
        //            QProcess *myProcess = new QProcess(this);
        //            myProcess->startDetached(exepath);
        //        });

        MPushButton *button = new MPushButton(this);
        button->set_name(name);
        button->set_ICON(iconpath);
        button->show();

        connect(button,&MPushButton::clicked,[=]()
        {
            QProcess *myProcess = new QProcess(this);
            myProcess->startDetached(exepath);
        });
        flowLayout->addWidget(button);
        node = node.nextSibling();
        count_flag++;
        if(count_flag-35>0)
        {
            showWidgt->setGeometry(0,0,ui->widget_main->width(),ui->widget_main->height()+100*((count_flag-35)/7+1));
        }

    }
}

 以上代码为读取XML部分,

 可以参考https://blog.csdn.net/z609932088/article/details/71694709

猜你喜欢

转载自www.cnblogs.com/DreamDog/p/9214052.html