QT 票据打印系统

本文详细介绍了使用QT框架开发票据打印系统的过程,包括UI设计、打印机设置、自定义布局和数据填充等内容,旨在帮助开发者实现高效、准确的票据打印功能。
摘要由CSDN通过智能技术生成
#include "charge.h"
#include "ui_charge.h"
#include<QMenuBar>
#include <QMainWindow>
#include<QWidget>
#include <QPrintDialog>
#include <QPrinter>
#include <QPainter>
#include <QStackedWidget>
#include <QSqlQuery>
#include<QMessageBox>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
#include <QVariantList>
#include <QSqlTableModel>
#include<QDebug>
#include <QFile>
#include <QPdfWriter>
#include <QFileDialog>
#include <QSqlRecord>
#include<QProcess>
#include<iostream>
#include <fstream>
#include <windows.h>
#include<QInputDialog>
#include <QFileDevice>

Charge::Charge(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Charge)
{
    ui->setupUi(this);
    QFile file("C:/Users/14771/Documents/A-0/main.qss");
       file.open(QFile::ReadOnly);
       QTextStream filetext(&file);
       QString stylesheet = filetext.readAll();
       this->setStyleSheet(stylesheet);
      file.close();
    connect(ui->actionNew,&QAction::triggered,[=] (){
    });
    //连接数据库
    primkey = 1;
    createConnection();

        //下拉选项框可编辑
        ui->comboBox->setEditable(true);
        ui->comboBox_pay->setEditable(true);
        ui->comboBox_classSum->setEditable(true);
        ui->comboBox_students->setEditable(true);
        ui->comboBox_receiver1->setEditable(true);;
        ui->comboBox_receiver2->setEditable(true);
        ui->comboBox_className_1->setEditable(true);
        ui->comboBox_className_2->setEditable(true);;
        ui->comboBox_className_3->setEditable(true);
        ui->comboBox_className_4->setEditable(true);
        ui->comboBox_className_7->setEditable(true);
        ui->comboBox_className_8->setEditable(true);
        ui->comboBox_className_9->setEditable(true);
        ui->comboBox_className_12->setEditable(true);
        ui->comboBox_className_14->setEditable(true);
        ui->comboBox_className_15->setEditable(true);


        //在tableview中创建一个model
        model = new QSqlTableModel(ui->tableView_search);
        ui->tableView_search->setModel(model);//绑定
        model->setTable("user_information");
        //添加列
        model->setHeaderData(0,Qt::Horizontal,"报名时间");

        model->setHeaderData(1,Qt::Horizontal,"票据编号");

        model->setHeaderData(2,Qt::Horizontal,"姓名");

        model->setHeaderData(3,Qt::Horizontal,"性别");

        model->setHeaderData(4,Qt::Horizontal,"电话");

        model->setHeaderData(5,Qt::Horizontal,"学生类型");

        model->setHeaderData(6,Qt::Horizontal,"课程名称");

        model->setHeaderData(7,Qt::Horizontal,"金额");

        model->setHeaderData(8,Qt::Horizontal,"收款单位");

        model->setHeaderData(9,Qt::Horizontal,"付款方式");

        model->setHeaderData(10,Qt::Horizontal,"收款人");

        model->setHeaderData(11,Qt::Horizontal,"课程总数");

        model->setHeaderData(12,Qt::Horizontal,"次序");

        model->select();

        //设置表格的单元为只读属性,即不能编辑
//        ui->tableView_search->setEditTriggers(QAbstractItemView::NoEditTriggers);
//        ui->tableView_search->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中模式为选中行
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
//备份数据库

}

void Charge::createConnection(){
    db = QSqlDatabase::addDatabase("QSQLITE");
   //设置数据库
   db.setDatabaseName("C:/sqlite/education.db");
   //打开数据库
   if( !db.open() ) //数据库打开失败
   {
       QMessageBox::warning(this, "erro", db.lastError().text());
       return;
   }
}
bool Charge::backupDB(){//数据库备份
    QString sourceFileName = QDir::currentPath()+"/temp/ICEMS.db";

        //创建备份文件夹
       QDir dir(QDir::currentPath()+"/backup");
       if(!dir.exists()){
           dir.mkdir(QDir::currentPath()+"/backup");
       }
        //检查源数据库文件是否存在
       bool isExist = QFile::exists(QDir::currentPath()+"/temp/ICEMS.db");
       if(!isExist){
            QMessageBox::about(this,QString::fromLocal8Bit("备份数据库"),
                               QString::fromLocal8Bit("备份失败,源数据库文件不存在!"));
            return false;
       }
       else
       {
                //获取当前时间
               QDateTime current_date_time = QDateTime::currentDateTime();
               QString date = current_date_time.toString("yyyy-MM-dd hh-mm-ss");
                //重构当前备份目录及文件名,加入时间字段
               QString destFileName = QDir::currentPath()+"/backup/"+ QString("ICMES_backup%1.db").arg(date);

                //info = QFileInfo(destFileName);
                // destFileName = info.fileName();
               //调用备份命令copy
               if(QFile::copy(sourceFileName,destFileName))
               {
                   return true ;
               }
               else
               {
                   return false ;
               }
        }
}
void Charge::receiveLogin(){
    this->show();
}

Charge::~Charge()
{
    delete ui;
}

void Charge::on_settings_button_clicked()
{

    emit showSettings();
}

void Charge::on_pushButton_clicked()//清空按钮cliked;
{
    ui->dateTimeEdit1->clear();
    ui->lineEdit_No->clear();
    ui->lineEdit_name->clear();
    ui->comboBox->clearEditText();
    ui->lineEdit_phoneNumber->clear();
    ui->comboBox_students->clearEditText();
    ui->comboBox_classSum->clearEditText();
    ui->comboBox_className_8->clear();
    ui->lineEdit_pay1_2->clear();
    ui->lineEdit_receive1_8->clear();
    ui->lineEdit_sum->clear();
    ui->lineEdit_sum_Big->clear();
    ui->comboBox_className_9->clearEditText();
    ui->lineEdit_receive1_3->clear();
    ui->lineEdit_pay1_3->clear();
    ui->comboBox_className_7->clearEditText();
    ui->lineEdit_receive2_2->clear();
    ui->lineEdit_pay2_2->clear();
    ui->lineEdit_sum_3->clear();
    ui->lineEdit_sum_Big_3->clear();
    ui->comboBox_className_15->clearEditText();
    ui->lineEdit_receive1_5->clear();
    ui->lineEdit_pay1_5->clear();
    ui->comboBox_className_14->clearEditText();
    ui->lineEdit_receive2_4->clear();
    ui->lineEdit_pay2_4->clear();
    ui->comboBox_className_12->clearEditText();
    ui->lineEdit_receive3_3->clear();
    ui->lineEdit_pay3_3->clear();
    ui->lineEdit_sum_4->clear();
    ui->lineEdit_sum_Big_4->clear();
    ui->comboBox_className_1->clearEditText();
    ui->lineEdit_receive4->clear();
    ui->lineEdit_pay1->clear();
    ui->lineEdit_pay4->clear();
    ui->comboBox_className_4->clearEditText();
    ui->lineEdit_pay2->clear();
    ui->lineEdit_receive1->clear();
    ui->lineEdit_receive2->clear();
    ui->comboBox_className_2->clearEditText();
    ui->lineEdit_pay3->clear();
    ui->comboBox_className_3->clear();
    ui->lineEdit_receive3->clear();
    ui->lineEdit_sum_5->clear();
    ui->lineEdit_sum_Big_5->clear();
    ui->comboBox_receiver1->clearEditText();
    ui->comboBox_pay->clearEditText();
    ui->comboBox_receiver2->clearEditText();
    ui->lineEdit_signal->clear();
    ui->lineEdit_name_2->clear();
    ui->lineEdit_phone->clear();
    ui->dateTimeEdit->clear();
    ui->lineEdit_sum_Big->clear();
    ui->lineEdit_sum->clear();
    ui->comboBox_receiver1->clearEditText();
    ui->comboBox_pay->clearEditText();
    ui->comboBox_receiver2->clearEditText();
    ui->lineEdit_signal->clear();

}
void Charge::receiveCharge3(){
    Charge::on_pushButton_save_clicked();
}

void Charge::on_pushButton_save_clicked()
{


    QString s1 =ui->dateTimeEdit1->text();//报名时间s1
    QString s2 =ui->lineEdit_No->text();//票据编号2
    QString s3 =ui->lineEdit_name->text();//姓名3
    QString s4 =ui->comboBox->currentText();//性别4
    QString s5 =ui->lineEdit_phoneNumber->text();//电话5
    QString s6 =ui->comboBox_students->currentText();//学生类型6
    int     s7 =ui->comboBox_classSum->currentIndex();//课程数目7
    QString s54 =ui->comboBox_receiver1->currentText();//收款单位54
    QString s55 =ui->comboBox_pay->currentText();//收款方式55
    QString s56 =ui->comboBox_receiver2->currentText();//收款人56

    //page1
    QString s8 =ui->comboBox_className_8->currentText();
    QString s12 =ui->lineEdit_sum->text();

   //page2
    QString s13 =ui->comboBox_className_9->currentText();
    QString s16 =ui->lineEdit_receive1_3->text();
    QString s17 =ui->lineEdit_pay1_3->text();
    QString s18 =ui->comboBox_className_7->currentText();
    QString s20 =ui->lineEdit_receive2_2->text();
    QString s21 =ui->lineEdit_pay2_2->text();
    QString s22 =ui->lineEdit_sum_3->text();

   //page3
    QString s23 =ui->comboBox_className_15->currentText();
    QString s25 =ui->lineEdit_receive1_5->text();
    QString s26 =ui->lineEdit_pay1_5->text();
    QString s27 =ui->comboBox_className_14->currentText();
    QString s29 =ui->lineEdit_receive2_4->text();
    QString s30 =ui->lineEdit_pay2_4->text();
    QString s31 =ui->comboBox_className_12->currentText();
    QString s33 =ui->lineEdit_receive3_3->text();
    QString s34 =ui->lineEdit_pay3_3->text();
    QString s35 =ui->lineEdit_sum_4->text();
    //page4
    QString s36 =ui->comboBox_className_1->currentText();
    QString s38 =ui->lineEdit_receive1->text();
    QString s39 =ui->lineEdit_pay1->text();
    QString s40 =ui->comboBox_className_2->currentText();
    QString s42 =ui->lineEdit_receive2->text();
    QString s43 =ui->lineEdit_pay2->text();
    QString s44 =ui->comboBox_className_3->currentText();
    QString s46 =ui->lineEdit_receive3->text();
    QString s47 =ui->dateTimeEdit1->text();
    QString s48 =ui->lineEdit_pay3->text();
    QString s49 =ui->comboBox_className_4->currentText();
    QString s51 =ui->lineEdit_receive4->text();
    QString s52 =ui->lineEdit_pay4->text();
    QString s53 =ui->lineEdit_sum_5->text();

    QSqlQuery sql_query;
    QVariantList date,No,name,sexual,phonenumber,studenttype,classname,money,payee1,payment,payee2,classsum,prim;
    QString insert_sql =  "INSERT INTO  user_information VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?)";
        //没有设定prime key
    s7 += 1;

    if(s7 ==1){

        sql_query.prepare(insert_sql); 
        sql_query.addBindValue(s1);
        sql_query.addBindValue(s2);
        sql_query.addBindValue(s3);
        sql_query.addBindValue(s4);
        sql_query.addBindValue(s5);
        sql_query.addBindValue(s6);


        sql_query.addBindValue(s8);//classname
        sql_query.addBindValue(s12);//总金额
        sql_query.addBindValue(s54);//收款单位
        sql_query.addBindValue(s55);//收款方式
        sql_query.addBindValue(s56);//收款人
        sql_query.addBindValue(s7);
        sql_query.addBindValue(primkey);
        primkey++;
       sql_query.exec();


    }
    else if (s7==2) {


        date<<s1<<s1;
        No <<s2<<s2;
        name<<s3<<s3;
        sexual<<s4<<s4;
        phonenumber <<s5<<s5;
        studenttype<<s6<<s6;
        classname<<s13<<s18;
        money <<s16<<s20;
        payee1<<s54<<s54;
        payment<<s55<<s55;
        payee2 <<s56<<s56;
        classsum<<s7<<s7;
        prim<<primkey<<primkey+1;
        sql_query.prepare(insert_sql);
        sql_query.addBindValue(date);
        sql_query.addBindValue(No);
        sql_query.addBindValue(name);
        sql_query.addBindValue(sexual);
        sql_query.addBindValue(phonenumber);
        sql_query.addBindValue(studenttype);
        sql_query.addBindValue(classname);
        sql_query.addBindValue(money);//金额
        sql_query.addBindValue(payee1);
        sql_query.addBindValue(payment);
        sql_query.addBindValue(payee2);
        sql_query.addBindValue(classsum);
        sql_query.addBindValue(prim);

        if(!sql_query.execBatch())
        {
            qDebug()<<sql_query.lastError();
        }
        else {
            primkey += 2;
        }

    }

    else if (s7==3) {

        date<<s1<<s1<<s1;
        No <<s2<<s2<<s2;
        name<<s3<<s3<<s3;
        sexual<<s4<<s4<<s4;
        phonenumber <<s5<<s5<<s5;
        studenttype<<s6<<s6<<s6;
        classname<<s23<<s27<<s31;
        money <<s25<<s29<<s33;
        payee1<<s54<<s54<<s54;
        payment<<s55<<s55<<s55;
        payee2 <<s56<<s56<<s56;
        classsum<<s7<<s7<<s7;
        prim<<primkey<<primkey+1<<primkey+2;

        sql_query.prepare(insert_sql);
        sql_query.addBindValue(date);
        sql_query.addBindValue(No);
        sql_query.addBindValue(name);
        sql_query.addBindValue(sexual);
        sql_query.addBindValue(phonenumber);
        sql_query.addBindValue(studenttype);
        sql_query.addBindValue(classname);
        sql_query.addBindValue(money);//金额
        sql_query.addBindValue(payee1);
        sql_query.addBindValue(payment);
        sql_query.addBind
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值