colorChanged = Signal() color = Property(QColor, getColor, setColor, notify=colorChanged) name = Property(str, getName, setName) chartCleared = Signal() @Slot() # This should be something like @Invokable def clearChart(self): self.setColor(Qt.transparent) self.update() self.chartCleared.emit() if __name__ == '__main__': app = QGuiApplication(sys.argv) qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart') view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) qmlFile = os.path.join(os.path.dirname(__file__), 'app.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)
self.load_from_json(filename) def load_from_json(self,filename): """Load list of cities from given file""" with open(filename,encoding="utf-8") as f: self.city_list = json.load(f) def rowCount(self, parent:QtCore.QModelIndex=...) -> int: """ Return number of cities in the list""" return len(self.city_list) def data(self, index:QtCore.QModelIndex, role:int=...) -> typing.Any: """ For given index and DisplayRole return name of the selected city""" # Return None if the index is not valid if not index.isValid(): return None # If the role is the DisplayRole, return name of the city if role == QtCore.Qt.DisplayRole: return self.city_list[index.row()]["muniLabel"] app = QGuiApplication(sys.argv) view = QQuickView() url = QUrl(VIEW_URL) citylist_model = CityListModel(CITY_LIST_FILE) ctxt = view.rootContext() ctxt.setContextProperty('cityListModel',citylist_model) view.setSource(url) view.show() app.exec_()
if role == Qt.DisplayRole: return d['name'] elif role == Qt.DecorationRole: return Qt.black elif role == PersonModel.MyRole: return d['myrole'] return None def populate(self): self._data.append({'name':'Qt', 'myrole':'role1'}) self._data.append({'name':'PySide', 'myrole':'role2'}) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) myModel = PersonModel() myModel.populate() view.rootContext().setContextProperty("myModel", myModel) qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) view.show() app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order.
############################################################################# import os import sys from PySide6.QtCore import QUrl from PySide6.QtGui import QGuiApplication from PySide6.QtQuick import QQuickView # This example uses a QML file to show a scrolling list containing # all the items listed in dataList. if __name__ == '__main__': dataList = ["Item 1", "Item 2", "Item 3", "Item 4"] app = QGuiApplication(sys.argv) view = QQuickView() ctxt = view.rootContext() ctxt.setContextProperty("myModel", dataList) qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) view.show() app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view
@Slot('double') def output(self, s): print(s) @Slot(str) def outputStr(self, s): print(s) @Slot('double') def outputFloat(self, x): print(x) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() # Instantiate the Python object. con = Console() # Expose the object to QML. context = view.rootContext() context.setContextProperty("con", con) #qml_file = os.fspath(Path(__file__).resolve().parent / 'qml_test2.qml') view.setSource(QUrl("Source/away_from_keyboard/qml_test2.qml")) if view.status() == QQuickView.Error: sys.exit(-1) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances
## ############################################################################# import os import sys from PySide6.QtCore import QTimer, QUrl from PySide6.QtGui import QGuiApplication from PySide6.QtQuick import QQuickView if __name__ == '__main__': app = QGuiApplication(sys.argv) timer = QTimer() timer.start(2000) view = QQuickView() qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) root = view.rootObject() timer.timeout.connect(root.updateRotater) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)
from PySide6.QtCore import QUrl from PySide6.QtGui import QGuiApplication from PySide6.QtQuick import QQuickView import sys VIEW_PATH = "view.qml" # Create the application object and pass command line arguments to it app = QGuiApplication(sys.argv) # Create the view object view = QQuickView() # Set the QML file to view view.setSource(QUrl(VIEW_PATH)) # Resize the view with the window view.setResizeMode(QQuickView.ResizeMode.SizeRootObjectToView) # Show the view (open the window) view.show() # Run the event loop app.exec_()
painter.drawRoundedRect(0, 0, itemSize.width(), itemSize.height() - 10, 10, 10) if self.rightAligned: points = [ QPointF(itemSize.width() - 10.0, itemSize.height() - 10.0), QPointF(itemSize.width() - 20.0, itemSize.height()), QPointF(itemSize.width() - 30.0, itemSize.height() - 10.0), ] else: points = [ QPointF(10.0, itemSize.height() - 10.0), QPointF(20.0, itemSize.height()), QPointF(30.0, itemSize.height() - 10.0), ] painter.drawConvexPolygon(points) if __name__ == "__main__": app = QApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) qmlRegisterType(TextBalloon, "TextBalloonPlugin", 1, 0, "TextBalloon") view.setSource(QUrl.fromLocalFile("main.qml")) if view.status() == QQuickView.Error: sys.exit(-1) view.show() sys.exit(app.exec_())
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)
def time_out(): global time_cnt time_cnt = time_cnt + 1 #print(time_cnt) label.send_text.emit(str(time_cnt)) if __name__ == '__main__': app = QGuiApplication(sys.argv) timer = QTimer() timer.start(1000) view = QQuickView() qml_file = "Source/away_from_keyboard/qml_test3.qml" view.setSource(QUrl.fromLocalFile(qml_file)) if view.status() == QQuickView.Error: sys.exit(-1) root = view.rootObject() timer.timeout.connect(root.updateRotater) #timer.timeout.connect(root.updateTime, text) timer.timeout.connect(time_out) label = PyLabel() label.send_text.connect(root.updateTime) con = Console()
# !/usr/bin/env python # encoding: utf-8 """ @author: gaogao @file: main_window.py @time: 2021/5/18 10:20 @desc: """ from PySide6.QtWidgets import QApplication from PySide6.QtQuick import QQuickView from PySide6.QtCore import QUrl app = QApplication([]) view = QQuickView() url = QUrl("view.qml") view.setSource(url) view.showMaximized() app.exec()
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ## ## $QT_END_LICENSE$ ## ############################################################################# """PySide6 port of the QML Polar Chart Example from Qt v5.x""" import sys import os from PySide6.QtQuick import QQuickView from PySide6.QtCore import Qt, QUrl from PySide6.QtWidgets import QApplication, QMainWindow if __name__ == '__main__': app = QApplication(sys.argv) viewer = QQuickView() viewer.engine().addImportPath(os.path.dirname(__file__)) viewer.engine().quit.connect(viewer.close) viewer.setTitle = "QML Polar Chart" qmlFile = os.path.join(os.path.dirname(__file__), 'main.qml') viewer.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) viewer.setResizeMode(QQuickView.SizeRootObjectToView) viewer.show() sys.exit(app.exec_())
import sys from PySide6.QtWidgets import QApplication from PySide6.QtQuick import QQuickView from PySide6.QtCore import QUrl if __name__ == '__main__': app = QApplication() view = QQuickView() url = QUrl("view.qml") view.setSource(url) view.show() sys.exit(app.exec_())