_exp_
25th July 2010, 16: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.
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.