[Solved] WebEngineView->findText()



  • I am using QWebEngineView to display the .html page.

    I try to use findText to find where the search text is found on the loaded page.

    bool textFound;
           findText(QStringLiteral("Experience"), QWebEnginePage::FindFlags(), [this](bool found) { textFound = found;});
           if(textFound)
           {
                 // do my processing
           }
    

    When I execute this code, getting error as below
    error: expected expression
    findText(QStringLiteral("Experience"), QWebEnginePage::FindFlags(), [this](bool found) { qDebug()<<"inside";});
    ^

    Kindly help me resolve this issue.


  • Moderators

    Hi @ejos,
    You will have to use C++11 to use lambdas. Add CONFIG += c++11 in your .pro file and recompile.


  • Moderators

    @ejos If that is done, you will need to access the outer function's variable textFound by reference. You can specify that in capture list. Also immediately accessing textFound after findText may lead to it's initial value. Better to call a function here instead of setting it in lambda function. So you will to change to following:

    findText(QStringLiteral("Experience"), QWebEnginePage::FindFlags(), 
    [=](bool found) {  foundText(found); }
    );
    //foundText = function with a boolean parameter
    

    Or if you go by your original way

    bool textFound ;
    findText(QStringLiteral("Experience"), QWebEnginePage::FindFlags(), 
    [&](bool found) { textFound = found; qDebug() << textFound; } );
    qDebug() << textFound; //No guarantee outside.
    
    

    Better use the first way.



  • @p3c0 said:

    findText(QStringLiteral("Experience"), QWebEnginePage::FindFlags(),
    [=](bool found) { foundText(found); }
    );

    @p3c0 : thanx a lot.. I worked exactly as expected. Used the 1st method


  • Moderators

    @ejos Glad that it worked :). Mark the post as solved if done.


Log in to reply
 

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