So, hab wieder ein Problem und zwar speichere ich in der Methode "createAttirbutes" das QWebElement in der Eigenschaft "webElement" und beim ausführen des Slots "setHtmlAttribute" greife ich auf das "webElement", aber es ist NULL obwohl der Slot nach der Methode ausgeführt wird. Wieso? Wenn ich direkt aus der "createAttributes" auf den Slot zugreife dann ist "webElement" nicht NULL??? Kapiere ich nicht.
Hier ist der Code:
HtmlAttributes.h
Code: Alles auswählen
#ifndef HTMLATTRIBUTES_H
#define HTMLATTRIBUTES_H
#include <QWidget>
#include <QWebElement>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QFormLayout>
class HtmlAttributes : public QWidget{
Q_OBJECT
private:
QFormLayout *layoutForm;
QList<QLabel*> labels;
QList<QLineEdit*> lineEdits;
QWebElement *webElement;
public:
HtmlAttributes(QWidget* parent = 0);
void createAttirbutes(QWebElement *web_element, QList<QString> html_attributes, QList<QString> html_commonAttributes);
public slots:
void setHtmlAttribute(QString, QString);
};
#endif // HTMLATTRIBUTES_H
HtmlAttributes.cpp
Code: Alles auswählen
#include "HtmlAttributes.h"
#include "AttributeItem.h"
#include <QMessageBox>
HtmlAttributes::HtmlAttributes(QWidget *parent) : QWidget(parent){
layoutForm = new QFormLayout;
}
void HtmlAttributes::createAttirbutes(QWebElement *web_element, QList<QString> html_attributes, QList<QString> html_commonAttributes){
//QMessageBox::information(this, "test", QVariant(findChildren<QLabel*>().size()).toString(), QMessageBox::Ok);
// Alles löschen
foreach(QLabel* label, findChildren<QLabel*>()){
if(label->close())
label->deleteLater();
}
foreach(QLineEdit* line_edit, findChildren<QLineEdit*>()){
if(line_edit->close())
line_edit->deleteLater();
}
// disconnect
// ...
if(layoutForm != NULL)
delete layoutForm;
layoutForm = new QFormLayout;
webElement = web_element;
// Tag Attribute erstellen
AttributeItem* itemTag = new AttributeItem("tag", web_element->tagName().toLower());
//connect(item, SIGNAL(setHtmlAttribute(QString,QString)), SLOT(setHtmlAttribute(QString,QString)));
layoutForm->addRow(itemTag->getLabel(), itemTag->getLineEdit());
// Allgemeine Attribute erstellen
AttributeItem* itemCommon = new AttributeItem("Allgemein");
layoutForm->addRow(itemCommon->getLabel());
for(qint32 i=0; i < html_commonAttributes.size(); ++i){
QString attributeName = html_commonAttributes.at(i);
QString attributeValue = web_element->attribute(attributeName, "");
AttributeItem* item = new AttributeItem(attributeName, attributeValue);
connect(item, SIGNAL(setHtmlAttribute(QString,QString)), SLOT(setHtmlAttribute(QString,QString)));
layoutForm->addRow(item->getLabel(), item->getLineEdit());
}
// Attribute erstellen
AttributeItem* itemAttribute = new AttributeItem("Attribute");
layoutForm->addRow(itemAttribute->getLabel());
for(qint32 i=0; i < html_attributes.size(); ++i){
QString attributeName = html_attributes.at(i);
QString attributeValue = web_element->attribute(attributeName, "");
AttributeItem* item = new AttributeItem(attributeName, attributeValue);
connect(item, SIGNAL(setHtmlAttribute(QString,QString)), SLOT(setHtmlAttribute(QString,QString)));
layoutForm->addRow(item->getLabel(), item->getLineEdit());
}
setLayout(layoutForm);
//setHtmlAttribute("id", "bla"); // direkt Aufruf: webElement ist nicht Null !?
}
void HtmlAttributes::setHtmlAttribute(QString _attribute_name, QString _attribute_value){
//QMessageBox::information(this, "test", _attribute_name +" "+ _attribute_value, QMessageBox::Ok);
if(webElement->isNull())
QMessageBox::information(this, "null", "", QMessageBox::Ok);
else{
QMessageBox::information(this, "test", "", QMessageBox::Ok);
webElement->setAttribute(_attribute_name, _attribute_value);
}
//QMessageBox::information(this, "test", webElement->tagName(), QMessageBox::Ok);
}
AttributeItem.h
Code: Alles auswählen
#ifndef ATTRIBUTEITEM_H
#define ATTRIBUTEITEM_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
class AttributeItem : public QWidget{
Q_OBJECT
private:
QLabel* label;
QLineEdit* lineEdit;
public:
AttributeItem(QString _label, QString _line_edit="", QWidget* parent=0);
QLabel *getLabel();
QLineEdit *getLineEdit();
public slots:
void lineEdited();
signals:
void setHtmlAttribute(QString, QString);
};
#endif // ATTRIBUTEITEM_H
AttributeItem.cpp
Code: Alles auswählen
#include "AttributeItem.h"
AttributeItem::AttributeItem(QString _label, QString _line_edit, QWidget* parent) : QWidget(parent){
label = new QLabel(_label);
lineEdit = new QLineEdit(_line_edit);
connect(lineEdit, SIGNAL(returnPressed()), SLOT(lineEdited()));
}
QLabel *AttributeItem::getLabel(){
return label;
}
QLineEdit *AttributeItem::getLineEdit(){
return lineEdit;
}
void AttributeItem::lineEdited(){
emit setHtmlAttribute(label->text(), lineEdit->text());
}
Und das bei QFormLayout die Anzahl der Reihen wächst finde ich irgendwie nicht gut. Nur bei 70 Klicks auf 20 Attribute-Element sind das 1400 Reihen und es wächst weiter (irgendwo wird doch Speicher belegt).
Deswegen lösche ich das Layout und erstelle es neu.