Hello, I'd like to create a simple application to show an xml file into a QTreeWidget.
I created the QMainWindow with QT designer the results is the ui_xmleditor.h file, then i made the XMLEditor.h class to implemets the manù funcionts.like open, etc...
The open function call the DomParser SLOT (which is defined in the XMlParser.h class) but it doenst work, the consolle show me the message:
Object::connect: No such slot XMlParser:omParser(QString)
Object::connect: (sender name: 'MainWindow')
I defined the XMlParser.h in this way cos i want to modify the QTreeWidget in ui_xmleditor.h.
Is my approach correct?
I post all the code down here.
XMLEditor.h
#include <QtGui/QWidget>
#include "ui_xmleditor.h"
#include "xmlParser.h"
#include <QtXml>
{
Q_OBJECT
public:
XMLEditor();
~XMLEditor();
signals:
void DomParser
(const QString &fileName
);
private slots:
void open();
private:
XMlParser *xmlParser;
public:
private:
Ui::MainWindow ui;
};
#include <QtGui/QWidget>
#include "ui_xmleditor.h"
#include "xmlParser.h"
#include <QtXml>
class XMLEditor : public QMainWindow
{
Q_OBJECT
public:
XMLEditor();
~XMLEditor();
signals:
void DomParser(const QString &fileName);
private slots:
void open();
private:
XMlParser *xmlParser;
public:
QString fileNameO;
private:
Ui::MainWindow ui;
};
To copy to clipboard, switch view to plain text mode
XMLEditor.cpp
#include <QtGui/QWidget>
#include <QFileDialog>
#include "xmleditor.h"
#include "xmlParser.h"
XMLEditor::XMLEditor()
{
ui.setupUi(this);
xmlParser = new XMlParser;
connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(open()));
//connect(this, SIGNAL(parseFile(const QString &fileName)), xmlParser, SLOT(xmlParser->DomParser(&fileName)));
connect(this,
SIGNAL(DomParser
(const QString)),
(XMlParser
*)xmlParser,
SLOT(DomParser
(const QString)));
//connect(this, SIGNAL(DomParser(const QString)), this, SLOT(open()));
}
XMLEditor::~XMLEditor()
{
}
void XMLEditor::open()
{
QString fileName
= QFileDialog::getOpenFileName(this, tr
("Open XML"),
".", tr
("XML files (*.xml)"));
emit DomParser("/home/mattia/workspace/XML/file.xml");
}
#include <QtGui/QWidget>
#include <QFileDialog>
#include "xmleditor.h"
#include "xmlParser.h"
XMLEditor::XMLEditor()
{
ui.setupUi(this);
xmlParser = new XMlParser;
connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(open()));
//connect(this, SIGNAL(parseFile(const QString &fileName)), xmlParser, SLOT(xmlParser->DomParser(&fileName)));
connect(this, SIGNAL(DomParser(const QString)), (XMlParser*)xmlParser, SLOT(DomParser(const QString)));
//connect(this, SIGNAL(DomParser(const QString)), this, SLOT(open()));
}
XMLEditor::~XMLEditor()
{
}
void XMLEditor::open()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open XML"), ".", tr("XML files (*.xml)"));
emit DomParser("/home/mattia/workspace/XML/file.xml");
}
To copy to clipboard, switch view to plain text mode
XMlparser.h
#include <QtGui/QWidget>
#include "ui_xmleditor.h"
{
Q_OBJECT
public:
XMlParser();
~XMlParser();
public:
public slots:
void DomParser
(const QString *fileName
);
private:
Ui::MainWindow ui;
};
#include <QtGui/QWidget>
#include "ui_xmleditor.h"
class QDomElement;
class XMlParser : public QMainWindow
{
Q_OBJECT
public:
XMlParser();
~XMlParser();
public:
void parseEntry(const QDomElement &element);
public slots:
void DomParser(const QString *fileName);
private:
Ui::MainWindow ui;
};
To copy to clipboard, switch view to plain text mode
xmlParser.cpp
#include "xmlParser.h"
#include "ui_xmleditor.h"
#include "xmleditor.h"
#include <QtGui/QWidget>
#include <QtXml>
#include <QMessageBox>
XMlParser::XMlParser(){}
XMlParser::~XMlParser(){}
void XMlParser
::DomParser(const QString *fileName
) {
ui.treeWidget->setWindowTitle("laaaaa");
QFile device
("/home/mattia/workspace/XML/file.xml");
int errorLine;
int errorColumn;
/*if (!doc.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
QMessageBox::warning(0, QObject::tr("DOM Parser"),
QObject::tr("Parse error at line %1, "
"column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr));
return;
}*/
if (root.tagName() != "utenti")
return;
while (!node.isNull()) {
if (node.toElement().tagName() == "utente")
parseEntry(node.toElement());
node = node.nextSibling();
}
}
{
ui.treeWidget->setWindowTitle("qui");
__item->setText(0,"persona");
while (!node.isNull()) {
if (node.toElement().tagName() == "nome") {
QDomNode childNodeName
= node.
firstChild();
__item1->setText(0, childNodeName.toText().data());
}else if (node.toElement().tagName() == "cognome") {
QDomNode childNodeSurname
= node.
firstChild();
__item1->setText(0, childNodeSurname.toText().data());
}else if (node.toElement().tagName() == "indirizzo") {
QDomNode childNodeAddress
= node.
firstChild();
__item1->setText(0, childNodeAddress.toText().data());
}
node = node.nextSibling();
}
}
#include "xmlParser.h"
#include "ui_xmleditor.h"
#include "xmleditor.h"
#include <QtGui/QWidget>
#include <QtXml>
#include <QMessageBox>
XMlParser::XMlParser(){}
XMlParser::~XMlParser(){}
void XMlParser::DomParser(const QString *fileName)
{
ui.treeWidget->setWindowTitle("laaaaa");
QFile device("/home/mattia/workspace/XML/file.xml");
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument doc;
/*if (!doc.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
QMessageBox::warning(0, QObject::tr("DOM Parser"),
QObject::tr("Parse error at line %1, "
"column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr));
return;
}*/
QDomElement root = doc.documentElement();
if (root.tagName() != "utenti")
return;
QDomNode node = root.firstChild();
while (!node.isNull()) {
if (node.toElement().tagName() == "utente")
parseEntry(node.toElement());
node = node.nextSibling();
}
}
void XMlParser::parseEntry(const QDomElement &element)
{
QDomNode node = element.firstChild();
ui.treeWidget->setWindowTitle("qui");
QTreeWidgetItem *__item = new QTreeWidgetItem(ui.treeWidget);
__item->setText(0,"persona");
while (!node.isNull()) {
if (node.toElement().tagName() == "nome") {
QDomNode childNodeName = node.firstChild();
QTreeWidgetItem *__item1 = new QTreeWidgetItem(__item);
__item1->setText(0, childNodeName.toText().data());
}else if (node.toElement().tagName() == "cognome") {
QDomNode childNodeSurname = node.firstChild();
QTreeWidgetItem *__item1 = new QTreeWidgetItem(__item);
__item1->setText(0, childNodeSurname.toText().data());
}else if (node.toElement().tagName() == "indirizzo") {
QDomNode childNodeAddress = node.firstChild();
QTreeWidgetItem *__item1 = new QTreeWidgetItem(__item);
__item1->setText(0, childNodeAddress.toText().data());
}
node = node.nextSibling();
}
}
To copy to clipboard, switch view to plain text mode
ui_xmleditor.h
#ifndef UI_XMLEDITOR_H
#define UI_XMLEDITOR_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QTreeWidget>
#include <QtGui/QWidget>
class Ui_MainWindow
{
public:
{
if (MainWindow->objectName().isEmpty())
MainWindow
->setObjectName
(QString::fromUtf8("MainWindow"));
MainWindow->resize(427, 542);
actionOpen
= new QAction(MainWindow
);
actionOpen
->setObjectName
(QString::fromUtf8("actionOpen"));
actionSave
= new QAction(MainWindow
);
actionSave
->setObjectName
(QString::fromUtf8("actionSave"));
actionAdd
= new QAction(MainWindow
);
actionAdd
->setObjectName
(QString::fromUtf8("actionAdd"));
actionDelete
= new QAction(MainWindow
);
actionDelete
->setObjectName
(QString::fromUtf8("actionDelete"));
centralwidget
= new QWidget(MainWindow
);
centralwidget
->setObjectName
(QString::fromUtf8("centralwidget"));
treeWidget
->setObjectName
(QString::fromUtf8("treeWidget"));
treeWidget
->setGeometry
(QRect(30,
10,
371,
481));
MainWindow->setCentralWidget(centralwidget);
menubar
->setObjectName
(QString::fromUtf8("menubar"));
menubar
->setGeometry
(QRect(0,
0,
427,
28));
menuFile
= new QMenu(menubar
);
menuFile
->setObjectName
(QString::fromUtf8("menuFile"));
menuEdit
= new QMenu(menubar
);
menuEdit
->setObjectName
(QString::fromUtf8("menuEdit"));
MainWindow->setMenuBar(menubar);
statusbar
->setObjectName
(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
menubar->addAction(menuFile->menuAction());
menubar->addAction(menuEdit->menuAction());
menuFile->addAction(actionOpen);
menuFile->addAction(actionSave);
menuEdit->addAction(actionAdd);
menuEdit->addAction(actionDelete);
retranslateUi(MainWindow);
} // setupUi
{
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
#endif // UI_XMLEDITOR_H
#ifndef UI_XMLEDITOR_H
#define UI_XMLEDITOR_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QTreeWidget>
#include <QtGui/QWidget>
class Ui_MainWindow
{
public:
QAction *actionOpen;
QAction *actionSave;
QAction *actionAdd;
QAction *actionDelete;
QWidget *centralwidget;
QTreeWidget *treeWidget;
QMenuBar *menubar;
QMenu *menuFile;
QMenu *menuEdit;
QStatusBar *statusbar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(427, 542);
actionOpen = new QAction(MainWindow);
actionOpen->setObjectName(QString::fromUtf8("actionOpen"));
actionSave = new QAction(MainWindow);
actionSave->setObjectName(QString::fromUtf8("actionSave"));
actionAdd = new QAction(MainWindow);
actionAdd->setObjectName(QString::fromUtf8("actionAdd"));
actionDelete = new QAction(MainWindow);
actionDelete->setObjectName(QString::fromUtf8("actionDelete"));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
treeWidget = new QTreeWidget(centralwidget);
treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
treeWidget->setGeometry(QRect(30, 10, 371, 481));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 427, 28));
menuFile = new QMenu(menubar);
menuFile->setObjectName(QString::fromUtf8("menuFile"));
menuEdit = new QMenu(menubar);
menuEdit->setObjectName(QString::fromUtf8("menuEdit"));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
menubar->addAction(menuFile->menuAction());
menubar->addAction(menuEdit->menuAction());
menuFile->addAction(actionOpen);
menuFile->addAction(actionSave);
menuEdit->addAction(actionAdd);
menuEdit->addAction(actionDelete);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
actionOpen->setText(QApplication::translate("MainWindow", "Open", 0, QApplication::UnicodeUTF8));
actionSave->setText(QApplication::translate("MainWindow", "Save", 0, QApplication::UnicodeUTF8));
actionAdd->setText(QApplication::translate("MainWindow", "Add", 0, QApplication::UnicodeUTF8));
actionDelete->setText(QApplication::translate("MainWindow", "Delete", 0, QApplication::UnicodeUTF8));
treeWidget->headerItem()->setText(0, QApplication::translate("MainWindow", "XML", 0, QApplication::UnicodeUTF8));
menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
menuEdit->setTitle(QApplication::translate("MainWindow", "Edit", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
#endif // UI_XMLEDITOR_H
To copy to clipboard, switch view to plain text mode
Bookmarks