Write control properties Qt Designer 11- import xml

I. Introduction

The article is responsible for export controls designed to xml data file, this article is responsible for the partial exported xml data file import, and then automatically generates the corresponding control on the canvas, Qt built-in xml data analysis functions, very powerful, in QtXml package assembly, Qt has the advantage that encapsulates the functionality of many of the major operating system platforms, especially GUI controls, is indeed very large one-stop supermarket GUI, network components, although not very powerful, but deal with some basic application is more than enough. Xml when exporting data, according to the list of properties and property values ​​are stored instead of xml child nodes, the need to traverse while parsing node attribute name and attribute value, QDomNamedNodeMap attrs = element.attributes (); then one by one cycle can be removed name and value, QDomNode n = attrs.item (i); QString nodeName = n.nodeName (); QString nodeValue = n.nodeValue ();

Experience Address: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p File: An executable file .zip

Second, the realization of functions

  1. All controls automatically load the plug-in generates a list file, the default built-in controls over 120.
  2. To drag the canvas automatically generate the corresponding control, WYSIWYG.
  3. Chinese property right of the bar, change the corresponding property is immediately applied to the corresponding selected control, simple and intuitive, very suitable for white use.
  4. Original text translation attribute field mapping mechanism, high efficiency, you can easily expand the property bar other languages.
  5. Automatically extracting properties of all the controls and properties shown on the right column, including drop-down box enumerated value and the like.
  6. Support manually select the plug-in file import plug-in external files.
  7. All controls current canvas configuration information can be exported to a xml file.
  8. You can manually select the xml file open layout control, automatic load control based on the xml file.
  9. Can pull the slider, check box analog data, text input box, the data application in three ways to generate all of the controls.
  10. Control supports eight directions pulling resize, adaptive to any resolution, right and left can fine-tune the keyboard up and down position.
  11. They opened the serial collection, gathering network, database data set collected in three ways.
  12. Code is very concise, very detailed notes, can be configured as a prototype, develop their own more features.
  13. Written in pure Qt, Qt support any version of the compiler + any + any system.

Third, renderings

Fourth, the core code

void frmMain::openFile(const QString &fileName)
{
    //打开文件
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        return;
    }

    //将文件填充到dom容器
    QDomDocument doc;
    if (!doc.setContent(&file)) {
        file.close();
        return;
    }

    file.close();

    //先清空原有控件
    QList<QWidget *> widgets = ui->centralwidget->findChildren<QWidget *>();
    qDeleteAll(widgets);
    widgets.clear();

    //先判断根元素是否正确
    QDomElement docElem = doc.documentElement();
    if (docElem.tagName() == "canvas") {
        QDomNode node = docElem.firstChild();
        QDomElement element = node.toElement();
        while(!node.isNull()) {
            QString name = element.tagName();

            //存储坐标+宽高
            int x, y, width, height;
            //存储其他自定义控件属性
            QList<QPair<QString, QVariant> > propertys;

            //节点名称不为空才继续
            if (!name.isEmpty()) {
                //遍历节点的属性名称和属性值
                QDomNamedNodeMap attrs = element.attributes();
                for (int i = 0; i < attrs.count(); i++) {
                    QDomNode n = attrs.item(i);
                    QString nodeName = n.nodeName();
                    QString nodeValue = n.nodeValue();
                    //qDebug() << nodeName << nodeValue;

                    //优先取出坐标+宽高属性,这几个属性不能通过setProperty实现
                    if (nodeName == "x") {
                        x = nodeValue.toInt();
                    } else if (nodeName == "y") {
                        y = nodeValue.toInt();
                    } else if (nodeName == "width") {
                        width = nodeValue.toInt();
                    } else if (nodeName == "height") {
                        height = nodeValue.toInt();
                    } else {
                        propertys.append(qMakePair(nodeName, QVariant(nodeValue)));
                    }
                }
            }

            //qDebug() << name << x << y << width << height;

            //根据不同的控件类型实例化控件
            int count = listWidgets.count();
            for (int i = 0; i < count; i++) {
                QString className = listWidgets.at(i)->name();
                if (name == className) {
                    QWidget *widget = listWidgets.at(i)->createWidget(ui->centralwidget);

                    //逐个设置自定义控件的属性
                    int count = propertys.count();
                    for (int i = 0; i < count; i++) {
                        QPair<QString, QVariant> property = propertys.at(i);
                        widget->setProperty(property.first.toLatin1().constData(), property.second);
                    }

                    //设置坐标+宽高
                    widget->setGeometry(x, y, width, height);
                    //实例化选中窗体跟随控件一起
                    newSelect(widget);
                    break;
                }
            }

            //移动到下一个节点
            node = node.nextSibling();
            element = node.toElement();
        }
    }
}

Fifth, introduce controls

  1. More than 150 exquisite control, covers a variety of dashboards, progress bar, the progress of the ball, compass, graphs, scales, thermometers, navigation bar, navigation bar, flatui, highlight the button, slide the selector, the lunar calendar and so on. Qwt far more than the number of controls integration.
  2. Each class can be independently as a separate control, zero coupling each control file and a header file to achieve a code amount, independent of other files to facilitate individual control integrated into the project source code form, less. qwt interlocking control class, highly coupled, want to use one of the controls, must contain all the code.
  3. Write all pure Qt, QWidget + QPainter to draw, to support any Qt version Qt4.6 Qt5.12, support for mingw, msvc, gcc compiler, etc., support any operating system such as windows + linux + mac + embedded linux, which does not garbled can be directly integrated into Qt Creator, a built-in controls and use the same, most of the effects can be as long as several properties are set, very convenient.
  4. DEMO separate source containing the control corresponding to each control has a convenient reference. It also provides integrated use of all controls a DEMO.
  5. Source code for each control has detailed Chinese annotation, are prepared in accordance with unified design specifications, easy to learn to write custom controls.
  6. Each control default color and demo corresponding color is very beautiful.
  7. More than 130 visible control, six invisible control.
  8. Portion control provides a variety of styles style selection, multiple choice style indicator.
  9. All controls changes adaptive stretched form.
  10. Integrated design custom attribute that supports drag design, WYSIWYG support the import and export in xml format.
  11. Activex control that comes with demo, all controls can be run directly in the browser ie.
  12. Fontawesome integrated graphics font + Alibaba iconfont collection of hundreds of graphic fonts, font fun graphic brings.
  13. All controls and finally generate a dynamic library files (dll or so, etc.) can be integrated directly into qtcreator designed for use in drag.
  14. Already qml version, the latter will consider a pyqt version, if the user is in great demand then.
  15. Custom plug-in open dynamic library (permanent free), and the back door without any restrictions, ease of use.
  16. 26 now available version dll, which includes qt5.12.3 msvc2017 32 + 64 mingw 32 + 64 in.
  17. From time to time to increase control and improve controls, regularly updated SDK, to welcome all suggestions, thank you!
  18. Qt introductory books recommended Huo Yafei of "Qt Creator Quick Start" "Qt5 programming entry", Qt official Advanced book recommendations "C ++ GUI Qt4 programming."
  19. Highly recommended programmer self-discipline and planning book series "lying programmer" "Programmer's growth course", "grief programmer", benefited from a lifetime!
  20. SDK download link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p

Guess you like

Origin www.cnblogs.com/feiyangqingyun/p/11516197.html