Making a search bar for browser?
-
Something like:
@
QUrl query(searchString); // from your QLineEdit
QUrl address("www.google.com/search?q=" + query.toPercentEncoded());
@Brain to terminal, check the docs if I haven't messed something up :)
-
this is what I have:
@void MainWindow::on_searchbar_returnPressed()
{
query(searchString);
ui->webView->setUrl(QUrl(QStringLiteral("www.google.com/search?q=" + query.toPercentEncoded());
}
@It gives me two errors:
'searchString' was not declared in this scope
'query' was not declared in this scope.The answer is probably obvious, but I can't figure it out.
-
You are using query without a type (add "QUrl" in front of that line). Instead of searchString you should inject the data that your user has put in your search line edit. I do not know the name of that variable in your application, so I've used a random substitute. For you it will probably be something like "ui->lineEdit->text()".
-
ok so i have updated it and it still doesn't work.
@void MainWindow::on_searchbar_returnPressed()
{
ui->lineEdit->searchString();
QUrl(query(searchString));
ui->webView->setUrl(QUrl(QStringLiteral("www.google.com/search?q=" + query.toPercentEncoded());
}@it says that QLineEdit has no member called searchString, query is not declared, and searchString is not declared. How do I declare these? I have no variable to store user input so how would I create it? (I thought ui->lineEdit->searchString would work). Thanks!
-
I think you really need to grab a book on C++.
-
Ok so Ive figured it out!
@void MainWindow::on_searchbar_returnPressed()
{
ui->webView->setUrl(QUrl(QString("http://www.google.com/?q=")+(ui->searchbar->text())));
}
@the only problem is that when I type something into the box, it takes me to google.com with fish in the search box. How can I make it take me straight to the google results? stupid question I know.
-
You have mostly helped yourself ;) You are welcome and happy further coding!