Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:23:15+00:00 2026-06-16T04:23:15+00:00

I am following the approach given on http://qt-project.org/wiki/Drag_and_Drop_within_a_GridView . I have the presented program.

  • 0

I am following the approach given on http://qt-project.org/wiki/Drag_and_Drop_within_a_GridView .

I have the presented program. Drag and drop is working, but after the drop I get
empty space in my grid. How can I improve this?

Moreover, I have also noticed that function indexAt() in line 46 not working correctly during the drag operation. Thus I had to add explicit calculation of gridArea.index.

(EDITED) I seems to me that the empty space appears because after droping the element it is returned to its original position (it is below the visible element). GridView is not updated and finish in an irregular state.

import QtQuick 1.1

GridView {
    id: mainGrid
    cellWidth: 165; cellHeight: 95
    width: 5*cellWidth; height: 4*cellHeight
    model: myModel
    delegate: myButton

    ListModel {
        id: myModel
        function createModel() {
            for (var i=1; i<=20; i++) {
                myModel.append({"display": i, "uid": i})
            }
        }
        Component.onCompleted: {createModel()}
    }

    Component {
        id: myButton
        Rectangle {
            id: item
            width: mainGrid.cellWidth-5; height: mainGrid.cellHeight-5;
            border.width: 1
            property int uid: (index >= 0) ? myModel.get(index).uid : -1
            Text {
                anchors.centerIn: parent
                text: display
                font.pixelSize: 48
            }
            states: [
                State {
                    name: "active"; when: gridArea.activeId == item.uid
                    PropertyChanges {target: item; x: gridArea.mouseX-80; y: gridArea.mouseY-45; z: 10; smooth: false}
                }
            ]
        }
    }

    MouseArea {
        id: gridArea
        anchors.fill: parent
        hoverEnabled: true
        preventStealing : true
        //property int index: mainGrid.indexAt(mouseX, mouseY) //WHY IS THIS NOT WORKING RELIABLE?
        property int mX: (mouseX < 0) ? 0 : ((mouseX < mainGrid.width - mainGrid.cellWidth) ? mouseX : mainGrid.width - mainGrid.cellWidth)
        property int mY: (mouseY < 0) ? 0 : ((mouseY < mainGrid.height - mainGrid.cellHeight) ? mouseY : mainGrid.height - mainGrid.cellHeight)
        property int index: parseInt(mX/mainGrid.cellWidth) + 5*parseInt(mY/mainGrid.cellHeight)  //item underneath cursor
        property int activeId: -1 //uid of active item
        property int activeIndex //current position of active item
        onPressAndHold: {
            activeId = mainGrid.model.get(activeIndex=index).uid
        }
        onReleased: {
            activeId = -1
        }
        onPositionChanged: {
            if (activeId != -1 && index != -1 && index != activeIndex) {
                mainGrid.model.move(activeIndex, activeIndex = index, 1)
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:23:16+00:00Added an answer on June 16, 2026 at 4:23 am

    Here, I give the working code. I have added additional hiearchy and put Rectangle into Item. Moreover, I had to reparent Rectangle. There are many similar solutions available,
    e.g. here.

    In the given solution, there is no problem with function indexAt() and thus explicit calculation of gridArea.index is not needed anymore.

    I will appreciate any comment on this solution and why these changes are so important.
    I still think that the original solution is intuitive and I do not understand why it is
    not working.

    import QtQuick 1.1
    
    GridView {
        id: mainGrid
        cellWidth: 165; cellHeight: 95
        width: 5*cellWidth; height: 4*cellHeight
        model: myModel
        delegate: myButton
    
        ListModel {
            id: myModel
            function createModel() {
                for (var i=1; i<=20; i++) {
                    myModel.append({"display": i, "uid": i})
                }
            }
            Component.onCompleted: {createModel()}
        }
    
        Component {
            id: myButton
            Item {
                id: item
                width: mainGrid.cellWidth-5; height: mainGrid.cellHeight-5;
                Rectangle {
                    id: box
                    parent: mainGrid
                    x: item.x; y: item.y;
                    width: item.width; height: item.height;
                    border.width: 1
                    property int uid: (index >= 0) ? myModel.get(index).uid : -1
                    Text {
                        anchors.centerIn: parent
                        text: display
                        font.pixelSize: 48
                    }
                    states: [
                        State {
                            name: "active"; when: gridArea.activeId == box.uid
                            PropertyChanges {target: box; x: gridArea.mouseX-80; y: gridArea.mouseY-45; z: 10}
                        }
                    ]
                }
            }
        }
    
        MouseArea {
            id: gridArea
            anchors.fill: parent
            hoverEnabled: true
            preventStealing : true
            property int index: mainGrid.indexAt(mouseX, mouseY) //item underneath cursor
            property int activeId: -1 //uid of active item
            property int activeIndex //current position of active item
    
            onPressAndHold: {
                activeId = mainGrid.model.get(activeIndex=index).uid
            }
            onReleased: {
                activeId = -1
            }
            onPositionChanged: {
                if (activeId != -1 && index != -1 && index != activeIndex) {
                    mainGrid.model.move(activeIndex, activeIndex = index, 1)
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched around but have been unsuccessful in determining whether the following approach
In my project I am using the following approach to querying data from the
I'm currently working on incorporating an authorization feature for Twitter following the approach described
I have the following to solve and I'm not sure how to approach this:
I'm using the Code First approach and have the following Model: public class Person
I am working on a SharePoint 2010 Server and i have following items in
I got my Apache Mod_WSGI and Django working following http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/ with a twist that
I am trying to parse review from this page: http://www.amazon.co.uk/product-reviews/B00143ZBHY Using following approach: Code
Given the following images, I'm really not sure how best to approach this issue.
I have been following the guidance given on this other post: Assigning an IronPython

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.