其他分享
首页 > 其他分享> > QT用Qtableview按时间段检索数据

QT用Qtableview按时间段检索数据

作者:互联网

最近开始学习QT开发,做了一个项目,Helpdesk系统,在用tableview按时间段检索数据时浪费了两天时间,终于解决了,分享一下,重要的一环就是这个'\%1\' AND '\%2\':

        ui->tableView_SearchByEngineer->clearSelection ();

    QSqlTableModel *model  = new QSqlTableModel(this);
    model->setTable("Ticket");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    QDateTime origin_time = QDateTime::fromString("2000-01-01 08:00:00","yyyy-MM-dd hh:mm:ss");
    QDateTime startDatetime = ui->dateEdit_Start_Date->dateTime();
    QDateTime endDatetime = ui->dateEdit_End_Date->dateTime();
    qint64 nStartSteps = origin_time.msecsTo(startDatetime);
    qint64 nEndSteps = origin_time.msecsTo(endDatetime);
    if(nStartSteps>nEndSteps)
    {
        QMessageBox mesg;
        mesg.warning(this,"Error","Your start time is later than end time!");
        return;
    }
//    QString sStartSteps = QString("%1").arg(nStartSteps);
//    QString sEndSteps = QString("%1").arg(nEndSteps);
    QString sFilter = "";//数据库查询语句

    sFilter += tr("issueCreateTime between '\%1\' AND '\%2\'").arg(startDatetime.toString ("yyyy-MM-dd hh:mm:ss")).arg(endDatetime.toString ("yyyy-MM-dd hh:mm:ss"));
    if(ui->comboBox_By_Engineer->currentText() != "Select...")
    {
        sFilter +=" AND itEngineer=\'" + ui->comboBox_By_Engineer->currentText() + "\'";
        if (ui->comboBox_By_Cases_Status->currentText() != "Select...")
        {
            sFilter +=" AND issueStatus=\'" + ui->comboBox_By_Cases_Status->currentText() + "\'";
        }
    }
    model->setFilter(sFilter);    //查询数据库
    model->select();//选中数据库中所有条目
    ui->tableView_SearchByEngineer->setModel(model);
    model->setHeaderData(0,Qt::Horizontal,"ID号");
    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,"IT工程师");
    model->setHeaderData(12,Qt::Horizontal,"备注");
//    ui->AllCasestableView->horizontalHeader()->setStyleSheet("QHeaderView::section {"
//            "color: black;padding-left: 4px;border: 1px solid #6c6c6c;}");
    ui->tableView_SearchByEngineer->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->stackedWidget->setCurrentWidget (ui->page_Cases_By_Engineer);


最终生成的model filter语句就是:"issueCreateTime between '2022-01-01 08:00:00' AND '2022-11-27 08:00:00' AND itEngineer='Engineer1' AND issueStatus='Hold'"

标签:检索,00,QT,setHeaderData,Qtableview,ui,model,Horizontal,Qt
来源: https://www.cnblogs.com/managechina/p/16534951.html