Qt Writing Control Property Designer 8-Network Acquisition

I. Preface

The last article has got through the serial port acquisition of one of the data sources. This time we want to talk about network acquisition. At present, there are three kinds of network communication, TCP/UDP/HTTP. Among them, TCP communication includes two kinds of client server. Tcp communication only uses multiple handshake mechanism without losing packets, but it consumes a lot of resources and needs to be established. Connect. udp communication may lose packets in the case of large amount of data or unstable network, and the order can not be guaranteed, but the data of a packet must be correct. Because it occupies very little resources and does not need to establish connections, it is also widely used in many scenarios. Since I used udp personally, I have not found the situation of packet loss. The amount of energy data is not large enough or the reason in the LAN, anyway, it is still cool to use. http communication is very popular nowadays, especially for data interaction with servers. Basically, post requests return a series of json data and parse the corresponding json data. This time, TCP communication is used as an example. The other two can be expanded by themselves and are very simple.

Experience address: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ Extraction code: 877p file: executable file. zip

II. Functions of Realization

  1. All controls in the plug-in file are automatically loaded to generate lists. By default, there are more than 120 controls in the plug-in file.
  2. Drag onto the canvas to automatically generate the corresponding control, WYSIWYG.
  3. On the right side of the Chinese property bar, changing the corresponding properties is immediately applied to the selected control, intuitive and concise, very suitable for the use of Xiaobai.
  4. The original attribute bar text translation mapping mechanism is very efficient and can be very convenient to expand the attribute bar of other languages.
  5. The properties of all controls are automatically extracted and displayed in the right property bar, including the enumeration value drop-down box, etc.
  6. Support manual selection of plug-in files and external import of plug-in files.
  7. All control configuration information for the current canvas can be exported to an xml file.
  8. You can manually select the xml file to open the control layout and automatically load the control according to the xml file.
  9. Pull slider, check analog data check box, text box input, three ways to generate data and apply all controls.
  10. Control supports eight azimuth pull to adjust size, adaptive arbitrary resolution, and keyboard position can be fine-tuned from top to bottom.
  11. Through the serial port acquisition, network acquisition, database acquisition three ways to set up data.
  12. The code is extremely concise, the comments are very detailed, can be used as the embryonic configuration, expand more functions.
  13. Pure Qt programming, support any Qt version + any compiler + any system.

III. EFFECT CHARACTERISTICS

Core Code

void frmData::initServer()
{
    //Instantiate serial port class, bind signal slot
    com = new QextSerialPort(QextSerialPort::EventDriven, this);
    connect(com, SIGNAL(readyRead()), this, SLOT(readDataCom()));

    //Instantiate network communication client class, bind signal slot
    tcpClient = new QTcpSocket(this);
    connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readDataClient()));

    //Instantiate network communication server class, bind signal slot
    tcpSocket = NULL;
    tcpServer = new QTcpServer(this);
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));

    //Open timer to read data from database
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(readDataDb()));
    timer->setInterval(1000);
}

void frmData::on_btnOpenTcpClient_clicked()
{
    if (ui->btnOpenTcpClient->text() == "Connect") {
        tcpClient->connectToHost(App::TcpServerIP, App::TcpServerPort);
        bool ok = tcpClient->waitForConnected(1000);
        if (ok) {
            setEnable(ui->btnOpenTcpClient, false);
            ui->btnOpenTcpClient->setText("To break off");
        }
    } else {
        tcpClient->disconnectFromHost();
        setEnable(ui->btnOpenTcpClient, true);
        ui->btnOpenTcpClient->setText("Connect");
    }
}

void frmData::on_btnOpenTcpServer_clicked()
{
    if (ui->btnOpenTcpServer->text() == "Monitor") {
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
        bool ok = tcpServer->listen(QHostAddress::AnyIPv4, App::TcpListenPort);
#else
        bool ok = tcpServer->listen(QHostAddress::Any, App::TcpListenPort);
#endif
        if (ok) {
            setEnable(ui->btnOpenTcpServer, false);
            ui->btnOpenTcpServer->setText("Stop it");
        }
    } else {
        if (tcpSocket != NULL) {
            tcpSocket->disconnectFromHost();
        }

        tcpSocket = NULL;
        tcpServer->close();
        setEnable(ui->btnOpenTcpServer, true);
        ui->btnOpenTcpServer->setText("Monitor");
    }
}

void frmData::readDataClient()
{
    QByteArray data = tcpClient->readAll();
    if (data.length() <= 0) {
        return;
    }

    //By default, the first byte is parsed and can be changed by itself.
    quint8 value = data.at(0);
    ui->txtValue->setText(QString::number(value));
    append(3, data.toHex());
}

void frmData::readDataServer()
{
    QByteArray data = tcpSocket->readAll();
    if (data.length() <= 0) {
        return;
    }

    //By default, the first byte is parsed and can be changed by itself.
    quint8 value = data.at(0);
    ui->txtValue->setText(QString::number(value));
    append(3, data.toHex());
}

void frmData::newConnection()
{
    while(tcpServer->hasPendingConnections()) {
        if (tcpSocket != NULL) {
            tcpSocket->disconnectFromHost();
        }

        tcpSocket = tcpServer->nextPendingConnection();
        connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readDataServer()));
    }
}

Introduction of Control

  1. More than 150 exquisite controls, covering a variety of dashboards, progress bars, progress balls, compasses, curves, rulers, thermometers, navigation bars, navigation bars, flatui, highlighted buttons, sliding selectors, lunar calendar, etc. Far more controls than qwt integrates.
  2. Each class can be separated into a separate control, zero-coupling, each control has a header file and an implementation file, independent of other files, to facilitate the integration of a single control in the form of source code into the project, less code. The control classes of qwt are interlinked and highly coupled. If you want to use one of the controls, you must include all the code.
  3. All pure Qt writing, QWidget+QPainter drawing, support any Qt version from Qt4.6 to Qt5.12, support mingw, msvc, gcc and other compilers, support any operating system such as windows+linux+mac + embedded linux, no scrambling, can be directly integrated into Qt Creator, as with its own controls, most of the effects as long as set up. Several attributes can be used, which is very convenient.
  4. Each control has a corresponding separate DEMO containing the source code of the control, which is convenient for reference. It also provides an integrated DEMO for all controls.
  5. The source code of each control has detailed Chinese annotations, which are compiled in accordance with the unified design specifications. It is convenient to learn how to compile custom controls.
  6. Each control's default color matching and demo's corresponding color matching are very beautiful.
  7. More than 130 visible controls and 6 invisible controls.
  8. Some controls provide a variety of style choices, a variety of indicator style choices.
  9. All controls adapt to form stretching changes.
  10. Integrate custom control property designer, support drag design, WYSIWYG, support import and export xml format.
  11. With activex control demo, all controls can run directly in ie browser.
  12. Integrate fontawesome graphic fonts + hundreds of graphic fonts collected by Alibaba iconfont to enjoy the fun of graphic fonts.
  13. All controls eventually generate a dynamic library file (dll or so, etc.) that can be directly integrated into the qtcreator to drag the design to use.
  14. At present, there is a version of qml, pyqt version will be considered later, if the user demand is large.
  15. Custom Control Plug-in Open Dynamic Library (permanently free), without any backdoor and restrictions, please feel free to use.
  16. At present, 26 versions of dll have been provided, including qt5.12.3 MSVC 2017 32 + 64 MinGW 32 + 64.
  17. Increase control and improve control from time to time, update SDK from time to time. Welcome to make suggestions. Thank you!
  18. Qt introductory books recommend Hoyafei's Quick Start to Qt Creator, Qt5 Programming Introduction, and Qt advanced books recommend the official C++ GUI Qt4 Programming.
  19. I strongly recommend programmers'self-cultivation and planning series "Talking about Programmers", "Programmers' Growth Course" and "Solution Programmers". They benefit a lot and benefit a lifetime!
  20. SDK Download Link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ Extraction code: 877p

Tags: Mobile Qt network xml Programming

Posted on Wed, 11 Sep 2019 18:51:33 -0700 by sps