Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Making a search bar for browser?
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
How would I modify the url? would it be something like:
Yes. You just need to convert it to percent encoding (see QUrl API).
would it be the ToPercentEncoded? If so where would i put this? Thanks
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 :)
Hey thanks a lot sierdzo. I probably seem like a total noob at Qt and ask stupid questions, but I am trying to learn. Thanks!
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.
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++.
Haha this is true. I understand the basics pretty well, and I have been able to make command-line programs, but I find Qt foreign.
Ok so Ive figured it out!
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.
never mind, got it to work. Thank you so much for your help! I really appreciate it!
You have mostly helped yourself ;) You are welcome and happy further coding!
haha well sometimes just thinking about it helps