Hello forum users! Describing the situation: the AddressBookParser class does not see the TextEdit class. Both are derived from the corresponding QT library classes. * Errors *:
'TextEdit' doesn’t name a type TextEdit * edit;
AND
addressBookParser :: AddressBookParser (TextEdit *, QString) 'AddressBookParser * parcer = new AddressBookParser (this, edit-> text ());
Ie everywhere where I address or create an object of type TextEdit
I tried everything I know, starting from the TextEdit prototype before declaring AddressBookParcer (I read somewhere that if files are connected to each other in the list 1-> 2-> 3, then 3 can recognize the 2nd class, xs as it is called), ending with the addition strictly correct constructors of type TextEdit (QWidget * p): QTextEdit (p) {}.
Why does the compiler say that there is no TextEdit? And how to fix it? I appeal to your wisdom, THANKS! The PS program looks at the XML document, and prints the contact by number ("number =" i "), which is entered in QLineEdit and should output the contents of this contact to TextEdit, and yes, SAX is used here (See M. Posting Qt5.3 Chapter 40)
//addressbookparcer.h #ifndef ADDRESSBOOKPARSER_H #define ADDRESSBOOKPARSER_H #include <QXmlDefaultHandler> #include <QDebug> #include <QtWidgets> #include <QException> #include <QMessageBox> #include "textedit.h" class AddressBookParser : public QXmlDefaultHandler { private: TextEdit* edit; QString _text; // данные int number; bool finded; public: AddressBookParser(TextEdit* e, QString num) { number = num.toInt(); edit = e; finded = false; } //переопределенные методы, расположены в порядке вызова virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts) //атрибуты документа { finded = false; for(int i = 0; i < atts.count(); i++){ if(atts.value(i) == QString::number(number)) { edit->setText(edit->toPlainText() + "\nAttribute: " + atts.value(i)); finded = true; } } return true; } virtual bool characters(const QString& text) { _text = text; return true; } virtual bool endElement(const QString&, const QString&, const QString& str) { if(str != "contact" && str != "addressbook" && finded) { //не обрабатываем /contact, /addressbook edit->setText(edit->toPlainText() + "\nTag name: " + str + "\t Text: " + _text); } return true; } virtual bool fatalError(const QXmlParseException& ex) //обработка исключительных ситуаций { qDebug() << "Line: " << ex.lineNumber() << "\t Column: " << ex.columnNumber() << "\t Message: " << ex.message(); return false; } }; #endif // ADDRESSBOOKPARSER_H //textedit.h #ifndef TEXTEDIT_H #define TEXTEDIT_H #include "addressbookparser.h" class TextEdit : public QTextEdit { Q_OBJECT private: QLineEdit* edit; public: TextEdit(QLineEdit* edit){ this->edit = edit; } public slots: void parcerSlot(bool) { bool f; edit->text().toInt(&f); if(!f) return; AddressBookParser* parcer = new AddressBookParser(this,edit->text()); QFile* file = new QFile("addressbook.xml"); QXmlInputSource source(file); //объект источника для парсинга документа QXmlSimpleReader reader; //ридер документа reader.setContentHandler(parcer); //устанавливаем обработчик reader.parse(source); //запуск парсинга } }; #endif // TEXTEDIT_H //main.cpp #include "textedit.h" int main(int argc, char** argv) { try{ QApplication app(argc, argv); QWidget wgt; wgt.resize(600,100); QVBoxLayout* layout = new QVBoxLayout(&wgt); QLineEdit* line = new QLineEdit; TextEdit* edit = new TextEdit(line); QPushButton* button = new QPushButton("обработать"); layout->addWidget(line); layout->addWidget(edit); layout->addWidget(button); wgt.show(); QObject::connect(button, SIGNAL(clicked(bool)), edit, SLOT(parcerSlot(bool))); return app.exec(); } catch(QException& ex) //обработка ошибок { QMessageBox M; M.setText(QString(ex.what())); M.show(); } }