You must add the child if the parent is selected the left mouse button.
Advised to connect to the signal model
void activated ( const QModelIndex & index )
but the child was not added to what was the reason?
Slot is not called.
.cpp
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "addfile.h"
  4. #include "addModel.h"
  5. MainWindow::MainWindow(QWidget *parent) :
  6. QMainWindow(parent),
  7. ui(new Ui::MainWindow)
  8. {
  9. ui->setupUi(this);
  10. model=new QStandardItemModel(0,4);
  11. ui->treeView->setModel(model);
  12. ui->treeView->setSelectionMode(QAbstractItemView::SingleSelection);
  13. ui->treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
  14. QDomDocument docDoc;
  15. QFile file("BDwww.xml");
  16. if (!file.open(QIODevice::ReadOnly))
  17. {
  18. if (!docDoc.setContent(&file)) {
  19. QDomElement domElemNode= docDoc.documentElement();
  20. readFile(domElemNode);
  21. }
  22. file.close();
  23. }
  24. }
  25. MainWindow::~MainWindow()
  26. {
  27. delete ui;
  28. }
  29. void MainWindow::changeEvent(QEvent *e)
  30. {
  31. QMainWindow::changeEvent(e);
  32. switch (e->type()) {
  33. case QEvent::LanguageChange:
  34. ui->retranslateUi(this);
  35. break;
  36. default:
  37. break;
  38. }
  39. }
  40. // Создание формы для добавления ссылки
  41. void MainWindow::on_pushButton_clicked()
  42. {
  43. QFrame *messAdd_www = new QFrame;
  44. messAdd_www->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
  45. messAdd_www->setGeometry(QRect(QPoint(QCursor::pos().x(), QCursor::pos().y()), QSize(250, 250)));
  46. messAdd_www->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
  47. messAdd_www->setLineWidth(2);
  48. closeButton= new QPushButton ("Closse",messAdd_www);
  49. closeButton->setGeometry(120,180,90,30);
  50. addButton=new QPushButton(tr("Add"),messAdd_www);
  51. addButton->setGeometry(20,180,90,30);
  52. linkWWW= new QLineEdit(messAdd_www);
  53. messAdd_www->show();
  54. [COLOR="red"]connect(ui->treeView, SIGNAL(activated(const QModelIndex&)), this, SLOT(addChild(const QModelIndex&))); [/COLOR]// THERE NOT WORKING
  55. connect(addButton,SIGNAL(clicked()),messAdd_www,SLOT(close()));
  56. connect(closeButton,SIGNAL(clicked()),messAdd_www,SLOT(close()));
  57. }
  58. // Создание формы для добавления категории
  59. void MainWindow::on_pushButton_2_clicked()
  60. {
  61. QFrame *categoryAdd = new QFrame;
  62. categoryAdd->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
  63. categoryAdd->setGeometry(QRect(QPoint(QCursor::pos().x(), QCursor::pos().y()), QSize(250, 250)));
  64. categoryAdd->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
  65. categoryAdd->setLineWidth(2);
  66. closeCategory= new QPushButton ("Closse",categoryAdd);
  67. closeCategory->setGeometry(120,180,90,30);
  68. addCategory=new QPushButton(tr("Add"),categoryAdd);
  69. addCategory->setGeometry(20,180,90,30);
  70. nameCategory= new QLineEdit(categoryAdd);
  71. categoryAdd->show();
  72. connect(addCategory,SIGNAL(clicked()),this,SLOT(appendixCategory()));
  73. connect(addCategory,SIGNAL(clicked()),this,SLOT(addInFile()));
  74. connect(addCategory,SIGNAL(clicked()),categoryAdd,SLOT(close()));
  75. connect(closeCategory,SIGNAL(clicked()),categoryAdd,SLOT(close()));
  76. }
  77. void MainWindow::on_tabWidget_currentChanged(QWidget* )
  78. {
  79. }
To copy to clipboard, switch view to plain text mode 
.h
Qt Code:
  1. #define ADDMODEL_H
  2. #include "mainwindow.h"
  3. // Добавление потомка в категорию
  4. void MainWindow::appendixWWW(const QModelIndex&)
  5. {
  6. qDebug()<<"Slot activ";
  7. childItem = new QStandardItem();
  8. childItem->setText(linkWWW->text());
  9. parentItem->setChild(0,childItem);
  10. }
  11. //Добавление категории в модель
  12. void MainWindow::appendixCategory()
  13. {
  14. parentItem = new QStandardItem();
  15. parentItem->setText(nameCategory->text());
  16. model->setItem(model->rowCount(),0, parentItem);
  17. }
  18. // Второй вариант добавления потомка
  19. void MainWindow::addChild(const QModelIndex&)
  20. {
  21. qDebug()<<"Slot activ";
  22. QModelIndex index = ui->treeView->selectionModel()->currentIndex();
  23. QAbstractItemModel *model = ui->treeView->model();
  24. if (model->columnCount(index) == 0) {
  25. if (!model->insertColumn(0, index))
  26. {
  27. return;
  28. }
  29. }
  30. if (!model->insertRow(0, index))
  31. {
  32. return;
  33. }
  34. for (int column = 0; column < model->columnCount(index); ++column)
  35. {
  36. QModelIndex child = model->index(0, column, index);
  37. model->setData(child, QVariant("[No data]"), Qt::EditRole);
  38. if (!model->headerData(column, Qt::Horizontal).isValid())
  39. {
  40. model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"),Qt::EditRole);
  41. }
  42. }
  43. }
  44. #endif // ADDMODEL_H
To copy to clipboard, switch view to plain text mode 
I've tried, and so the same problem slot is not called.
Qt Code:
  1. connect(ui->treeView->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(addChild(const QItemSelection&,const QItemSelection&)));
To copy to clipboard, switch view to plain text mode 

P.S: Sorry for the mistakes! I'm Russian.