[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



  • Can you provide a little more code (method were it happens and class) and errors thrown by compiler.

    It's impossible to help with given information.



  • 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);
    }@



  • I'm glad you got it working :) .
    The code I wrote was brain to terminal, so there might be a mistake or two.

    Please, mark thread as solved.

    Regards,
    Jake



  • How do I do that?



  • Just edit your first post and add [SOLVED] in front of the title.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.