Qt database application 7- export and print QTableWidget/QTableView data

Posted by eddedwards on Wed, 26 Jan 2022 09:21:40 +0100

1, Foreword

The original intention of this component is to create a wheel to make data import and export extremely simple. It can be done in a few lines of code, which is applicable to most application scenarios. This is also the biggest difference between this component and qtxls. Qtxls aims to be large and comprehensive and provide various xls interfaces. Programmers need to deal with how to organize exported data, This can not avoid calling a lot of function code, which is often not so convenient for novice programmers. For example, many people actually want to export the data in the current QTableWidget or QTableView, and do not want to study how to organize the data. The hope is to pass in the QTableWidget control name, field name, field width and data content, Don't worry, because the controls are given to you and the data is there. Just implement the function to fix it. Just use it comfortably.

2, Functional features

  1. Component integrates exporting data to csv, xls, pdf and printing data at the same time.
  2. All operations provide static methods without new, and structural data is adopted for parameter settings such as data and attributes, which is very convenient.
  3. It also supports QTableView, QTableWidget, QStandardItemModel, QSqlTableModel and other data sources.
  4. Provide static methods to directly pass in QTableView and QTableWidget controls to automatically identify column name, column width and data content.
  5. Each group of functions provides a separate complete example with detailed comments, which is very suitable for Qter programmers at all stages.
  6. The original data export mechanism does not rely on any third-party libraries such as office components or operating systems, and supports embedded linux.
  7. The speed is super fast. It takes only 2 seconds to complete 100000 rows of data in 9 fields.
  8. It only takes four steps to quickly export massive data, such as 100W records to Excel.
  9. At the same time, it provides direct write data interface and multi-threaded write data interface without card main interface.
  10. You can set the title, subtitle and table name.
  11. You can set the field name, column name and column width of exported data.
  12. You can set the automatic stretching and filling of the end column, and the default stretching is more beautiful.
  13. You can set whether to enable verification filtering data. After enabling, the data that meets the rules will be displayed in special colors.
  14. Check columns, check rules, check values, and check value data types can be specified.
  15. The verification rule supports accurate equal to = =, greater than >, greater than or equal to > =, less than <, less than or equal to < =, not equal to! = contains.
  16. The data type of the check value supports integer int, floating point float and double, and the default text string type.
  17. You can set the random background color and the column set that needs the random background color.
  18. It supports grouping and outputting data, such as grouping and outputting data according to equipment, which is convenient for viewing.
  19. You can set csv separator, line content separator and sub content separator.
  20. You can set the border width and auto fill data type. The auto data type is on by default.
  21. You can set whether to turn on the data cell style. It is not turned on by default, which can save about 30% of the file volume.
  22. You can set horizontal layout, paper margin, etc., such as exporting to pdf and printing data.
  23. Support mixed arrangement of pictures and texts, export data to pdf and print data, and automatic paging.
  24. Super flexible, you can freely change the source code, set the alignment, text color, background color, etc.
  25. Support any Excel software, including but not limited to excel 2003-2021, wps, openoffice, etc.
  26. Written in pure Qt, it supports any Qt version + any compiler + any system.

3, Experience address

  1. Experience address: https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A Extraction code: o05q file name: bin_dataout.zip
  2. Domestic sites: https://gitee.com/feiyangqingyun
  3. International sites: https://github.com/feiyangqingyun
  4. Personal homepage: https://blog.csdn.net/feiyangqingyun
  5. Zhihu homepage: https://www.zhihu.com/people/feiyangqingyun/

4, Renderings

5, Related code

void frmSimple::on_btnLoad1_clicked()
{
    //Set the data model first, otherwise setColumnWidth will not work
    ui->tableView->setModel(model);

    //Set the number of columns, column title and column width
    model->setColumnCount(column);
    //An easy way to set the column header collection
    model->setHorizontalHeaderLabels(columnNames);
    for (int i = 0; i < column; ++i) {
        ui->tableView->setColumnWidth(i, columnWidths.at(i));
    }

    //Loop add row data
    QDateTime now = QDateTime::currentDateTime();
    model->setRowCount(row);
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < column; ++j) {
            QStandardItem *item = new QStandardItem;
            //The last column shows the time difference
            if (j == column - 1) {
                item->setText(now.addSecs(i).toString("yyyy-MM-dd HH:mm:ss"));
            } else {
                item->setText(QString("%1_%2").arg(i + 1).arg(j + 1));
            }
            model->setItem(i, j, item);
        }
    }
}

void frmSimple::on_btnCsv1_clicked()
{
    QString file = QUIHelper::appPath() + "/db/dataout_tableview.csv";
    DataHelper::DataOut(ui->tableView, model, 0, file, "Test title", "Test information");
    //Open the file you just exported
    QUIHelper::openFile(file, "Export test information");
}

void frmSimple::on_btnXls1_clicked()
{
    //Generic functions are passed directly into the control
#if 1
    QString file = QUIHelper::appPath() + "/db/dataout_tableview.xls";
    DataHelper::DataOut(ui->tableView, model, 1, file, "Test title", "Test information");
#else
    //If the file name is not passed in, a dialog box will pop up and select save file name
    QString file = DataHelper::DataOut(ui->tableView, model, 1);
#endif
    //Open the file you just exported
    QUIHelper::openFile(file, "Export test information");
}

void frmSimple::on_btnPdf1_clicked()
{
    //Generic functions are passed directly into the control
    QString file = QUIHelper::appPath() + "/db/dataout_tableview.pdf";
    DataHelper::DataOut(ui->tableView, model, 2, file, "Test title", "Test information");
    //Open the file you just exported
    QUIHelper::openFile(file, "Export test information");
}

void frmSimple::on_btnPrint1_clicked()
{
    //Generic functions are passed directly into the control
    DataHelper::DataOut(ui->tableView, model, 3, "", "Test title");
}

void frmSimple::on_btnLoad2_clicked()
{
    //Set column title, number and width
    ui->tableWidget->setColumnCount(column);
    //An easy way to set the column header set
    ui->tableWidget->setHorizontalHeaderLabels(columnNames);
    for (int i = 0; i < column; ++i) {
        ui->tableWidget->setColumnWidth(i, columnWidths.at(i));
    }

    //Add data
    QDateTime now = QDateTime::currentDateTime();
    ui->tableWidget->setRowCount(row);
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < column; ++j) {
            QTableWidgetItem *item = new QTableWidgetItem;
            //The last column shows the time difference
            if (j == column - 1) {
                item->setText(now.addSecs(i).toString("yyyy-MM-dd HH:mm:ss"));
            } else {
                item->setText(QString("%1_%2").arg(i + 1).arg(j + 1));
            }
            ui->tableWidget->setItem(i, j, item);
        }
    }
}

void frmSimple::on_btnCsv2_clicked()
{
    QString file = QUIHelper::appPath() + "/db/dataout_tablewidget.csv";
    DataHelper::DataOut(ui->tableWidget, 0, file, "Test title", "Test information");
    //Open the file you just exported
    QUIHelper::openFile(file, "Export test information");
}

void frmSimple::on_btnXls2_clicked()
{
    QString file = QUIHelper::appPath() + "/db/dataout_tablewidget.xls";
    DataHelper::DataOut(ui->tableWidget, 1, file, "Test title", "Test information");
    //Open the file you just exported
    QUIHelper::openFile(file, "Export test information");
}

void frmSimple::on_btnPdf2_clicked()
{
    //Generic functions are passed directly into the control
    QString file = QUIHelper::appPath() + "/db/dataout_tablewidget.pdf";
    DataHelper::DataOut(ui->tableWidget, 2, file, "Test title", "Test information");
    //Open the file you just exported
    QUIHelper::openFile(file, "Export test information");
}

void frmSimple::on_btnPrint2_clicked()
{
    //Generic functions are passed directly into the control
    DataHelper::DataOut(ui->tableWidget, 3, "", "Test title");
}

Topics: Database Qt