#include "treeeditwidget.h"
#include "ui_treeeditwidget.h"
#include <QLineEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QDateTimeEdit>
#include <QDateEdit>
TreeEditWidget::TreeEditWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::TreeEditWidget)
{
ui->setupUi(this);
initForm();
}
TreeEditWidget::~TreeEditWidget()
{
delete ui;
}
void TreeEditWidget::initForm()
{
QStringList list;
list <<tr("任务名称")<<tr("测试次数")<<tr("紧急程度")<<tr("起始时间")<<tr("结束时间");
ui->treeWidget->setColumnCount(5); //设置列数
ui->treeWidget->setHeaderLabels(list); //设置头的标题
ui->treeWidget->setColumnWidth(0,150);
ui->treeWidget->setColumnWidth(1,100);
ui->treeWidget->setColumnWidth(2,100);
ui->treeWidget->setColumnWidth(3,100);
ui->treeWidget->setColumnWidth(4,100);
list.clear();
list<<"测试计划"<<""<<""<<"20201225"<<"202012261111";
QTreeWidgetItem *plan = new QTreeWidgetItem(ui->treeWidget,QStringList(list));
list.clear();
list<<"终端测试"<<"1"<<"普通"<<"20201225"<<"202012251122";
QTreeWidgetItem *task1 = new QTreeWidgetItem(plan,QStringList(list));
list.clear();
list<<"设备测试"<<"2"<<"紧急"<<"20201225"<<"202012251223";
QTreeWidgetItem *task2 = new QTreeWidgetItem(plan,QStringList(list));
list.clear();
list<<"系统测试"<<"3"<<"严重"<<"20201225"<<"202012261223";
QTreeWidgetItem *task3 = new QTreeWidgetItem(plan,QStringList(list));
}
void TreeEditWidget::slotFinishEdit()
{
if (doubleClickItem != nullptr)
{
QLineEdit *edit = qobject_cast<QLineEdit*>(ui->treeWidget->itemWidget(doubleClickItem, 0));
if (!edit)
return;
QString text = edit->text();
ui->treeWidget->removeItemWidget(doubleClickItem, 0);
doubleClickItem->setText(0, text);
}
}
void TreeEditWidget::slotValueChange()
{
if (doubleClickItem != nullptr)
{
QSpinBox *edit = qobject_cast<QSpinBox*>(ui->treeWidget->itemWidget(doubleClickItem, 1));
if (!edit)
return;
ui->treeWidget->removeItemWidget(doubleClickItem, 1);
doubleClickItem->setText(1, QString::number(edit->value()));
}
}
void TreeEditWidget::slotComboBoxChange(const QString &str)
{
if (doubleClickItem != nullptr)
{
QComboBox *edit = qobject_cast<QComboBox*>(ui->treeWidget->itemWidget(doubleClickItem, 2));
if (!edit)
return;
ui->treeWidget->removeItemWidget(doubleClickItem, 2);
doubleClickItem->setText(2, str);
}
}
void TreeEditWidget::slotDateTimeChange()
{
if (doubleClickItem != nullptr)
{
QDateTimeEdit *edit = qobject_cast<QDateTimeEdit*>(ui->treeWidget->itemWidget(doubleClickItem, 4));
if (!edit)
return;
QString str = edit->dateTime().toString("yyyyMMddHHmm");
ui->treeWidget->removeItemWidget(doubleClickItem, 4);
doubleClickItem->setText(4, str);
}
}
void TreeEditWidget::slotDateChange()
{
if (doubleClickItem != nullptr)
{
QDateEdit *edit = qobject_cast<QDateEdit*>(ui->treeWidget->itemWidget(doubleClickItem, 3));
if (!edit)
return;
QString str = edit->date().toString("yyyyMMdd");
ui->treeWidget->removeItemWidget(doubleClickItem, 3);
doubleClickItem->setText(3, str);
}
}
void TreeEditWidget::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
doubleClickItem = nullptr;
if (column == 0)
{
doubleClickItem = item;
QLineEdit* lineEdit = new QLineEdit(this);
lineEdit->setText(item->text(column));
connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(slotFinishEdit()));
ui->treeWidget->setItemWidget(doubleClickItem, 0, lineEdit);
}
else if(column == 1)
{
doubleClickItem = item;
QSpinBox* lineEdit = new QSpinBox(this);
lineEdit->setValue(1);
connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(slotValueChange()));
ui->treeWidget->setItemWidget(doubleClickItem, 1, lineEdit);
}
else if(column == 2)
{
doubleClickItem = item;
QComboBox* cbx = new QComboBox(this);
QStringList list;
list<<"普通"<<"紧急"<<"严重";
cbx->addItems(list);
connect(cbx, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(slotComboBoxChange(const QString &)));
ui->treeWidget->setItemWidget(doubleClickItem, 2, cbx);
}
else if(column == 3)
{
doubleClickItem = item;
QDateEdit* date = new QDateEdit(this);
date->setDate(QDate::currentDate());
connect(date, SIGNAL(editingFinished()), this, SLOT(slotDateChange()));
ui->treeWidget->setItemWidget(doubleClickItem, 3, date);
}
else if(column == 4)
{
doubleClickItem = item;
QDateTimeEdit* cbx = new QDateTimeEdit(this);
cbx->setDateTime(QDateTime::currentDateTime());
connect(cbx, SIGNAL(editingFinished()), this, SLOT(slotDateTimeChange()));
ui->treeWidget->setItemWidget(doubleClickItem, 4, cbx);
}
}
评论1