Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Making a search bar for browser?
Forum Updated to NodeBB v4.3 + New Features

Making a search bar for browser?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 2 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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

      (Z(:^

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicky j
        wrote on last edited by
        #3

        Thanks sierdzo!
        How would I modify the url? would it be something like:
        QUrl("www.google.com/search?q+UserInput")?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Yes. You just need to convert it to percent encoding (see QUrl API).

          (Z(:^

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nicky j
            wrote on last edited by
            #5

            would it be the ToPercentEncoded? If so where would i put this? Thanks

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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 :)

              (Z(:^

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nicky j
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nicky j
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    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()".

                    (Z(:^

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nicky j
                      wrote on last edited by
                      #10

                      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!

                      1 Reply Last reply
                      0
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #11

                        I think you really need to grab a book on C++.

                        (Z(:^

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nicky j
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            nicky j
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              nicky j
                              wrote on last edited by
                              #14

                              never mind, got it to work. Thank you so much for your help! I really appreciate it!

                              1 Reply Last reply
                              0
                              • sierdzioS Offline
                                sierdzioS Offline
                                sierdzio
                                Moderators
                                wrote on last edited by
                                #15

                                You have mostly helped yourself ;) You are welcome and happy further coding!

                                (Z(:^

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  nicky j
                                  wrote on last edited by
                                  #16

                                  haha well sometimes just thinking about it helps

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved