PDA

View Full Version : Adding a child of treeView



_exp_
25th July 2010, 17:31
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


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addfile.h"
#include "addModel.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model=new QStandardItemModel(0,4);
ui->treeView->setModel(model);
ui->treeView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->treeView->setSelectionBehavior(QAbstractItemView::SelectRows );
QDomDocument docDoc;
QFile file("BDwww.xml");
if (!file.open(QIODevice::ReadOnly))
{
if (!docDoc.setContent(&file)) {
QDomElement domElemNode= docDoc.documentElement();
readFile(domElemNode);
}
file.close();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
// Создание формы для добавления ссылки
void MainWindow::on_pushButton_clicked()
{
QFrame *messAdd_www = new QFrame;
messAdd_www->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
messAdd_www->setGeometry(QRect(QPoint(QCursor::pos().x(), QCursor::pos().y()), QSize(250, 250)));
messAdd_www->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
messAdd_www->setLineWidth(2);
closeButton= new QPushButton ("Closse",messAdd_www);
closeButton->setGeometry(120,180,90,30);
addButton=new QPushButton(tr("Add"),messAdd_www);
addButton->setGeometry(20,180,90,30);
linkWWW= new QLineEdit(messAdd_www);
messAdd_www->show();
connect(ui->treeView, SIGNAL(activated(const QModelIndex&)), this, SLOT(addChild(const QModelIndex&))); // THERE NOT WORKING
connect(addButton,SIGNAL(clicked()),messAdd_www,SL OT(close()));
connect(closeButton,SIGNAL(clicked()),messAdd_www, SLOT(close()));
}
// Создание формы для добавления категории
void MainWindow::on_pushButton_2_clicked()
{
QFrame *categoryAdd = new QFrame;
categoryAdd->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
categoryAdd->setGeometry(QRect(QPoint(QCursor::pos().x(), QCursor::pos().y()), QSize(250, 250)));
categoryAdd->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
categoryAdd->setLineWidth(2);
closeCategory= new QPushButton ("Closse",categoryAdd);
closeCategory->setGeometry(120,180,90,30);
addCategory=new QPushButton(tr("Add"),categoryAdd);
addCategory->setGeometry(20,180,90,30);
nameCategory= new QLineEdit(categoryAdd);
categoryAdd->show();
connect(addCategory,SIGNAL(clicked()),this,SLOT(ap pendixCategory()));
connect(addCategory,SIGNAL(clicked()),this,SLOT(ad dInFile()));
connect(addCategory,SIGNAL(clicked()),categoryAdd, SLOT(close()));
connect(closeCategory,SIGNAL(clicked()),categoryAd d,SLOT(close()));
}
void MainWindow::on_tabWidget_currentChanged(QWidget* )
{
}

.h


#define ADDMODEL_H
#include "mainwindow.h"
// Добавление потомка в категорию
void MainWindow::appendixWWW(const QModelIndex&)
{
qDebug()<<"Slot activ";
childItem = new QStandardItem();
childItem->setText(linkWWW->text());
parentItem->setChild(0,childItem);
}
//Добавление категории в модель
void MainWindow::appendixCategory()
{
parentItem = new QStandardItem();
parentItem->setText(nameCategory->text());
model->setItem(model->rowCount(),0, parentItem);
}
// Второй вариант добавления потомка
void MainWindow::addChild(const QModelIndex&)
{
qDebug()<<"Slot activ";
QModelIndex index = ui->treeView->selectionModel()->currentIndex();
QAbstractItemModel *model = ui->treeView->model();
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)
{
QModelIndex child = model->index(0, column, index);
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.


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.

ChrisW67
26th July 2010, 01:50
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).

_exp_
26th July 2010, 12:11
Thanks 2 weeks pained and had just inter press.