相关疑难解决方法(0)

如何使用QML模型?

我有一个用qml和c ++编写的GUI.有2个组合框(qt control 5.1).只要第一个组合的值发生变化,第二个组合框就必须在运行时更新.

maincontext->setContextProperty("typemodel", QVariant::fromValue(m_typemodel));

maincontext->setContextProperty("unitmodel", QVariant::fromValue(m_unitmodel));
Run Code Online (Sandbox Code Playgroud)

这是我从c ++给qml的两个模型.

ComboBox {
    id: typebox

    anchors.left: text1.right
    anchors.leftMargin: 5
    signal changed(string newtext)

    width: 70
    height: 23
    anchors.top: parent.top
    anchors.topMargin: 37
    model: typemodel

    onCurrentTextChanged: {

        mainwin.unitGenerator(typebox.currentText);

    }
Run Code Online (Sandbox Code Playgroud)

这是第一个组合框.如您所见,每次更改第一个组合框的值时,第二个组合框的c ++模型都会更新(mainwin.unitGenerator(typebox.currentText)).但它似乎没有更新组合框的模型.

如何在运行时更新qml的模型?

c++ qt combobox qml

13
推荐指数
1
解决办法
3万
查看次数

从QAbstractListModel中删除行

我有一个自定义模型,它源自QAbstractListModel,它暴露给QML.我需要支持添加新项目和删除现有项目的操作.虽然插入操作没有任何问题,但删除操作会导致应用程序在调用endRemoveRows()函数时崩溃.

    void GPageModel::addNewPage()
    {
        if(m_pageList.count()<9)
        {
            beginInsertRows(QModelIndex(),rowCount(),rowCount());
            GPage * page = new GPage();
            QQmlEngine::setObjectOwnership(page,QQmlEngine::CppOwnership);
            page->setParent(this);
            page->setNumber(m_pageList.count());
            page->setName("Page " + QString::number(m_pageList.count()+1));
            m_pageList.append(page);
            endInsertRows();
        }
    }

    void GPageModel::removePage(const int index)
    {
        if(index>=0 && index<m_pageList.count())
        {        
            beginRemoveRows(QModelIndex(),index,index);
            qDebug()<<QString("beginRemoveRows(QModelIndex(),%1,%1)").arg(index);
            GPage * page = m_pageList.at(index);        
            m_pageList.removeAt(index);
            delete page;
            endRemoveRows();
        }
    }
Run Code Online (Sandbox Code Playgroud)

GPage类派生自QObject.我很想知道在尝试调用endRemoveRows()时导致应用程序崩溃的原因.当调用endRemoveRows()时,我在"QList :: at:"索引超出范围""时得到"ASSERT失败."如何从QAbstracListModel中删除行?还有其他方法吗?

我在Windows 7 64位计算机上使用Qt 5.1.0.

qt qabstractitemmodel qml qabstractlistmodel qtquick2

6
推荐指数
1
解决办法
9978
查看次数

带有QAbstractItemModel和上下文菜单的Qml 2.0 TableView

我有TableView连接的Qml 2.0控件QAbstractItemModel.我想创建一个可以更改属性的上下文菜单,或者只是从模型中调用具体对象的方法.

例:

QAbstractItemModel有一个std::vector<Person>.Person有一个方法alter()可以做一些改变(任何改变,哪些改变都无关紧要,重点是我们能够调用该方法).

右键单击该行时,菜单会显示一个项目Alter.

我能找到的就是如何制作菜单.

  rowDelegate: Item {
    Menu {
      id: myContextMenu
      MenuItem {text: "Alter"; onTriggered: {} }
    }
    MouseArea {
      id: longPressArea
      anchors.fill: parent
      acceptedButtons: Qt.LeftButton | Qt.RightButton
      onClicked: {
        if (mouse.button == Qt.RightButton)
          myContextMenu.popup()
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

但我仍然不知道如何将菜单与行的确切对象连接起来.

c++ qt menu qml qt5

1
推荐指数
1
解决办法
7790
查看次数

标签 统计

qml ×3

qt ×3

c++ ×2

combobox ×1

menu ×1

qabstractitemmodel ×1

qabstractlistmodel ×1

qt5 ×1

qtquick2 ×1