Making a search bar for browser?
-
Hello!
I am new to Qt and programming in general.Ok, so basically I have been working on my own homemade browser, and I have encountered a snag. I have googled and searched all over, but I can’t really find a good answer.
The problem is that I am trying to make a search bar for my browser. When I say ‘search bar’ I mean a bar that you can type into that will google your query. I already have a working address bar where I can search URLs, but navigating the internet via URL is a pain! I want a bar where I can type ‘cat’ and it will google the term, rather than have to type in ‘www.wikipedia/cat.com’Would someone please explain to me how I could go about making a search bar? If there is something wrong with my post, please let me know. Im trying to learn. Thanks in advance!
-
Just as you are opening the URL in webKit right now, you just need to implement it so that the url is modified to use google. So, if the user writes 'cat', you should pass this url to your webView: www.google.com/search?q=cat
-
Thanks sierdzo!
How would I modify the url? would it be something like:
QUrl("www.google.com/search?q+UserInput")? -
Yes. You just need to convert it to percent encoding (see QUrl API).
-
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!