[solved] How to use a public Qwebelement variable
-
Hello, when I try to use the Qwebelement as a public variable, it doesn't work. For this reason I want to know what am I doing wrong?
This is my code:
Header
@private: QWebElement *element;@
Cpp
@
element = ui->webView->page()->currentFrame()->findFirstElement("#"+ElementID);@
However the compiler send me some errors and I am not sure how to solve that
-
The code is the next
header
@
private:
QWebElement *element;
@Cpp
@void HtmlEditor::on_webView_selectionChanged()
{
element = ui->webView->page()->currentFrame()->
findAllElements(":hover").last();
qDebug() << element.attribute("style")
<< element.attribute("id")
<< element.attributeNames() <<
element.classes()
;
ElementID = element.attribute("id");
ui->ElementIDlineEdit->setText(ElementID);
}@The errors are the next
@error: cannot convert 'QWebElement' to 'QWebElement*' in assignment@ -
Your problem is right there in the compiler error. You are trying to asign a QWebElement object (as returned by "QWebElementCollection::last()":http://qt-project.org/doc/qt-4.8/qwebelementcollection.html#last ) to a QWebElement pointer.
-
As KA51O and compiler said. You are trying to assign value to pointer type value.
You have to change your element pointer to object:
@private:
QWebElement element; // No more *@or change in your on_webView_selectionChanged() method:
@*element = ui->webView->page()->currentFrame()->
findAllElements(":hover").last(); // notice * in the front@In second case, because element is pointer, you need to change element. to element->
-
Thanks the second case works perfect, but I need to construct the variable first.
Other way would be like this
@void HtmlEditor::on_webView_selectionChanged()
{
element = new QWebElement(ui->webView->page()->currentFrame()->
findAllElements(":hover").last());
...
}@In this case I don't need a previus constructor.
The complete code would be the next
Header
@
private:
QWebElement *element;@Cpp
@void HtmlEditor::on_webView_selectionChanged()
{
element = new QWebElement(ui->webView->page()->currentFrame()->
findAllElements(":hover").last());
qDebug() << element->attribute("style")
<< element->attribute("id")
<< element->attributeNames() <<
element->classes()
;
ElementID = element->attribute("id");
ui->ElementIDlineEdit->setText(ElementID);
}@ -
How do I do that?