Results 1 to 7 of 7

Thread: Error signal connection between 2 classes

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Error signal connection between 2 classes

    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
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include "ui_xmleditor.h"
    3. #include "xmlParser.h"
    4. #include <QtXml>
    5.  
    6. class XMLEditor : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. XMLEditor();
    12. ~XMLEditor();
    13. signals:
    14. void DomParser(const QString &fileName);
    15. private slots:
    16. void open();
    17. private:
    18. XMlParser *xmlParser;
    19. public:
    20. QString fileNameO;
    21. private:
    22. Ui::MainWindow ui;
    23. };
    To copy to clipboard, switch view to plain text mode 

    XMLEditor.cpp
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include <QFileDialog>
    3. #include "xmleditor.h"
    4. #include "xmlParser.h"
    5.  
    6.  
    7. XMLEditor::XMLEditor()
    8. {
    9. ui.setupUi(this);
    10. xmlParser = new XMlParser;
    11.  
    12. connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(open()));
    13. //connect(this, SIGNAL(parseFile(const QString &fileName)), xmlParser, SLOT(xmlParser->DomParser(&fileName)));
    14. connect(this, SIGNAL(DomParser(const QString)), (XMlParser*)xmlParser, SLOT(DomParser(const QString)));
    15. //connect(this, SIGNAL(DomParser(const QString)), this, SLOT(open()));
    16.  
    17. }
    18.  
    19. XMLEditor::~XMLEditor()
    20. {
    21.  
    22. }
    23.  
    24.  
    25. void XMLEditor::open()
    26. {
    27. QString fileName = QFileDialog::getOpenFileName(this, tr("Open XML"), ".", tr("XML files (*.xml)"));
    28. emit DomParser("/home/mattia/workspace/XML/file.xml");
    29. }
    To copy to clipboard, switch view to plain text mode 

    XMlparser.h
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include "ui_xmleditor.h"
    3.  
    4.  
    5.  
    6. class XMlParser : public QMainWindow
    7. {
    8. Q_OBJECT
    9. public:
    10. XMlParser();
    11. ~XMlParser();
    12. public:
    13. void parseEntry(const QDomElement &element);
    14. public slots:
    15. void DomParser(const QString *fileName);
    16. private:
    17. Ui::MainWindow ui;
    18. };
    To copy to clipboard, switch view to plain text mode 

    xmlParser.cpp
    Qt Code:
    1. #include "xmlParser.h"
    2. #include "ui_xmleditor.h"
    3. #include "xmleditor.h"
    4. #include <QtGui/QWidget>
    5. #include <QtXml>
    6. #include <QMessageBox>
    7.  
    8. XMlParser::XMlParser(){}
    9.  
    10. XMlParser::~XMlParser(){}
    11.  
    12. void XMlParser::DomParser(const QString *fileName)
    13. {
    14. ui.treeWidget->setWindowTitle("laaaaa");
    15. QFile device("/home/mattia/workspace/XML/file.xml");
    16. QString errorStr;
    17. int errorLine;
    18. int errorColumn;
    19. /*if (!doc.setContent(device, true, &errorStr, &errorLine,
    20.   &errorColumn)) {
    21.   QMessageBox::warning(0, QObject::tr("DOM Parser"),
    22.   QObject::tr("Parse error at line %1, "
    23.   "column %2:\n%3")
    24.   .arg(errorLine)
    25.   .arg(errorColumn)
    26.   .arg(errorStr));
    27.   return;
    28.   }*/
    29. QDomElement root = doc.documentElement();
    30. if (root.tagName() != "utenti")
    31. return;
    32. QDomNode node = root.firstChild();
    33. while (!node.isNull()) {
    34. if (node.toElement().tagName() == "utente")
    35. parseEntry(node.toElement());
    36. node = node.nextSibling();
    37. }
    38. }
    39.  
    40. void XMlParser::parseEntry(const QDomElement &element)
    41. {
    42. QDomNode node = element.firstChild();
    43. ui.treeWidget->setWindowTitle("qui");
    44. QTreeWidgetItem *__item = new QTreeWidgetItem(ui.treeWidget);
    45. __item->setText(0,"persona");
    46. while (!node.isNull()) {
    47.  
    48.  
    49. if (node.toElement().tagName() == "nome") {
    50. QDomNode childNodeName = node.firstChild();
    51. QTreeWidgetItem *__item1 = new QTreeWidgetItem(__item);
    52. __item1->setText(0, childNodeName.toText().data());
    53.  
    54. }else if (node.toElement().tagName() == "cognome") {
    55. QDomNode childNodeSurname = node.firstChild();
    56. QTreeWidgetItem *__item1 = new QTreeWidgetItem(__item);
    57. __item1->setText(0, childNodeSurname.toText().data());
    58.  
    59. }else if (node.toElement().tagName() == "indirizzo") {
    60. QDomNode childNodeAddress = node.firstChild();
    61. QTreeWidgetItem *__item1 = new QTreeWidgetItem(__item);
    62. __item1->setText(0, childNodeAddress.toText().data());
    63. }
    64. node = node.nextSibling();
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 

    ui_xmleditor.h
    Qt Code:
    1. #ifndef UI_XMLEDITOR_H
    2. #define UI_XMLEDITOR_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QMainWindow>
    9. #include <QtGui/QMenu>
    10. #include <QtGui/QMenuBar>
    11. #include <QtGui/QStatusBar>
    12. #include <QtGui/QTreeWidget>
    13. #include <QtGui/QWidget>
    14.  
    15. class Ui_MainWindow
    16. {
    17. public:
    18. QAction *actionOpen;
    19. QAction *actionSave;
    20. QAction *actionAdd;
    21. QAction *actionDelete;
    22. QWidget *centralwidget;
    23. QTreeWidget *treeWidget;
    24. QMenuBar *menubar;
    25. QMenu *menuFile;
    26. QMenu *menuEdit;
    27. QStatusBar *statusbar;
    28.  
    29. void setupUi(QMainWindow *MainWindow)
    30. {
    31. if (MainWindow->objectName().isEmpty())
    32. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    33. MainWindow->resize(427, 542);
    34. actionOpen = new QAction(MainWindow);
    35. actionOpen->setObjectName(QString::fromUtf8("actionOpen"));
    36. actionSave = new QAction(MainWindow);
    37. actionSave->setObjectName(QString::fromUtf8("actionSave"));
    38. actionAdd = new QAction(MainWindow);
    39. actionAdd->setObjectName(QString::fromUtf8("actionAdd"));
    40. actionDelete = new QAction(MainWindow);
    41. actionDelete->setObjectName(QString::fromUtf8("actionDelete"));
    42. centralwidget = new QWidget(MainWindow);
    43. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    44. treeWidget = new QTreeWidget(centralwidget);
    45. treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
    46. treeWidget->setGeometry(QRect(30, 10, 371, 481));
    47. MainWindow->setCentralWidget(centralwidget);
    48. menubar = new QMenuBar(MainWindow);
    49. menubar->setObjectName(QString::fromUtf8("menubar"));
    50. menubar->setGeometry(QRect(0, 0, 427, 28));
    51. menuFile = new QMenu(menubar);
    52. menuFile->setObjectName(QString::fromUtf8("menuFile"));
    53. menuEdit = new QMenu(menubar);
    54. menuEdit->setObjectName(QString::fromUtf8("menuEdit"));
    55. MainWindow->setMenuBar(menubar);
    56. statusbar = new QStatusBar(MainWindow);
    57. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    58. MainWindow->setStatusBar(statusbar);
    59.  
    60. menubar->addAction(menuFile->menuAction());
    61. menubar->addAction(menuEdit->menuAction());
    62. menuFile->addAction(actionOpen);
    63. menuFile->addAction(actionSave);
    64. menuEdit->addAction(actionAdd);
    65. menuEdit->addAction(actionDelete);
    66.  
    67. retranslateUi(MainWindow);
    68.  
    69. QMetaObject::connectSlotsByName(MainWindow);
    70. } // setupUi
    71.  
    72. void retranslateUi(QMainWindow *MainWindow)
    73. {
    74. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    75. actionOpen->setText(QApplication::translate("MainWindow", "Open", 0, QApplication::UnicodeUTF8));
    76. actionSave->setText(QApplication::translate("MainWindow", "Save", 0, QApplication::UnicodeUTF8));
    77. actionAdd->setText(QApplication::translate("MainWindow", "Add", 0, QApplication::UnicodeUTF8));
    78. actionDelete->setText(QApplication::translate("MainWindow", "Delete", 0, QApplication::UnicodeUTF8));
    79. treeWidget->headerItem()->setText(0, QApplication::translate("MainWindow", "XML", 0, QApplication::UnicodeUTF8));
    80. menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
    81. menuEdit->setTitle(QApplication::translate("MainWindow", "Edit", 0, QApplication::UnicodeUTF8));
    82. } // retranslateUi
    83.  
    84. };
    85.  
    86. namespace Ui {
    87. class MainWindow: public Ui_MainWindow {};
    88. } // namespace Ui
    89.  
    90. #endif // UI_XMLEDITOR_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Error signal connection between 2 classes

    I'd say, don't waste your time replicating the DOM tree structure to QTreeWidgetItems. Rather, use the model-view approach and wrap the existing DOM tree to a QAbstractItemModel.

    PS. There's an example available: Simple DOM Model.

    PPS. The solution to your signal-slot connection problem is to refactor
    Qt Code:
    1. void DomParser(const QString *fileName);
    To copy to clipboard, switch view to plain text mode 
    as
    Qt Code:
    1. void DomParser(const QString &fileName);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error signal connection between 2 classes

    I got it, but i have some problems to understand what's the parent id and what's its functions.
    For example if I have created a layout wiht QT Designer and i want to implement other function i have to include the ui_class.h into my class.h, i'll add some functions into class.h and i'll implement them in class.cpp.
    Now i want to modify, for example a Widget, that is declared in ui_class.h but to do this i want to create another class just to handling the data into the Widget, what have i to do to have a riferiment at the elements in ui_class.h?
    I hope you understant it cos i know that my english isn't so good

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Error signal connection between 2 classes

    Quote Originally Posted by mattia View Post
    I got it, but i have some problems to understand what's the parent id and what's its functions.
    I'm sorry, what parent id are you talking about?

    For example if I have created a layout wiht QT Designer and i want to implement other function i have to include the ui_class.h into my class.h, i'll add some functions into class.h and i'll implement them in class.cpp.
    Correct.

    Now i want to modify, for example a Widget, that is declared in ui_class.h but to do this i want to create another class just to handling the data into the Widget, what have i to do to have a riferiment at the elements in ui_class.h?
    Yes, you might want to implement the model in separate files. In this case you would place a QTreeView on the form in Qt Designer. Then, in class.cpp you would a) include model.h (or whatever it's called), b) create an instance of the model, and c) anywhere after calling setupUi() (which creates the treeview) you would set the model to the treeview with QAbstractItemView::setModel().

    Qt Code:
    1. // class.cpp
    2. #include "class.h"
    3. #include "model.h" // <-- a
    4.  
    5. Class::Class(QWidget* parent) : QWidget(parent)
    6. {
    7. ui.setupUi(this);
    8.  
    9. Model* model = new Model(ui.treeView); // <-- b
    10. ui.treeView->setModel(model); // <-- c
    11. }
    To copy to clipboard, switch view to plain text mode 

    I hope you understant it cos i know that my english isn't so good
    It's ok. Mine is not that good either..
    J-P Nurmi

  5. #5
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error signal connection between 2 classes

    Ok, thanks so much for your suggestion, I made as you told me and if I pass a Widget to another class i can modify it.
    But what's happen if i want to pass to another class a pointer to QMainWindow to have acces at all the interface members(I mean the ui_class Widget put with QT Designer).
    Down here the code.
    In ui_guiwid.h I just added a QLabel labelPark and an menù action actionChangeLabel.
    On doSmt.cpp the main error.
    Thanks

    guiwid.h
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include "ui_guiwid.h"
    3.  
    4. class guiwid : public QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. guiwid();
    10. ~guiwid();
    11. private slots:
    12. void callDoStm();
    13. private:
    14. Ui::MainWindow ui;
    15. };
    To copy to clipboard, switch view to plain text mode 


    guiwid.cpp
    Qt Code:
    1. #include "guiwid.h"
    2. #include "doSmt.h"
    3. #include <QtGui/QWidget>
    4.  
    5. guiwid::guiwid()
    6. {
    7. ui.setupUi(this);
    8. connect(ui.actionChangeLabel, SIGNAL(triggered()), this, SLOT(callDoStm()));
    9. }
    10.  
    11. guiwid::~guiwid()
    12. {
    13.  
    14. }
    15.  
    16. void guiwid::callDoStm(){
    17. doSmt *ds = new doSmt(ui.MainWindow);
    18. ds->changeLabel();
    19. }
    To copy to clipboard, switch view to plain text mode 


    doSmt.h
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include <QMainWindow>
    3.  
    4. class doSmt
    5. {
    6. public:
    7. doSmt(QMainWindow *mainWindow);
    8. ~doSmt();
    9. public:
    10. void changeLabel();
    11. QMainWindow *mainWindow;
    12. };
    To copy to clipboard, switch view to plain text mode 


    doSmt.cpp
    Qt Code:
    1. #include "doSmt.h"
    2. #include <QtGui/QWidget>
    3. #include <QLabel>
    4.  
    5. doSmt::doSmt(QMainWindow *mainWindow)
    6. {
    7. this->mainWindow = mainWindow;
    8. }
    9.  
    10. doSmt::~doSmt()
    11. {
    12.  
    13. }
    14.  
    15. void doSmt::changeLabel()
    16. {
    17. mainWindow->labelPark->setText("Yeap!"); //I can't access on this member
    18. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Error signal connection between 2 classes

    Well, to be honest, I think that's quite bad design. Contents of "Ui::MainWindow ui" is private part of "guiwid". Nobody else should be able to access them, really. Rather, "guiwid" should provide an interface to allow needed tasks to be done. Thanks to Qt's signals and slots, you could even keep "doSmt" unware of "guiwid". "doSmt" could for example just emit a signal which is connected to a slot in "guiwid".
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    mattia (31st October 2007)

  8. #7
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error signal connection between 2 classes

    Ok, i got it. Thanks so much again, now i know something more about how to design class on QT.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.