Skip to content

QML and Qt Quick

Looking for The Bling Thing(tm)? Post here!
20.1k Topics 77.8k Posts
  • Virtual keyboard embedded in page

    Unsolved
    1
    0 Votes
    1 Posts
    151 Views
    No one has replied
  • Getting environment variables in QML

    Solved
    5
    0 Votes
    5 Posts
    4k Views
    M
    @kindid Thank you this is a good idea.
  • Row Selection and Image Cells with QML TableView & Qt 5.12.11 & Qt Quick Controls 2

    Unsolved
    3
    0 Votes
    3 Posts
    1k Views
    J
    I was able to roll my own selection. The logic needed was simple since I only needed to support the selection of a single row. My ModelItem looks like: struct ModelItem { Q_GADGET Q_PROPERTY( QString population MEMBER population ) Q_PROPERTY( int averageAge MEMBER averageAge ) Q_PROPERTY( bool selected MEMBER selected ) public: enum class Role { Selection = Qt::UserRole, ColumnType, ColorValue }; Q_ENUM(Role) QString population; int averageAge; bool selected { false }; bool operator!=( const ModelItem& other ) { return other.population != this->population || other.averageAge != this->averageAge; } }; The key points here are the selected property to hold whether or not an item is selected and the definition of the custom Selection role. I needed a roleNames function for my custom role QHash<int, QByteArray> ModelList:: roleNames() const { return { { Qt::DisplayRole, "display" }, { int( ModelItem::Role::Selection ), "selected" }, { int( ModelItem::Role::ColumnType ), "type" }, { int( ModelItem::Role::ColorValue ), "colorValue" } }; } The key here is that I use the string "selected" to refer to my custom Selection role. My data function looks like: QVariant ModelList:: data( const QModelIndex& index, int role ) const { const ModelItem modelItem = mList.at( index.row() ); QVariant result = QVariant(); if ( role == Qt::DisplayRole ) { if ( index.column() == 0 ) { result = QVariant( QString( modelItem.population ) ); } else { result = QVariant( QString::number( modelItem.averageAge ) ); } } if ( role == int( ModelItem::Role::Selection ) ) { result = QVariant( QString( modelItem.selected ? "#eeeeee" : "white" ) ); } if ( role == int( ModelItem::Role::ColumnType ) ) { if ( index.column() == 0 ) result = QVariant( QString( "stringValue" ) ); else result = QVariant( QString( "colorValue" ) ); } if ( role == int( ModelItem::Role::ColorValue ) ) { QString color; if ( modelItem.averageAge < 13 ) color = "red"; else if ( modelItem.averageAge < 35 ) color = "yellow"; else color = "green"; result = QVariant( color ); } qDebug() << role << " " << result; return result; } The key here is to check to see if the role is the custom Selection role and then return a color based on the value of selected in the modelItem. The final piece is to have the QML use this custom role: delegate: DelegateChooser { role: "type" DelegateChoice { roleValue: "colorValue" delegate: Rectangle { color: selected Rectangle { color: colorValue width: parent.height height: parent.height radius: width * 0.5; anchors.horizontalCenter: parent.horizontalCenter; } MouseArea { anchors.fill: parent onClicked: { var idx = Backend.modelResults.list.index( row, column ) console.log( "Clicked cell: ", idx.row, " ", Backend.modelResults.list.data( idx ) ) Backend.modelResults.list.select( idx.row ); } } } } DelegateChoice { delegate: Rectangle { color: selected Text { text: display anchors.fill: parent anchors.margins: 10 color: 'black' font.pixelSize: 15 verticalAlignment: Text.AlignVCenter } MouseArea { anchors.fill: parent onClicked: { var idx = Backend.modelResults.list.index( row, column ) console.log( "Clicked cell: ", idx.row, " ", Backend.modelResults.list.data( idx ) ) Backend.modelResults.list.select( idx.row ); } } } } } The key here is the color: selected part of the Rectangle for the delegate of each DelegateChoice. selected refers to the string selected I setup in the roleNames function above. The system knows to call the data function with the correct ModelItem::Role so if ( role == int( ModelItem::Role::Selection ) ) resolves to true. For each DelegateChoice, I defined a MouseArea for the cell which calls the select function in the model. The select function is: void ModelList:: select( int index ) { beginResetModel(); for ( int x = 0; x < this->mList.length(); x++ ) { this->mList[x].selected = ( x == index ); } endResetModel(); } The begin/endResetModel causes the table to be redrawn when the selection changes. The example project has been updated. Suggested improvement to this solution are welcome.
  • Implement asynchronous QAbstractItemModel

    Locked Unsolved
    3
    0 Votes
    3 Posts
    524 Views
    SGaistS
    Duplicate here Closing this one
  • Hand icon for scrollbar

    Unsolved
    4
    0 Votes
    4 Posts
    322 Views
    SGaistS
    Please show your actual code. This stack overflow thread may also help.
  • Unable to find the platform plugin error when run in release mode

    Unsolved
    2
    0 Votes
    2 Posts
    492 Views
    SGaistS
    Hi and welcome to devnet, Can you start your application with the QT_DEBUG_PLUGINS environment set to 1 ? That should give your more information about what is going on with the platform plugin.
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    20 Views
    No one has replied
  • using alignment enum in an array

    Solved
    5
    0 Votes
    5 Posts
    605 Views
    mzimmersM
    Right you are (about var, not the dum-dum part). It works fine now. Thanks! I also changed my variant (in the first example) to var, as I discovered today that variant is obsolete. FWIW...
  • change property of delegate by function

    Unsolved
    1
    0 Votes
    1 Posts
    176 Views
    No one has replied
  • A problem that I can't call some functions declared in QtObject of qml

    Unsolved
    2
    1 Votes
    2 Posts
    214 Views
    R
    I met this problem too,bro.Has you figure this out yet?
  • how to paint(draw?) 3d point on 3d grid

    Solved
    3
    0 Votes
    3 Posts
    483 Views
    D
    Thanks to reply! I solved that refered to this.
  • Controlling a main.qml's Loader from a 'children' qml files

    Unsolved
    3
    0 Votes
    3 Posts
    361 Views
    N
    @sierdzio Thank you for your reply - I really appreciate it. It will take some time to digest your instructions as I am very new to qt and qml - I have to read about signals and how they work. However, just yesterday I got it working by creating a property of Loader in the main qml file which refers to the view I want to load other views and then I am calling that property in the qml file that is loaded from the tabs qml file.
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    8 Views
    No one has replied
  • "QML_ELEMENT does not name a type" in Qt 5.15

    Solved 5.15
    8
    0 Votes
    8 Posts
    8k Views
    ChabaC
    I tried to build OpenGL Under QML example (https://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html). I get same error with version 5.14.1, but it works fine with version 5.15.2.
  • Is there a way to forward a signal so that it assigns a Q_PROPERTY value automatically?

    Unsolved
    5
    0 Votes
    5 Posts
    1k Views
    GrecKoG
    It's a macro from an external lib (originateing from this : http://gitlab.unique-conception.org/qt-qml-tricks/qt-supermacros ), roughly expanding to this: private: Q_PROPERTY(int propertyName READ propertyName NOTIFY propertyNameChanged) int m_propertyName; public: int propertyName() { return m_propertyName; } void update_propertyName(int propertyName) { if (m_propertyName == propertyName) return; m_propertyName = propertyName; Q_EMIT propertyNameChanged(); } Q_SIGNAL propertyNameChanged();
  • Update Q_PROPERTY: how does the notify signal work?

    Solved
    14
    0 Votes
    14 Posts
    13k Views
    GrecKoG
    And the QML engine doesn't event read the parameter of the notify signal if there's one.
  • 0 Votes
    1 Posts
    424 Views
    No one has replied
  • 0 Votes
    1 Posts
    273 Views
    No one has replied
  • Dial-style progress bars

    Solved
    13
    0 Votes
    13 Posts
    5k Views
    mzimmersM
    So, I'm trying to draw (lightly) from the github example posted above, and having trouble with what he calls the "handle" (the highlighted circle within the arc). I'll worry about calculating the correct position later, but for now...how do I go rotating this around the center of the parent? I won't include the code (way too lengthy) but this is what I'm getting at various rotation points. It's clearly rotating around the lower right corner. How can I get it to rotate around the center of the window? Scratch that question...I just played a little with Arun's stuff, and it's way better than anything I could do myself. Thanks...
  • How should I set up FirstPersonCameraController?

    Solved
    2
    0 Votes
    2 Posts
    301 Views
    Anton1978A
    Everything is working. In short, I was sure that if I clicked on Scene3D with the mouse, it was immediately in focus. It turns out that something is wrong, you need to give its focus programmatically. Scene3D { id: scene3d anchors.fill: parent anchors.leftMargin: 200 anchors.topMargin: 10 anchors.rightMargin: 10 anchors.bottomMargin: 10 focus: true aspects: ["input", "logic"] cameraAspectRatioMode: Scene3D.AutomaticAspectRatio Mega3DScene{} }