Qt Writing Control Property Designer 9-Database Acquisition

I. Preface

As a data source, database is widely used in many configuration software, such as specifying database type, filling in database connection information, specifying corresponding database tables and fields, acquisition interval, automatically acquiring database data according to acquisition interval, and displaying control assignment bound to the interface. Using database as data source has a great advantage that it does not need to write additional communication code, and it has nothing to do with the other party's language or platform. There will be no nagging. For example, inaccurate communication protocols lead to incorrect parsing. This will support arbitrary languages and platforms. There is a transition of database as an intermediate carrier, and any language platform will have a database, which is compatible. Therefore, using database as a data source is a good solution, which can concentrate on the continuous integration of software functions.

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_btnOpenDb_clicked()
{
    if (ui->btnOpenDb->text() == "open") {
        if (App::DbType == "sqlite") {
            //First check if the database file exists
            QString dbPath = qApp->applicationDirPath() + "/" + App::DbPath;
            QFile file(dbPath);
            if (file.size() == 0) {
                return;
            }

            dbConn = QSqlDatabase::addDatabase("QSQLITE");
            dbConn.setDatabaseName(dbPath);
        } else if (App::DbType == "mysql") {
            //First check whether the database server IP passes through, do not check the direct connection, there is no IP card for a long time.
            QTcpSocket socket;
            socket.connectToHost(App::DbPath, App::DbPort);
            if (!socket.waitForConnected(2000)) {
                return;
            } else {
                socket.disconnectFromHost();
            }

            dbConn = QSqlDatabase::addDatabase("QMYSQL");
            dbConn.setHostName(App::DbPath);
            dbConn.setPort(App::DbPort);
            dbConn.setDatabaseName(App::DbName);
            dbConn.setUserName(App::DbUser);
            dbConn.setPassword(App::DbPwd);
        } else {
            //Other databases are not supported yet, so you can join them on your own.
            return;
        }

        bool ok = dbConn.open();
        if (ok) {
            setEnable(ui->btnOpenDb, false);
            ui->btnOpenDb->setText("Close");
            timer->start();
        }
    } else {
        if (dbConn.isOpen()) {
            dbConn.close();
        }

        setEnable(ui->btnOpenDb, true);
        ui->btnOpenDb->setText("open");
        timer->stop();
    }
}

void frmData::readDataDb()
{
    QString sql = QString("select %1 from %2").arg(App::DbColumn).arg(App::DbTable);
    QSqlQuery query;
    if (query.exec(sql)) {
        if (query.next()) {
            double value = query.value(0).toDouble();
            ui->txtValue->setText(QString::number(value));
            append(4, QString("Current value: %1").arg(value));
        }
    }
}

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

Keywords: Database Qt socket xml

Added by numan82 on Thu, 12 Sep 2019 04:36:27 +0300