Exemple #1
0
def main():
    print(f'PySide6=={PySideVer} Qt=={QtVer}')

    parser = ArgumentParser(
        description='cappuccino: Simple image viewer with download')
    parser.add_argument('download_keyword',
                        nargs='?',
                        default='',
                        help='image keyword to download')
    args = parser.parse_args()

    download_keyword = args.download_keyword
    if not download_keyword and not exist_images():
        download_keyword = DEFAULT_KEYWORD

    initialize_qt()

    app = QGuiApplication(sys.argv)
    app.setWindowIcon(QIcon(resource_path('cappuccino.ico')))

    is_download = download_keyword != ''
    mmodel = MainModel(is_download, IMAGES_DIR_NAME)
    dmodel = DownloaderModel(download_keyword, IMAGES_DIR_NAME)
    imodel = ImageViewerModel(IMAGES_DIR_NAME)
    dmodel.download_completed.connect(mmodel.on_download_completed)

    engine = QQmlApplicationEngine()
    engine.rootContext().setContextProperty('mmodel', mmodel)
    engine.rootContext().setContextProperty('dmodel', dmodel)
    engine.rootContext().setContextProperty('imodel', imodel)
    engine.load(f'file:///{resource_path("qml/Main.qml")}')

    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())
Exemple #2
0
# https://stackoverflow.com/questions/57619227/connect-qml-signal-to-pyside2-slot
class SearchCommands(QObject):
    @Slot(str)
    def openUrl(self, searchTerm):
        # Send the user to Bing based on this SO answer:
        # https://stackoverflow.com/a/31715355
        # TODO: Only do the search if the searchTerm's length is more than 0.
        # Maybe it would be cool to hide the search button if there's nothing
        # in the search box, too.
        # TODO 2: Have the code to do a search be async so it doesn't look
        # choppy when the user presses the button.
        # Not sure how much memory it'll save, but using "".join()
        # instead of "+" for concatenation does prevent creating new strings
        # constantly.
        webbrowser.open("".join(["https://bing.com/search?q=", searchTerm]),
                        new=2)


if __name__ == "__main__":
    # Set the Universal style.
    sys.argv += ['--style', 'Universal']
    app = QGuiApplication(sys.argv)
    # Hook up some stuff so I can access the searchClass from QML.
    searchClass = SearchCommands()
    engine = QQmlApplicationEngine()
    engine.rootContext().setContextProperty("searchClass", searchClass)
    engine.load("MainWindow.qml")
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())
Exemple #3
0
    url = "http://country.io/names.json"
    response = urllib.request.urlopen(url)
    data = json.loads(response.read().decode('utf-8'))
    # print("data",data)
    # Format and sort the data
    data_list = list(data.values())
    # print("data_list", data_list)
    data_list.sort()

    # Set up the application window
    app = QGuiApplication(sys.argv)
    view = QQuickView()
    view.setResizeMode(QQuickView.SizeRootObjectToView)

    # Expose the list to the Qml code
    my_model = QStringListModel()
    my_model.setStringList(data_list)
    view.rootContext().setContextProperty("myModel", my_model)

    # Load the QML file
    qml_file = Path(__file__).parent / "main_view.qml"
    view.setSource(QUrl.fromLocalFile(os.fspath(qml_file.resolve())))

    # Show the window
    if view.status() == QQuickView.Error:
        sys.exit(-1)
    view.show()
    # execute and cleanup
    app.exec()
    del view
Exemple #4
0
    qmlfile = sys.argv[1] if len(
        sys.argv) > 1 else re.sub(r'^(.*)[.]py$', r'\1', sys.argv[0]) + '.qml'
    qmlRegisterType(FileIO, "FileIOPlugin", 1, 0, "FileIO")
    qmlRegisterType(syntaxhighlighter, "SyntaxHighlighter", 1, 0,
                    "SyntaxHighlighter")
    reinstance = Re()
    # if re.search(r'(?:\w+)?Window\s*{',open(qmlfile).read()):
    if re.search(r'^(?<!//)\s*(?:\w+)?Window\s*{\s*$',
                 open(qmlfile).read(),
                 flags=re.MULTILINE) and (
                     not re.search(r'/[*](?![*]/)*?\n\s*(?:\w+)?Window\s*{',
                                   open(qmlfile).read(),
                                   flags=re.DOTALL)
                     or re.search(r'[*]/(?!/[*])*?\n\s*(?:\w+)?Window\s*{',
                                  open(qmlfile).read(),
                                  flags=re.DOTALL)):
        print('applicationwindow->', qmlfile)
        engine = QQmlApplicationEngine()
        engine.rootContext().setContextProperty("re", reinstance)
        engine.load(qmlfile)
    else:
        print('qquickitem->', qmlfile)
        engine = QQuickView()
        engine.setResizeMode(QQuickView.SizeRootObjectToView)
        engine.rootContext().setContextProperty("re", reinstance)
        engine.setSource(qmlfile)
        engine.show()
    res = app.exec()
    del engine
    sys.exit(res)