Adding a child of treeView
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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addfile.h"
#include "addModel.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->treeView->setModel(model);
{
if (!docDoc.setContent(&file)) {
readFile(domElemNode);
}
file.close();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
// Создание формы Ð´Ð»Ñ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð¸Ñ ÑÑылки
void MainWindow::on_pushButton_clicked()
{
messAdd_www->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
messAdd_www
->setFrameStyle
(QFrame::WinPanel |
QFrame::Raised);
messAdd_www->setLineWidth(2);
closeButton->setGeometry(120,180,90,30);
addButton->setGeometry(20,180,90,30);
messAdd_www->show();
[COLOR="red"]connect(ui->treeView, SIGNAL(activated(const QModelIndex&)), this, SLOT(addChild(const QModelIndex&))); [/COLOR]// THERE NOT WORKING
connect(addButton,SIGNAL(clicked()),messAdd_www,SLOT(close()));
connect(closeButton,SIGNAL(clicked()),messAdd_www,SLOT(close()));
}
// Создание формы Ð´Ð»Ñ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð¸Ñ ÐºÐ°Ñ‚ÐµÐ³Ð¾Ñ€Ð¸Ð¸
void MainWindow::on_pushButton_2_clicked()
{
categoryAdd->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
categoryAdd
->setFrameStyle
(QFrame::WinPanel |
QFrame::Raised);
categoryAdd->setLineWidth(2);
closeCategory->setGeometry(120,180,90,30);
addCategory->setGeometry(20,180,90,30);
categoryAdd->show();
connect(addCategory,SIGNAL(clicked()),this,SLOT(appendixCategory()));
connect(addCategory,SIGNAL(clicked()),this,SLOT(addInFile()));
connect(addCategory,SIGNAL(clicked()),categoryAdd,SLOT(close()));
connect(closeCategory,SIGNAL(clicked()),categoryAdd,SLOT(close()));
}
void MainWindow
::on_tabWidget_currentChanged(QWidget* ) {
}
.h
Code:
#define ADDMODEL_H
#include "mainwindow.h"
// Добавление потомка в категорию
void MainWindow::appendixWWW(const QModelIndex&)
{
qDebug()<<"Slot activ";
childItem->setText(linkWWW->text());
parentItem->setChild(0,childItem);
}
//Добавление категории в модель
void MainWindow::appendixCategory()
{
parentItem->setText(nameCategory->text());
model->setItem(model->rowCount(),0, parentItem);
}
// Второй вариант Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð¸Ñ Ð¿Ð¾Ñ‚Ð¾Ð¼ÐºÐ°
void MainWindow::addChild(const QModelIndex&)
{
qDebug()<<"Slot activ";
QModelIndex index
= ui
->treeView
->selectionModel
()->currentIndex
();
if (model->columnCount(index) == 0) {
if (!model->insertColumn(0, index))
{
return;
}
}
if (!model->insertRow(0, index))
{
return;
}
for (int column = 0; column < model->columnCount(index); ++column)
{
model
->setData
(child,
QVariant("[No data]"), Qt
::EditRole);
if (!model->headerData(column, Qt::Horizontal).isValid())
{
model
->setHeaderData
(column, Qt
::Horizontal,
QVariant("[No header]"),Qt
::EditRole);
}
}
}
#endif // ADDMODEL_H
I've tried, and so the same problem slot is not called.
Code:
connect(ui->treeView->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(addChild(const QItemSelection&,const QItemSelection&)));
P.S: Sorry for the mistakes! I'm Russian.
Re: Adding a child of treeView
Check that your "slot" is actually declared as a slot. If it is not a slot then an error message will be output when you run the application and the connect() is called.
The activated() signal is only sent when the cell is activated (usually by double-clicking or pressing Enter on the cell).
Re: Adding a child of treeView
Thanks 2 weeks pained and had just inter press.