aboutsummaryrefslogtreecommitdiffstats
path: root/examples/network/googlesuggest/searchbox.py
blob: 9cbe20b23122bef7afa5f931ebc810ae74fc51e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

from PySide6.QtCore import Slot
from PySide6.QtGui import QDesktopServices
from PySide6.QtWidgets import QLineEdit

from googlesuggest import GSuggestCompletion


class SearchBox(QLineEdit):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.completer = GSuggestCompletion(self)

        self.returnPressed.connect(self.do_search)
        self.setWindowTitle("Search with Google")

        self.adjustSize()
        self.resize(400, self.height())
        self.setFocus()

    @Slot()
    def do_search(self):
        self.completer.prevent_suggest()
        url = f"https://www.google.com/search?q={self.text()}"
        QDesktopServices.openUrl(url)