小言_互联网的博客

qt tablewidget中添加自定义的list

611人阅读  评论(0)

qt tablewidget中添加自定义的列表;
列表中包含鼠标点击事件;
tablewidget行高随列表内容变化.

.h文件

#ifndef lefthzyqsbgzjlqjylb_H
#define lefthzyqsbgzjlqjylb_H

#include <QWidget>
#include "chuangjianyiqi.h"
#include "mytable.h"
#include "historySubmission.h"
#include "tinyxml2.h"
using namespace tinyxml2;

namespace Ui {
   
class lefthzyqsbgzjlqjylb;
}

class lefthzyqsbgzjlqjylb : public QWidget
{
   
    Q_OBJECT

public:
    explicit lefthzyqsbgzjlqjylb(QWidget *parent = 0);
    ~lefthzyqsbgzjlqjylb();
    void refresh();
    void addRow(QString uuid="", QString taskName="", QString type="", QString equipmentName="", QString id="", QString model="", QString measurementTrace="", QString time="", QString certificateNo="", QString organization="", QString expirationDate="",  QString remark="", QString attachments="");
    QStringList getTypeList();
    QStringList getNamesOfXType(QString type);
private slots:

    void on_btn_improt_clicked();//导入execl文件
    void on_btn_downloadExeclTemplate_clicked();
    myTable *create_HangQian_YiQiSheBeiBiao();
    void on_btn_add_clicked();
    void on_btn_update_clicked();
    void on_btn_del_clicked();
    void on_btn_save_clicked();
    void on_btn_addRow_clicked();

signals:
    qieHuanRenWu(QString projectName);
private:
    Ui::lefthzyqsbgzjlqjylb *ui;
    chuangjianyiqi *yiqi;
    myTable *table;
    historySubmission *w_historySub;

    QStringList typeList;
    XMLDocument doc;
    XMLElement *root;
};

#endif // lefthzyqsbgzjlqjylb_H

.cpp文件

#include "lefthzyqsbgzjlqjylb.h"
#include "ui_lefthzyqsbgzjlqjylb.h"
#include <QGridLayout>
#include <QDebug>
#include <QVector>
#include"operatorexcel.h"
#include "common.h"
#include <QFileDialog>
#include <QAxObject>
#include <QStringList>
#include <QDateEdit>
#include <QUuid>
#include "databasectrl.h"
#include "mycombobox.h"
#include "mymessagebox.h"
lefthzyqsbgzjlqjylb::lefthzyqsbgzjlqjylb(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::lefthzyqsbgzjlqjylb)
{
   
    ui->setupUi(this);
    ui->btn_downloadExeclTemplate->setVisible(false);
    ui->btn_improt->setVisible(false);
    ui->btn_add->setVisible(false);
    ui->btn_update->setVisible(false);
    table = create_HangQian_YiQiSheBeiBiao();
    (new QGridLayout(ui->widget_2))->addWidget(table);
//    ui->widget_2->layout()->addWidget(table);
    yiqi=new chuangjianyiqi();
    w_historySub=new historySubmission;
    (new QGridLayout(ui->widget_historySub))->addWidget(w_historySub);
    typeList=getTypeList();
    connect(w_historySub->table->tableWidget,&QTableWidget::doubleClicked,[=](QModelIndex index)
    {
   
        //获得鼠标点击的x,y坐标点
//        int x = pos.x ();
//        int y = pos.y ();
//        QModelIndex index = w_historySub->table->tableWidget->indexAt (QPoint(x,y));
//        qdebug<<index.model()->data(index).toString();
        common::task=w_historySub->table->tableWidget->item(index.row(),1)->text();
        refresh();
    });
}

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

void lefthzyqsbgzjlqjylb::refresh()
{
   
    emit qieHuanRenWu(common::task);
    table->tableWidget->setRowCount(0);
    QList<QStringList> submissionList=databaseCtrl::instance()->getHistorySubmission("tb_base_hz_device");
    w_historySub->table->setData(submissionList);
    if(databaseCtrl::instance()->getTaskNames().contains(common::task)==false)
    {
   
        return;
    }
    QStringList taskNameList;
    QStringList typeList;
    QStringList verification_institutionList;
    QString task_name=common::task;
    QList<QStringList> list=databaseCtrl::instance()->queryDevice(taskNameList, typeList,verification_institutionList,task_name,"","","","","hz");
//    table->setData(list);

    for(int i=0;i<list.size();i++)
    {
   
        QStringList temp=list.at(i);
        addRow(temp.at(0),temp.at(1),temp.at(2),temp.at(3),temp.at(4),temp.at(5),temp.at(6),temp.at(7),temp.at(8),temp.at(9),temp.at(10),temp.at(11),temp.at(12));
    }
    if(list.isEmpty())
    {
   
        QMessageBox msgBox;
        msgBox.setWindowTitle("仪器设备(工具计量器具)一览表(航中)页面提示:");
        msgBox.setText("未查询到任何信息!");
        msgBox.exec();
        return;
    }
//    table->tableWidget->horizontalHeader()->setStretchLastSection(true);//拉伸最后一列

}

void lefthzyqsbgzjlqjylb::addRow(QString uuid,QString taskName, QString type, QString equipmentName, QString id, QString model, QString measurementTrace, QString time, QString certificateNo, QString expirationDate,QString organization,  QString remark, QString attachments)
{
   
    //增加一行
    int row=table->tableWidget->rowCount()+1;
    int column=table->tableWidget->columnCount();
    table->tableWidget->setRowCount(row);
    if(uuid=="")
    {
   
        uuid=QUuid::createUuid().toString();
    }
    QTableWidgetItem *item = new QTableWidgetItem(uuid);
    item->setCheckState(Qt::Unchecked);
    table->tableWidget->setItem(row-1,0, item);

    //设置第一列为默认当前项目名称,并自动填充
    table->tableWidget->setItem(row-1,1,new QTableWidgetItem(taskName));

    //类别列表
    myComboBox *wTypeList=new myComboBox(this);
    wTypeList->setEditable(true);
    wTypeList->addItems(typeList);
    wTypeList->setCurrentText(type);
    wTypeList->setToolTip("点击编辑或下拉选择");
    table->tableWidget->setCellWidget(row-1,2,wTypeList);

    //名称列表
    myComboBox *wNameOfXTypeList=new myComboBox(this);
    wNameOfXTypeList->setEditable(true);
    wNameOfXTypeList->addItem("");
    wNameOfXTypeList->addItems(getNamesOfXType(type));
    wNameOfXTypeList->setCurrentText(equipmentName);
    wNameOfXTypeList->setToolTip("点击编辑或下拉选择");
    table->tableWidget->setCellWidget(row-1,3,wNameOfXTypeList);

    connect(wTypeList,&myComboBox::currentTextChanged,[=](QString currentType){
   
        wNameOfXTypeList->clear();
        wNameOfXTypeList->addItem("");
        wNameOfXTypeList->addItems(getNamesOfXType(currentType));
        wNameOfXTypeList->update();
    });
    //编号
    table->tableWidget->setItem(row-1,4,new QTableWidgetItem(id));
    //型号
    table->tableWidget->setItem(row-1,5,new QTableWidgetItem(model));
    //量值溯源方式
    myComboBox *wLiangZhiSuYuanList=new myComboBox(this);
    QStringList liangZhiSuYuanList;
    liangZhiSuYuanList<<""<<"检定"<<"校准"<<"自校"<<"比测";
    wLiangZhiSuYuanList->setEditable(true);
    wLiangZhiSuYuanList->addItems(liangZhiSuYuanList);
    wLiangZhiSuYuanList->setCurrentText(measurementTrace);
    wLiangZhiSuYuanList->setToolTip("点击编辑或下拉选择");
    table->tableWidget->setCellWidget(row-1,6,wLiangZhiSuYuanList);
    //检定/校准日期
    QDateEdit *checkDateEdit=new QDateEdit;
    checkDateEdit->setCalendarPopup(true);
    checkDateEdit->setDate(QDate::fromString(time,"yyyy-MM-dd"));
    table->tableWidget->setCellWidget(row-1,7,checkDateEdit);
    //证书编号
    table->tableWidget->setItem(row-1,8,new QTableWidgetItem(certificateNo));
    //有效期
    QDateEdit *dateEdit=new QDateEdit;
    dateEdit->setCalendarPopup(true);
    dateEdit->setDate(QDate::fromString(expirationDate,"yyyy/MM/dd"));
    table->tableWidget->setCellWidget(row-1,9,dateEdit);
    //检定/校准机构
    table->tableWidget->setItem(row-1,10,new QTableWidgetItem(organization));
    //备注
    table->tableWidget->setItem(row-1,11,new QTableWidgetItem(remark));
    //附件
    QWidget * w=new QWidget(this);
    QPushButton *btnUpload=new QPushButton(this);
    btnUpload->setText("上传");
    QVBoxLayout *vlayout=new QVBoxLayout;
    vlayout->setMargin(0);
    vlayout->setSpacing(0);
    vlayout->addWidget(btnUpload);

    QTableWidget *attachTable=new QTableWidget(0,3);
    attachTable->setObjectName("attachTable");
//    attachTable->setVisible(false);
        attachTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    attachTable->horizontalHeader()->hide();
    attachTable->verticalHeader()->hide();
    vlayout->addWidget(attachTable);
    w->setLayout(vlayout);
    table->tableWidget->setCellWidget(row-1,12,w);
    if(attachments!="")
    {
   
        QStringList attachlist=attachments.split(";",QString::SkipEmptyParts);

        for(int i=0;i<attachlist.size();i++)
        {
   
           qdebug<<attachlist;
            QStringList fileName_fileMd5=attachlist.at(i).split("|");
            if(fileName_fileMd5.size()<2)
            {
   
                continue;
            }
            QLabel *linkLabel=new QLabel;
            linkLabel->setText(fileName_fileMd5.at(0));
            linkLabel->setObjectName(fileName_fileMd5.at(1));
            QPushButton *btn_download=new QPushButton;
            btn_download->setIcon(QIcon(":/resource/下载.jpg"));
            QPushButton *btn_del=new QPushButton;
            btn_del->setIcon(QIcon(":/resource/删除.jpg"));
            int attachRow=attachTable->rowCount()+1;
            attachTable->setRowCount(attachRow);
            attachTable->setCellWidget(attachRow-1,0,btn_download);
            attachTable->setCellWidget(attachRow-1,1,btn_del);
            attachTable->setCellWidget(attachRow-1,2,linkLabel);
            connect(btn_download,QPushButton::clicked,[=]()
            {
   
                QPoint point=attachTable->mapFromGlobal(QCursor::pos());
                QModelIndex index = attachTable->indexAt (QPoint(point.x(),point.y()));
                QLabel *label=static_cast<QLabel*>(attachTable->cellWidget(index.row(),2));
                qdebug<<index.row()<<index.column()<<label->text();

            });
            connect(btn_del,QPushButton::clicked,[=]()
            {
   
                QPoint point=attachTable->mapFromGlobal(QCursor::pos());
                QModelIndex index = attachTable->indexAt (QPoint(point.x(),point.y()));
                qdebug<<index.row()<<index.column();
                attachTable->removeRow(index.row());
            });
        }
        attachTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应行高
        attachTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应行高
    }
    connect(btnUpload,&QPushButton::clicked,[=]()
    {
   
        int HHeaderHeight = table->tableWidget->horizontalHeader()->height();
        int VHeaderWidth = table->tableWidget->verticalHeader()->width();
        QPoint ppt=table->tableWidget->mapFromGlobal(QCursor::pos());
        QModelIndex index = table->tableWidget->indexAt (QPoint(ppt.x()-VHeaderWidth,ppt.y()-HHeaderHeight));//神奇!!
        if(index.isValid()==true)
        {
   
            table->tableWidget->setCurrentCell(index.row(),index.column());
            QObjectList objlist=table->tableWidget->cellWidget(index.row(),index.column())->children();
            for(int i=0;i<objlist.size();i++)
            {
   
                if(objlist.at(i)->objectName()=="attachTable")
                {
   
                    QTableWidget* attachTableObj= static_cast<QTableWidget*>(objlist.at(i));
                    for(int i=0;i<attachTableObj->rowCount();i++)
                    {
   
                        qdebug<<static_cast<QLabel*>(attachTableObj->cellWidget(i,2))->objectName();
                    }
                }
            }
        }
        else
        {
   
            qdebug<<"index is not Valid";
        }

        QStringList filePathList = QFileDialog::getOpenFileNames(this,
                                                                 "选择需要上传的附件文件", "../", "*");
        if(filePathList.isEmpty())
        {
   
            return;
        }
        else
        {
   
//            attachTable->setVisible(true);
            for(int i=0;i<filePathList.size();i++)
            {
   
                QFileInfo fileInfo=QFileInfo(filePathList.at(i));
                bool copyResult=common::copyFileToPath(fileInfo.absoluteFilePath(),QApplication::applicationDirPath() + "/attachments/"+common::getFileMd5(fileInfo.absoluteFilePath()));
                if(copyResult==true)
                {
   
                    QLabel *linkLabel=new QLabel;
                    linkLabel->setText(fileInfo.fileName());
                    linkLabel->setObjectName(common::getFileMd5(fileInfo.absoluteFilePath()));
                    QPushButton *btn_download=new QPushButton;
                    btn_download->setIcon(QIcon(":/resource/下载.jpg"));
                    QPushButton *btn_del=new QPushButton;
                    btn_del->setIcon(QIcon(":/resource/删除.jpg"));
                    int attachRow=attachTable->rowCount()+1;
                    attachTable->setRowCount(attachRow);
                    attachTable->setCellWidget(attachRow-1,0,btn_download);
                    attachTable->setCellWidget(attachRow-1,1,btn_del);
                    attachTable->setCellWidget(attachRow-1,2,linkLabel);
                    connect(btn_download,QPushButton::clicked,[=]()
                    {
   
                        QPoint point=attachTable->mapFromGlobal(QCursor::pos());
                        QModelIndex index = attachTable->indexAt (QPoint(point.x(),point.y()));
                        QLabel *label=static_cast<QLabel*>(attachTable->cellWidget(index.row(),2));
                        qdebug<<index.row()<<index.column()<<label->text();
                        common::downLoadFileFromSomeDir(this,QApplication::applicationDirPath() +"/attachments",label->objectName(),label->text());

                    });
                    connect(btn_del,QPushButton::clicked,[=]()
                    {
   
                        QPoint point=attachTable->mapFromGlobal(QCursor::pos());
                        QModelIndex index = attachTable->indexAt (QPoint(point.x(),point.y()));
                        qdebug<<index.row()<<index.column();
                        attachTable->removeRow(index.row());

                    });
                    attachTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应行高
                    attachTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应行高
                }
            }
        }
        int attachmentHeight =attachTable->rowCount()*25+30;
        if(table->tableWidget->rowHeight(table->tableWidget->currentRow())<attachmentHeight)
        {
   
            table->tableWidget->setRowHeight(table->tableWidget->currentRow(),attachmentHeight);
        }
    });
}

QStringList lefthzyqsbgzjlqjylb::getTypeList()
{
   
    if(!doc.LoadFile(QString(QApplication::applicationDirPath() + "/config/oceanDevices.xml").toUtf8().data()));
    {
   
        doc.PrintError();
    }
    root=doc.RootElement();
    XMLElement *tempnode=root->FirstChild()->ToElement();
    while(tempnode!=NULL)
    {
   
        typeList<<tempnode->Attribute("typeName");
        tempnode=tempnode->NextSiblingElement();//获取同级别的下一个兄弟元素
    }
    return typeList;
}

QStringList lefthzyqsbgzjlqjylb::getNamesOfXType(QString type)
{
   
    XMLElement *tempnode=root->FirstChild()->ToElement();
    QStringList devicesOfTypeXList;
    while(tempnode!=NULL)
    {
   
        if(tempnode->Attribute("typeName")==type)
        {
   
            XMLElement *nameElement=tempnode->FirstChildElement();
            while(nameElement!=NULL)
            {
   
                devicesOfTypeXList<<nameElement->GetText();
                nameElement=nameElement->NextSiblingElement();
            }
            break;
        }
        tempnode=tempnode->NextSiblingElement();//获取同级别的下一个兄弟元素
    }
    return devicesOfTypeXList;

}


void lefthzyqsbgzjlqjylb::on_btn_improt_clicked()
{
   

    QString path = QFileDialog::getOpenFileName(this,
                    "选择需要导入的Execl文件", "../", "Execl(*.xlsx)");
    if(path.isEmpty())
    {
   
        return;
    }
    OperatorExcel myExecl;
    QVector<DatasetDevice> data;
    QList<QStringList> yiqiList;
    myExecl.readExcelVoyageEquipment(path,data,0,2);
    for(int i=0;i<data.size();i++)
    {
   
        if(data.at(i).task.isEmpty())
        {
   
            continue;
        }
        QStringList l;
        //类别	仪器(标准物质)名称	编号	型号	量值溯源方式	检定/校准日期	证书编号	有效期	检定/校准机构	备注
        l.push_back(data.at(i).task);
        l.push_back(data.at(i).type);
        l.push_back(data.at(i).equipmentName);
        l.push_back(data.at(i).id);
        l.push_back(data.at(i).model);
        l.push_back(data.at(i).measurementTrace);
        l.push_back(data.at(i).time);
        l.push_back(data.at(i).certificateNo);
        l.push_back(data.at(i).expirationDate);
        l.push_back(data.at(i).organization);
        l.push_back(data.at(i).remark);
        yiqiList.append(l);

        tb_base_device device;
        device.type=data.at(i).type;  //类别
        device.name=data.at(i).equipmentName;//名称
        device.id =data.at(i).id;//编号
        device.model=data.at(i).model;//型号
        device.traceability=data.at(i).measurementTrace;//量值溯源方式
        device.checkdate=QDate::fromString(data.at(i).time,"yyyy-MM-dd");//校准日期
        device.certificate_number=data.at(i).certificateNo;//证书编号
        device.validity=data.at(i).expirationDate;  //有效期
        device.verification_institutions=data.at(i).organization;//校准机构
        device.remark=data.at(i).remark;   //备注
        databaseCtrl::instance()->addDevice(device,"hz");
    }
    table->setData(yiqiList);

}

void lefthzyqsbgzjlqjylb::on_btn_downloadExeclTemplate_clicked()
{
   
    QString fileName="航次仪器设备(工作计量器具)一览表.xlsx";
    common::downLoadExcelTemplate(this,fileName);
}

myTable *lefthzyqsbgzjlqjylb::create_HangQian_YiQiSheBeiBiao()
{
   
    QStringList header;
    header<<"航次任务名称"<<"类别"<<"仪器(标准物质)名称"<<"编号"<<"型号"<<"量值溯源方式"<<"检定/校准日期"<<"证书编号"<<"有效期"<<"检定/校准机构"<<"备注"<<"附件";
    myTable *table=new myTable(0,header.size());
    table->setHHeader(header);
    table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
//        table->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应文本,自动调整列宽,数据多时界面卡
//    table->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应文本,自动调整行高,数据多时界面卡
//    table->setMinimumWidth(1200);
//        table->setFixedWidth(1200);
    QList<int> list;
    list<<25<<153<<152<<190<<60<<60<<100<<105<<73<<106<<145<<64<<112;
    table->setColumnsWidth(list,table->tableWidget);
    return table;
}

void lefthzyqsbgzjlqjylb::on_btn_add_clicked()
{
   
    if(databaseCtrl::instance()->getTaskNames().contains(common::task)==false)
    {
   
        QMessageBox msgBox;
        msgBox.setText("请切换到相应任务!");
        msgBox.exec();
        return;
    }
    chuangjianyiqi *w_yiqi=new chuangjianyiqi();
    w_yiqi->setWindowModality(Qt::ApplicationModal);
    w_yiqi->show();
    w_yiqi->raise();
    connect(w_yiqi,&chuangjianyiqi::addDevice,[&](tb_base_device new_device){
   
        QStringList list;
        list.append(common::task);
        list.append(new_device.type);
        list.append(new_device.name);
        list.append(new_device.id);
        list.append(new_device.model);
        list.append(new_device.traceability);
        list.append(new_device.checkdate.toString());
        list.append(new_device.certificate_number);
        list.append(new_device.validity);
        list.append(new_device.verification_institutions);
        list.append(new_device.remark);

        bool result =databaseCtrl::instance()->addDevice(new_device,"hz");

        if(result)
        {
   
            table->addRow(list);
            QMessageBox msgBox;
            msgBox.setText("仪器设备(工作计量器具)信息添加成功!");
            msgBox.exec();
        }
        else
        {
   
            QMessageBox msgBox;
            msgBox.setText("仪器设备(工作计量器具)信息添加失败!");
            msgBox.exec();
        }
    });
}

void lefthzyqsbgzjlqjylb::on_btn_update_clicked()
{
   
    int row=table->tableWidget->currentRow();
    if(row<0)
    {
   
        QMessageBox msgBox;
        msgBox.setText("请先点击选中需要修改的行!");
        msgBox.exec();
        return;
    }
    tb_base_device old_device;
    old_device.type=table->tableWidget->item(row,2)->text();
    old_device.name=table->tableWidget->item(row,3)->text();
    old_device.id=table->tableWidget->item(row,4)->text();
    old_device.model=table->tableWidget->item(row,5)->text();
    old_device.traceability=table->tableWidget->item(row,6)->text();
    old_device.checkdate=QDate::fromString(table->tableWidget->item(row,7)->text());
    old_device.certificate_number=table->tableWidget->item(row,8)->text();
    old_device.validity=table->tableWidget->item(row,9)->text();
    old_device.verification_institutions=table->tableWidget->item(row,10)->text();
    old_device.remark=table->tableWidget->item(row,11)->text();
    chuangjianyiqi *w_yiqi=new chuangjianyiqi();
    w_yiqi->updateDevice(old_device);
    w_yiqi->setWindowModality(Qt::ApplicationModal);
    w_yiqi->show();
    w_yiqi->raise();
    connect(w_yiqi,&chuangjianyiqi::addDevice,[=](tb_base_device new_device){
   
        QStringList list;
        list.append(new_device.type);
        list.append(new_device.name);
        list.append(new_device.id);
        list.append(new_device.model);
        list.append(new_device.traceability);
        list.append(new_device.checkdate.toString("yyyy-MM-dd"));
        list.append(new_device.certificate_number);
        list.append(new_device.validity);
        list.append(new_device.verification_institutions);
        list.append(new_device.remark);
        QString oldUniqueId=QString("%1_%2_%3_%4").arg(common::task).arg(old_device.type).arg(old_device.name).arg(old_device.id);
        int result =databaseCtrl::instance()->updateDevice(oldUniqueId,new_device,"hz");
        if(result)
        {
   
            table->tableWidget->setItem(row,1 ,new QTableWidgetItem(common::task));
            table->tableWidget->setItem(row,2 ,new QTableWidgetItem(new_device.type));
            table->tableWidget->setItem(row,3 ,new QTableWidgetItem(new_device.name));
            table->tableWidget->setItem(row,4 ,new QTableWidgetItem(new_device.id));
            table->tableWidget->setItem(row,5 ,new QTableWidgetItem(new_device.model));
            table->tableWidget->setItem(row,6 ,new QTableWidgetItem(new_device.traceability));
            table->tableWidget->setItem(row,7 ,new QTableWidgetItem(new_device.checkdate.toString("yyyy-MM-dd")));
            table->tableWidget->setItem(row,8 ,new QTableWidgetItem(new_device.certificate_number));
            table->tableWidget->setItem(row,9 ,new QTableWidgetItem(new_device.validity));
            table->tableWidget->setItem(row,10,new QTableWidgetItem(new_device.verification_institutions));
            table->tableWidget->setItem(row,11,new QTableWidgetItem(new_device.remark));
            QMessageBox msgBox;
            msgBox.setText("仪器设备(工作计量器具)信息修改成功!");
            msgBox.exec();
        }
        else
        {
   
            QMessageBox msgBox;
            msgBox.setText("仪器设备(工作计量器具)信息修改失败!");
            msgBox.exec();
        }
    });
}

void lefthzyqsbgzjlqjylb::on_btn_del_clicked()
{
   
    QList<int> list=table->getSelectedRow();
    if(list.isEmpty())
    {
   
        QMessageBox::information(this, QString("提示"),QString("请勾选后点击删除!"),QMessageBox::Yes);
        return;
    }
    QString delList;
    QStringList uniqueIdList;
    for(int i=0;i<list.size();i++)
    {
   
        QString type=static_cast<myComboBox*>(table->tableWidget->cellWidget(list.at(i),2))->currentText();//从第一行往下获取
        QString name=static_cast<myComboBox*>(table->tableWidget->cellWidget(list.at(i),3))->currentText();//从第一行往下获取
        QString id=table->tableWidget->item(list.at(i),4)->text();//从第一行往下获取
        delList.append(type).append("-").append(name).append("-").append(id).append("\n");
        if(table->tableWidget->item(list.at(i),0)!=NULL)
        {
   
            uniqueIdList.append(table->tableWidget->item(list.at(i),0)->text());
        }
    }
    int ret = QMessageBox::warning(this, QString("提示"),QString("确定要删除:\n%1?").arg(delList),QMessageBox::Yes,QMessageBox::Cancel);
    if(ret==QMessageBox::Yes)
    {
   

        for(int i=0;i<uniqueIdList.size();i++)
        {
   
//            databaseCtrl::instance()->delDevice(uniqueIdList.at(i),"hq");
            databaseCtrl::instance()->delDevice(uniqueIdList.at(i),"hz");
        }
        table->myremoveRow(list);
    }
}


void lefthzyqsbgzjlqjylb::on_btn_save_clicked()
{
   
    if(databaseCtrl::instance()->getTaskNames().contains(common::task)==false)
    {
   
        return;
    }
    for(int i=0;i<(table->tableWidget->rowCount());i++)
    {
   
        tb_base_device device;
        device.uuid=table->tableWidget->item(i,0)->text();
        device.task=table->tableWidget->item(i,1)->text();
        device.type=static_cast<myComboBox*>(table->tableWidget->cellWidget(i,2))->currentText();
        device.name=static_cast<myComboBox*>(table->tableWidget->cellWidget(i,3))->currentText();
        device.id=table->tableWidget->item(i,4)->text();
        device.model=table->tableWidget->item(i,5)->text();
        device.traceability=static_cast<myComboBox*>(table->tableWidget->cellWidget(i,6))->currentText();
        device.checkdate=static_cast<QDateEdit*>(table->tableWidget->cellWidget(i,7))->date();
        device.certificate_number=table->tableWidget->item(i,8)->text();
        device.validity=static_cast<QDateEdit*>(table->tableWidget->cellWidget(i,9))->date().toString("yyyy/MM/dd");
        device.verification_institutions=table->tableWidget->item(i,10)->text();
        device.remark=table->tableWidget->item(i,11)->text();
        QObjectList objlist=table->tableWidget->cellWidget(i,12)->children();
        QString attachmentListStr;
        for(int i=0;i<objlist.size();i++)
        {
   
            if(objlist.at(i)->objectName()=="attachTable")
            {
   
                QTableWidget* attachTableObj= static_cast<QTableWidget*>(objlist.at(i));
                for(int i=0;i<attachTableObj->rowCount();i++)
                {
   
                    QLabel *temp=static_cast<QLabel*>(attachTableObj->cellWidget(i,2));
                    QString fileName_FileMd5=QString("%1|%2").arg(temp->text()).arg(temp->objectName());
                    attachmentListStr.append(fileName_FileMd5).append(";");
                }
            }
        }
        device.attachments=attachmentListStr;
//        databaseCtrl::instance()->addDevice(device,"hq");
        databaseCtrl::instance()->addDevice(device,"hz");
    }
}

void lefthzyqsbgzjlqjylb::on_btn_addRow_clicked()
{
   
    //判断当前项目是否为空
    if(databaseCtrl::instance()->getTaskNames().contains(common::task)==false)
    {
   
        QMessageBox msgBox;
        msgBox.setWindowTitle("航次仪器设备(工作计量器具)一览表页面提示:");
        msgBox.setText("当前任务为空,请选择任务后再进行填写!");
        msgBox.exec();
        return;
    }
    addRow("",common::task);
    table->tableWidget->scrollToBottom();
    QList<int> lst=table->getColumnsWidths();
    table->setColumnsWidth(lst,table->tableWidget);
}

.ui文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>lefthzyqsbgzjlqjylb</class>
 <widget class="QWidget" name="lefthzyqsbgzjlqjylb">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1114</width>
    <height>740</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QGridLayout" name="gridLayout_2">
   <property name="leftMargin">
    <number>0</number>
   </property>
   <property name="topMargin">
    <number>0</number>
   </property>
   <property name="rightMargin">
    <number>0</number>
   </property>
   <property name="bottomMargin">
    <number>0</number>
   </property>
   <property name="spacing">
    <number>0</number>
   </property>
   <item row="0" column="0">
    <widget class="QLabel" name="label">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <property name="font">
      <font>
       <family>Times New Roman</family>
       <pointsize>15</pointsize>
       <weight>75</weight>
       <bold>true</bold>
      </font>
     </property>
     <property name="text">
      <string>仪器设备(工作计量器具)一览表(航中)</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item row="2" column="0">
    <widget class="QWidget" name="widget_2" native="true">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
    </widget>
   </item>
   <item row="1" column="0">
    <widget class="QWidget" name="widget" native="true">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <layout class="QGridLayout" name="gridLayout">
      <item row="0" column="1">
       <widget class="QPushButton" name="btn_improt">
        <property name="text">
         <string>导入Execl文件</string>
        </property>
       </widget>
      </item>
      <item row="0" column="0">
       <widget class="QPushButton" name="btn_downloadExeclTemplate">
        <property name="text">
         <string>下载Execl模板</string>
        </property>
       </widget>
      </item>
      <item row="0" column="5">
       <widget class="QPushButton" name="btn_update">
        <property name="text">
         <string>修改</string>
        </property>
       </widget>
      </item>
      <item row="0" column="4">
       <widget class="QPushButton" name="btn_add">
        <property name="text">
         <string>新建</string>
        </property>
       </widget>
      </item>
      <item row="0" column="6">
       <widget class="QPushButton" name="btn_del">
        <property name="text">
         <string>删除</string>
        </property>
       </widget>
      </item>
      <item row="0" column="2">
       <spacer name="horizontalSpacer">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>648</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
      <item row="0" column="3">
       <widget class="QPushButton" name="btn_addRow">
        <property name="text">
         <string>新增行</string>
        </property>
       </widget>
      </item>
      <item row="0" column="7">
       <widget class="QPushButton" name="btn_save">
        <property name="text">
         <string>保存</string>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
   <item row="0" column="1" rowspan="3">
    <widget class="QWidget" name="widget_historySub" native="true">
     <property name="minimumSize">
      <size>
       <width>10</width>
       <height>0</height>
      </size>
     </property>
     <property name="maximumSize">
      <size>
       <width>200</width>
       <height>16777215</height>
      </size>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

ui.h文件

/********************************************************************************
** Form generated from reading UI file 'lefthzyqsbgzjlqjylb.ui'
**
** Created by: Qt User Interface Compiler version 5.9.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_LEFTHZYQSBGZJLQJYLB_H
#define UI_LEFTHZYQSBGZJLQJYLB_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_lefthzyqsbgzjlqjylb
{
   
public:
    QGridLayout *gridLayout_2;
    QLabel *label;
    QWidget *widget_2;
    QWidget *widget;
    QGridLayout *gridLayout;
    QPushButton *btn_improt;
    QPushButton *btn_downloadExeclTemplate;
    QPushButton *btn_update;
    QPushButton *btn_add;
    QPushButton *btn_del;
    QSpacerItem *horizontalSpacer;
    QPushButton *btn_addRow;
    QPushButton *btn_save;
    QWidget *widget_historySub;

    void setupUi(QWidget *lefthzyqsbgzjlqjylb)
    {
   
        if (lefthzyqsbgzjlqjylb->objectName().isEmpty())
            lefthzyqsbgzjlqjylb->setObjectName(QStringLiteral("lefthzyqsbgzjlqjylb"));
        lefthzyqsbgzjlqjylb->resize(1114, 740);
        QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(0);
        sizePolicy.setHeightForWidth(lefthzyqsbgzjlqjylb->sizePolicy().hasHeightForWidth());
        lefthzyqsbgzjlqjylb->setSizePolicy(sizePolicy);
        gridLayout_2 = new QGridLayout(lefthzyqsbgzjlqjylb);
        gridLayout_2->setSpacing(0);
        gridLayout_2->setObjectName(QStringLiteral("gridLayout_2"));
        gridLayout_2->setContentsMargins(0, 0, 0, 0);
        label = new QLabel(lefthzyqsbgzjlqjylb);
        label->setObjectName(QStringLiteral("label"));
        QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Minimum);
        sizePolicy1.setHorizontalStretch(0);
        sizePolicy1.setVerticalStretch(0);
        sizePolicy1.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
        label->setSizePolicy(sizePolicy1);
        QFont font;
        font.setFamily(QStringLiteral("Times New Roman"));
        font.setPointSize(15);
        font.setBold(true);
        font.setWeight(75);
        label->setFont(font);
        label->setAlignment(Qt::AlignCenter);

        gridLayout_2->addWidget(label, 0, 0, 1, 1);

        widget_2 = new QWidget(lefthzyqsbgzjlqjylb);
        widget_2->setObjectName(QStringLiteral("widget_2"));
        QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Expanding);
        sizePolicy2.setHorizontalStretch(0);
        sizePolicy2.setVerticalStretch(0);
        sizePolicy2.setHeightForWidth(widget_2->sizePolicy().hasHeightForWidth());
        widget_2->setSizePolicy(sizePolicy2);

        gridLayout_2->addWidget(widget_2, 2, 0, 1, 1);

        widget = new QWidget(lefthzyqsbgzjlqjylb);
        widget->setObjectName(QStringLiteral("widget"));
        sizePolicy1.setHeightForWidth(widget->sizePolicy().hasHeightForWidth());
        widget->setSizePolicy(sizePolicy1);
        gridLayout = new QGridLayout(widget);
        gridLayout->setObjectName(QStringLiteral("gridLayout"));
        btn_improt = new QPushButton(widget);
        btn_improt->setObjectName(QStringLiteral("btn_improt"));

        gridLayout->addWidget(btn_improt, 0, 1, 1, 1);

        btn_downloadExeclTemplate = new QPushButton(widget);
        btn_downloadExeclTemplate->setObjectName(QStringLiteral("btn_downloadExeclTemplate"));

        gridLayout->addWidget(btn_downloadExeclTemplate, 0, 0, 1, 1);

        btn_update = new QPushButton(widget);
        btn_update->setObjectName(QStringLiteral("btn_update"));

        gridLayout->addWidget(btn_update, 0, 5, 1, 1);

        btn_add = new QPushButton(widget);
        btn_add->setObjectName(QStringLiteral("btn_add"));

        gridLayout->addWidget(btn_add, 0, 4, 1, 1);

        btn_del = new QPushButton(widget);
        btn_del->setObjectName(QStringLiteral("btn_del"));

        gridLayout->addWidget(btn_del, 0, 6, 1, 1);

        horizontalSpacer = new QSpacerItem(648, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        gridLayout->addItem(horizontalSpacer, 0, 2, 1, 1);

        btn_addRow = new QPushButton(widget);
        btn_addRow->setObjectName(QStringLiteral("btn_addRow"));

        gridLayout->addWidget(btn_addRow, 0, 3, 1, 1);

        btn_save = new QPushButton(widget);
        btn_save->setObjectName(QStringLiteral("btn_save"));

        gridLayout->addWidget(btn_save, 0, 7, 1, 1);


        gridLayout_2->addWidget(widget, 1, 0, 1, 1);

        widget_historySub = new QWidget(lefthzyqsbgzjlqjylb);
        widget_historySub->setObjectName(QStringLiteral("widget_historySub"));
        widget_historySub->setMinimumSize(QSize(10, 0));
        widget_historySub->setMaximumSize(QSize(200, 16777215));

        gridLayout_2->addWidget(widget_historySub, 0, 1, 3, 1);


        retranslateUi(lefthzyqsbgzjlqjylb);

        QMetaObject::connectSlotsByName(lefthzyqsbgzjlqjylb);
    } // setupUi

    void retranslateUi(QWidget *lefthzyqsbgzjlqjylb)
    {
   
        lefthzyqsbgzjlqjylb->setWindowTitle(QApplication::translate("lefthzyqsbgzjlqjylb", "Form", Q_NULLPTR));
        label->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\344\273\252\345\231\250\350\256\276\345\244\207\357\274\210\345\267\245\344\275\234\350\256\241\351\207\217\345\231\250\345\205\267\357\274\211\344\270\200\350\247\210\350\241\250(\350\210\252\344\270\255)", Q_NULLPTR));
        btn_improt->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\345\257\274\345\205\245Execl\346\226\207\344\273\266", Q_NULLPTR));
        btn_downloadExeclTemplate->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\344\270\213\350\275\275Execl\346\250\241\346\235\277", Q_NULLPTR));
        btn_update->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\344\277\256\346\224\271", Q_NULLPTR));
        btn_add->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\346\226\260\345\273\272", Q_NULLPTR));
        btn_del->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\345\210\240\351\231\244", Q_NULLPTR));
        btn_addRow->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\346\226\260\345\242\236\350\241\214", Q_NULLPTR));
        btn_save->setText(QApplication::translate("lefthzyqsbgzjlqjylb", "\344\277\235\345\255\230", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
   
    class lefthzyqsbgzjlqjylb: public Ui_lefthzyqsbgzjlqjylb {
   };
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_LEFTHZYQSBGZJLQJYLB_H


转载:https://blog.csdn.net/u012484779/article/details/116796184
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场