Нет вменяемого кода, нет практически ничего. Как по такому судить можно?
методы в порядке вызова :
C++ (Qt)
void TextModuleEditorDialog::on_addBtn_clicked()
{
QModelIndex index = _ui->treeView->currentIndex();
if (index.isValid())
{
qlonglong parentID = _controller->GetModuleID(index);
qlonglong newID = _controller->GetNextID();
ModulePtr module = ModulePtr(new Module());
module->SetModuleID(newID);
module->SetMasterID(parentID);
QStringList freeNumbers = _controller->GetFreeModuleNumbers(index);
if (freeNumbers.isEmpty())
{
_controller->SendMessage("Can not insert",this,MessageLevel::Error);
return;
}
_ui->treeView->expand(index);
AddOrEditDialog *addDialog = new AddOrEditDialog(module,freeNumbers,this);
addDialog->setWindowTitle("Inserting");
if (addDialog->exec() == QDialog::Accepted)
{
addDialog->UpdateModuleData();
if (_controller->InsertModule(module,index))
{
_controller->SendMessage("Module is inserted",this,MessageLevel::Success);
}
}
}
else
{
_controller->SendMessage("Set parent module!",this,MessageLevel::Error);
}
}
C++ (Qt)
bool ModelsController::InsertModule(ModulePtr module, QModelIndex viewParent)
{
QModelIndex viewIndex = viewParent;
QModelIndex sourseIndex = SourceIndex(viewParent);
qDebug()<<"--------------------------";
qDebug()<<_data->rowCount(SourceIndex(viewParent)) <<"- source model rows(before)";
qDebug()<<_view->rowCount(viewParent) <<"- view model rows(before)" ;
bool isInserted = _data->InsertModule(module,SourceIndex(viewParent));
qDebug()<<_data->rowCount(SourceIndex(viewParent)) <<"- source model rows(after)";
qDebug()<<_view->rowCount(viewParent) <<"- view model rows(after)";
qDebug()<<"----------------------------";
return isInserted;
}
C++ (Qt)
bool ModulesModel::InsertModule(ModulePtr module, QModelIndex parent)
{ bool result = false;
QMap<QString, QVariant> map;
map[Modules::Columns::number] = module->ModuleN();
map[Modules::Columns::name] = module->ModuleName();
result = insertChild(parent,map);
QModelIndex indexTL = index(0,0,parent);
QModelIndex indexBR = index(rowCount(parent),columnCount(parent),parent);
emit dataChanged(indexTL,indexBR);
return result;
}
C++ (Qt)
bool SqlTreeModel::insertChild(const QModelIndex &parent, const QMap<QString, QVariant> &data)
{
bool success = true;
IdType parentId = GetId(parent);
beginInsertRows(parent,0,0);
if (d->insertChild(parentId,data) == 0)
{
success = false;
}
endInsertRows();
return success;
}
C++ (Qt)
IdType TreeData::insertChild( IdType parent, const QMap<QString, QVariant>& data )
{
TreeNode* parentNode = node(parent);
if ( !parentNode )
return 0;
// TEST
if (_isManualSubmit)
{
IdType newId = GetNextId();
TreeNode* newNode = new TreeNode;
newNode->id = newId;
newNode->parent = parent;
newNode->modification = Inserted;
for ( int i = 0; i < _dataNames.count(); i++ )
{
QString name = _dataNames[i];
QVariant value;
if (name == _idName)
value = newNode->id;
else if (name == _parentName)
value = newNode->parent;
else
value = data[name];
newNode->data.append( value );
}
_history.insert(newId, newNode);
parentNode->children.append(newId);
return newId;
}
// END TEST
QSqlQuery query(_db);
QStringList names = data.keys();
QStringList values;
IdType newId = GetNextId();
foreach ( QVariant value, data.values() ) {
values.append( QString("'%1'").arg(value.toString()) );
}
names.prepend( _parentName );
values.prepend(QString("%1").arg(parent) );
names.prepend( _idName );
values.prepend(QString("%1").arg(newId) );
QString queryStr = QString( "INSERT INTO %1 (%2) VALUES(%3) RETURNING %4" )
.arg(_tableName)
.arg(names.join(", "))
.arg(values.join(", "))
.arg(_idName);
qDebug()<<queryStr;
if (!query.exec(queryStr) )
{
qDebug() << query.lastError().text();
return 0;
}
if (!query.next())
{
qDebug() << query.lastError().text();
return 0;
}
IdType id = query.value(0).toLongLong();
if ( id )
parentNode->children.append(id);
return id;
}