跳到內容

QML and Qt Quick

Looking for The Bling Thing(tm)? Post here!
20.0k 主題 77.3k 貼文
  • Qt Quick 3D use transparent textures with Model

    Unsolved
    1
    0 評價
    1 貼文
    301 瀏覽
    尚無回覆
  • workerscript always recieve the first version of ListModel

    Unsolved
    1
    0 評價
    1 貼文
    119 瀏覽
    尚無回覆
  • Make stringlistmodel example workl with ListView not being the top level item

    Solved
    2
    0 評價
    2 貼文
    192 瀏覽
    H
    I managed to find out myself. Here is the code, in case others might want the same: C++ #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QStringList> #include <qqmlengine.h> #include <qqmlcontext.h> #include <qqml.h> #include <QtQuick/qquickitem.h> #include <QtQuick/qquickview.h> int main(int argc, char ** argv) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/view.qml")); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl) QCoreApplication::exit(-1);}, Qt::QueuedConnection); engine.load(url); // get the root object of the loaded QML QList<QObject*> root_objects(engine.rootObjects()); QStringList projectList = { "Project 1", "Project 2", "Project 3", "Project 4" }; const QObjectList& children(root_objects[0]->children()); if (!children.isEmpty()) { QList<QObject*> lists(root_objects[0]->findChildren<QObject*>("listViewProjects")); if (!lists.isEmpty()) lists.first()->setProperty("model", QVariant::fromValue(projectList)); } return app.exec(); } QML import QtQuick 2.4 import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Window 2.5 Window { id: window visible: true width: 640 height: 480 title: qsTr("ListView") ListView { id: listViewProjects objectName: "listViewProjects" anchors.fill: parent delegate: Item { id: project required property string modelData required property int index height: 30 width: parent.width Text { anchors.verticalCenter: parent.verticalCenter text: parent.modelData } MouseArea { anchors.fill: parent onClicked: listViewProjects.currentIndex = parent.index } } highlight: Rectangle { color: "lightsteelblue"; radius: 2 } focus: true } } It also shows how to use the index variable for setting the selection with the mouse: once a required property is declared (modelData), you must declare all properties you intend to use. This is not shown in any other example I could find. If you fail to do this, index is undefined.
  • Pop up Menu outside QtWindow

    Unsolved
    1
    0 評價
    1 貼文
    172 瀏覽
    尚無回覆
  • Different views QtCreator vs runtime

    Unsolved
    1
    0 評價
    1 貼文
    171 瀏覽
    尚無回覆
  • QML and SFML

    Unsolved
    1
    0 評價
    1 貼文
    417 瀏覽
    尚無回覆
  • 0 評價
    1 貼文
    987 瀏覽
    尚無回覆
  • qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.

    Unsolved
    2
    0 評價
    2 貼文
    331 瀏覽
    JonBJ
    @kayshika export QT_DEBUG_PLUGINS=1 and then run your app for diagnostic information. Probably need to apt-get some other library.
  • How do I move my map markers in realtime?

    Unsolved
    1
    0 評價
    1 貼文
    395 瀏覽
    尚無回覆
  • 此主題已被刪除!

    Unsolved
    1
    0 評價
    1 貼文
    4 瀏覽
    尚無回覆
  • ListView - how to highlight current item

    Solved
    4
    0 評價
    4 貼文
    3k 瀏覽
    P
    https://github.com/pawlosck/QMLClock/blob/aaf6c65e5c1cd775c65a510f72d11de339da3709/alarm_list_window.qml The most important things: Item change color if is selected or not. Selected Item is black and other is red color: ListView.isCurrentItem ? "black" : "red" Add MouseArea with onClicked into ListView to change current Item using mouse. MouseArea { anchors.fill: parent onClicked: setCurrentItem(mouseX, mouseY) } Full code of ListView ListView { spacing: 5 clip: true id: listviewID anchors.fill: parent width: parent.width height: parent.height/2 model: modelID delegate: delegatID MouseArea { anchors.fill: parent onClicked: setCurrentItem(mouseX, mouseY) } } Full code of delegate with Items Component { id: delegatID Rectangle { id: alarmItemDelegate width: listviewID.width height: 50 border.width: 1 radius: 5 color: ListView.isCurrentItem ? "grey" : "transparent" Item { anchors.fill: parent Text { id: remainingAl x: 5 anchors.top: parent.top text: "Remaining: " + model.remainingTime color: alarmItemDelegate.ListView.isCurrentItem ? "yellow" : "black" } Text { id: typeAl x: 5 anchors.verticalCenter: parent.verticalCenter text: "Type: " + model.typeAlarm color: alarmItemDelegate.ListView.isCurrentItem ? "yellow" : "black" } Text { id: messageAl x: 5 anchors.bottom: parent.bottom text: "message: " + model.messageAlarm color: alarmItemDelegate.ListView.isCurrentItem ? "yellow" : "black" } } } }
  • TextInput - how to restrict width of text to parent's width

    Solved
    6
    0 評價
    6 貼文
    930 瀏覽
    P
    Problem solved. I had to add option clip=true into TextInput. After that, text is not expanded beyond border of Rectangle. https://github.com/pawlosck/QMLClock/commits/0c2e21ddd26dd9b59130f858b399b7fd8de662d8 Rectangle { id: typeInfoBorder border.width: 1 // color: "green" Layout.fillWidth: true Layout.minimumWidth: 40 height: 20 TextInput { id: typeInfoLabel width: parent.width height: parent.height clip: true text: typeInfoString onTextChanged: { var index = 0 for (var alarm of list_of_alarms) { if (alarm.getTimerID() === listviewID.currentItem.getTimerID()) { alarm.setTypeOfAlarm(text) } } } } }
  • How to save current index of ComboBox for supported map types

    Solved
    3
    0 評價
    3 貼文
    423 瀏覽
    edipE
    Thank you @LeLev I found a solution before but your solution is much better.
  • 0 評價
    2 貼文
    1k 瀏覽
    T
    I've developed a hackish workaround - if you're frustrated by this issue too, this "fix" is ugly but might help in the short-term: I noticed that Qt Creator still recognizes the old qmlRegisterType function calls if they're invoked from main in a project. So naturally, I made a custom compiler step for qmake that generates a header file with all QML_ELEMENT-tagged classes registered in a function. First, make sure you've set up qmltypes in your project as usual - your QML_IMPORT_NAME and all that is set. Add to the bottom of MyProject.pro: type_registrar_unfucker.name = type_registrar_unfucker type_registrar_unfucker.input = TYPE_REGISTRAR_SOURCES type_registrar_unfucker.commands = python3 $$PWD/type_unfuck.py ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT} $$QML_IMPORT_NAME $$QML_IMPORT_MAJOR_VERSION type_registrar_unfucker.output = ${QMAKE_FILE_IN_BASE}.h type_registrar_unfucker.clean = ${QMAKE_FILE_IN_BASE}.h type_registrar_unfucker.variable_out = HEADERS QMAKE_EXTRA_COMPILERS += type_registrar_unfucker TYPE_REGISTRAR_SOURCES += $$OUT_PWD/myproject_metatypes.json Add as type_unfuck.py in your source directory: # type unfuck # ----------- # This python script deals with the issue of Qt Creator not being able to recognize element types # registered using 'QML_ELEMENT'. # # It does so by generating a header, 'myprojectname_metatypes.h'. # Include this header ONLY in main.cpp, and then call `registerTypes()` first thing in `main`. # Then, Qt Creator will stop whining. import sys import json source_path = sys.argv[1] dest_path = sys.argv[2] import_name = sys.argv[3] import_major = sys.argv[4] with open(source_path) as f: data = json.load(f) includes = ["QtQml"] types = [] for file in data: includes.append(file["inputFile"]) for type in file["classes"]: if "classInfos" in type and {"name":"QML.Element","value":"auto"} in type["classInfos"]: types.append(type["className"]) with open(dest_path, 'w') as f: for i in includes: f.write(f'#include <{i}>\n') f.write('void registerTypes() {\n') for t in types: f.write(f'\tqmlRegisterType<{t}>("{import_name}", {import_major}, 0, "{t}");\n') f.write('}\n') Then, just add #include <myproject_metatypes.h> to your project's main.cpp and invoke registerTypes(); immediately in your main function. Build your project, then click on Tools -> QML/JS -> Reset Code Model, and Qt Creator should now recognize your custom components correctly.
  • delete Item and load another on the same ApplicationWindow

    Unsolved
    1
    0 評價
    1 貼文
    132 瀏覽
    尚無回覆
  • 此主題已被刪除!

    Unsolved
    1
    0 評價
    1 貼文
    5 瀏覽
    尚無回覆
  • Loading qml file several times without closing

    Unsolved
    4
    0 評價
    4 貼文
    504 瀏覽
    ODБOïO
    So you want to open Chat.qml (window) multiple times ? You can't do it with the loader, it loads only one Component at the time try to create your windows dynamically
  • Using editable ComboBox

    Solved editablcombobox combobox
    4
    0 評價
    4 貼文
    1k 瀏覽
    K
    Thought initially that the issue could be completely solved in qml, but Settings seem to miss some features as present in QSettings. Therefore decided to move forward with solution based on C++ with QML. https://forum.qt.io/topic/120954/combobox-together-with-qstringlistmodel
  • ComboBox together with QStringListModel

    Solved qml c++ qml combobox
    4
    0 評價
    4 貼文
    3k 瀏覽
    K
    The only solution I found is with inheriting from QStringListModel. main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <QSettings> #include "MyStringListModel.h" #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); qmlRegisterType <MyStringListModel> ( "StringListModel", 1, 0, "MyStringListModel" ); MyStringListModel lstModel; QSettings settings ( "TestQml.ini", QSettings::IniFormat ); lstModel.readSettings(&settings, "Components"); if ( lstModel.stringList().size() < 1 ) { lstModel.setStringList( QStringList() << "Test 1" << "Test 2"); lstModel.setCurrentIndex(0); } qDebug() << lstModel.stringList(); engine.rootContext()->setContextProperty ("myLstModel", &lstModel ); engine.load(url); return app.exec(); } MyStringListModel.h #ifndef MYSTRINGLISTMODEL_H #define MYSTRINGLISTMODEL_H #include <QObject> #include <QStringListModel> class QSettings; class MyStringListModel : public QStringListModel { Q_OBJECT Q_PROPERTY (int CurrentIndex READ getCurrentIndex WRITE setCurrentIndex NOTIFY sigCurrentIndex ) int CurrentIndex; public: MyStringListModel(); int getCurrentIndex () const; void setCurrentIndex ( int indx ); void readSettings ( QSettings *settings, const QString & category ); void writeSettings ( QSettings *settings, const QString & category ) const; public slots: void addNewText ( QString editText ); void sltEditText ( QString editText ); signals: void sigCurrentIndex(); }; #endif // MYSTRINGLISTMODEL_H MyStringListModel.cpp #include "MyStringListModel.h" #include <QSettings> MyStringListModel::MyStringListModel() : CurrentIndex ( 0 ) { } int MyStringListModel::getCurrentIndex() const { return CurrentIndex; } void MyStringListModel::setCurrentIndex(int indx) { CurrentIndex = indx; } void MyStringListModel::addNewText(QString editText) { const QStringList &lst = stringList(); for ( int i = 0; i < lst.size(); ++i ) { if ( editText == lst[i] ) return; } setStringList ( stringList() << editText ); setCurrentIndex ( rowCount() - 1 ); } void MyStringListModel::readSettings(QSettings *settings, const QString &category) { settings->beginGroup(category); QStringList lst = settings->value("Entries", QStringList()).toStringList(); setStringList ( lst ); int currentIndex = settings->value("CurrentPosition", 0 ).toInt(); setCurrentIndex(currentIndex); settings->endGroup(); } void MyStringListModel::writeSettings(QSettings *settings, const QString &category) const { settings->beginGroup(category); settings->setValue("Entries", stringList()); settings->setValue("CurrentPosition", CurrentIndex ); settings->endGroup(); } void MyStringListModel::sltEditText(QString editText) { addNewText ( editText ); QSettings settings ( "TestQml.ini", QSettings::IniFormat ); writeSettings(&settings, "Components"); } main.qml import QtQuick 2.15 import QtQuick.Controls 2.15 import StringListModel 1.0 ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Scroll") Frame { ComboBox { id: secondComboBox editable: true model: myLstModel currentIndex: model.CurrentIndex textRole: "display" onAccepted: { if (find(editText) === -1) model.addNewText ( editText ) } } } }
  • Qt Quick 3D use 3D models with skeleton

    Unsolved qt quick 3d model skeleton
    1
    0 評價
    1 貼文
    493 瀏覽
    尚無回覆