OfficeAssistant_Win10/OfficeAssistant_msvc/addcategory.cpp

77 lines
2.3 KiB
C++
Raw Normal View History

2023-08-18 19:09:55 +08:00
#include "addcategory.h"
2023-08-13 12:16:22 +08:00
#include <QString>
2023-08-18 19:09:55 +08:00
#include "config.h"
2023-08-13 12:16:22 +08:00
AddCategory::AddCategory(QWidget* parent)
: QDialog(parent)
{
ui.setupUi(this);
setWindowFlags(Qt::WindowCloseButtonHint);
setFixedSize(900, 600);
//connect(ui.submit, &QPushButton::click, this, &AddCategory::submit);
QStringList title;
ui.tableWidget->setColumnCount(3);
2023-08-18 19:09:55 +08:00
title << QString::fromLocal8Bit("名称") << QString::fromLocal8Bit("显示") << QString::fromLocal8Bit("排序");
2023-08-13 12:16:22 +08:00
ui.tableWidget->setHorizontalHeaderLabels(title);
2023-08-18 19:09:55 +08:00
sqlite_helper.get_category(categrories,true);
2023-08-13 12:16:22 +08:00
ui.tableWidget->setRowCount(categrories.count());
int i = 0;
for(auto category : categrories)
{
ui.tableWidget->setItem(i, 0, new QTableWidgetItem(category.name));
2023-08-18 19:09:55 +08:00
if(category.id==ALL||category.id==OTHERS||category.id==ELITE)
2023-08-13 12:16:22 +08:00
{
ui.tableWidget->item(i, 0)->setFlags(ui.tableWidget->item(i, 0)->flags() & (~Qt::ItemIsEditable));
}
QComboBox* combo_box = new QComboBox;
2023-08-18 19:09:55 +08:00
combo_box->addItem(QString::fromLocal8Bit("显示"), true);
combo_box->addItem(QString::fromLocal8Bit("不显示"), false);
2023-08-13 12:16:22 +08:00
if(category.display==true)
{
combo_box->setCurrentIndex(0);
}else
{
combo_box->setCurrentIndex(1);
}
ui.tableWidget->setCellWidget(i, 1, combo_box);
ui.tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(category.sort)));
combo_boxes << combo_box;
i++;
}
2023-08-18 19:09:55 +08:00
//submit_btn = new QPushButton(QString::fromLocal8Bit("提交"));
2023-08-13 12:16:22 +08:00
connect(ui.submit, &QPushButton::clicked, this, &AddCategory::submit);
}
AddCategory::~AddCategory()
{
for(auto combo_box : combo_boxes)
{
delete combo_box;
}
}
void AddCategory::submit()
{
int num = categrories.count();
QList<Categrory> categrories_new;
int i=0;
for(Categrory categrory:categrories)
{
categrory.name = ui.tableWidget->item(i, 0)->text();
QComboBox * combox =(QComboBox *)ui.tableWidget->cellWidget(i, 1);
categrory.display = combox->currentData().toBool();
bool ok;
categrory.sort = ui.tableWidget->item(i, 2)->text().toInt(&ok, 10);
if(!ok)
{
2023-08-18 19:09:55 +08:00
QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("排序列中有一行不是数字"));
2023-08-13 12:16:22 +08:00
return;
}
categrories_new << categrory;
i++;
}
categrories.clear();
categrories = categrories_new;
changed = true;
close();
}