Hello All, I'm working on a project that originally worked well in PySide. I can't use PySide in the Mini2440 using Qt Windows Server so I have to rewrite everything in C++.
Is it possible to run PySide on a QWS? If I have to code in C++, the problem I have is related to having QML update itself when changes are made to an object contained within an AbstractListModel. Everything seems to be in order. I've include emit commands to no avail. Using the console, I know for a fact that value do indeed change as they're suppose to. Here's where I am: There exists three relevant C++ classes, a main and a ListView object in the QML. VarListModel, revealed to the QML context, contains VarWrappers. Controller is used for the QML to update VarListModel items. Once again, the items' value do change, but QML does not notice this or updates. I hope this isn't related to the bugs people were seeing in 4.7 where they found that DataChanged(QIndex, QIndex) wasn't updating the QML, but that bug nearly perfectly describes my situation. Relevant Code is as follows. VarWrapper.h class VarWrapper : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged) Q_PROPERTY(bool checked READ isChecked NOTIFY checkedChanged) public: VarWrapper(QString name, QString value, QObject *parent=0); QString name() ; void setName( QString &name); QString value() ; void setValue( QString &value); bool isChecked(); void toggle_checked(); signals: void nameChanged(); void valueChanged(); void checkedChanged(); private: QString m_name; QString m_value; bool m_checked; }; VarWrapper.cpp VarWrapper::VarWrapper(QString name, QString value, QObject *parent) : QObject(parent), m_name(name), m_value(value), m_checked(false) { } QString VarWrapper::name() { return m_name; } void VarWrapper::setName( QString &name) { if (name != m_name) { m_name = name; emit nameChanged(); } } QString VarWrapper::value() { return m_value; } void VarWrapper::setValue( QString &value) { if (value != m_value) { m_value = value; emit valueChanged(); } } bool VarWrapper::isChecked() { return m_checked; } void VarWrapper::toggle_checked() { m_checked = !m_checked; emit checkedChanged(); return; } VarListModel.h class VarListModel : public QAbstractListModel { Q_OBJECT public: enum VarRoles { NameRole = Qt::UserRole + 1, ValueRole, CheckedRole }; VarListModel(QObject *parent = 0); void addVariable(VarWrapper *varr); int rowCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; // Q_INVOKABLE void toggleChecked(int index); VarWrapper * get(int index); private: QList<VarWrapper*> m_vars; }; VarListModel::VarListModel(QObject *parent) : QAbstractListModel(parent) { QHash<int, QByteArray> roles; roles[NameRole] = "name"; roles[ValueRole] = "value"; roles[CheckedRole] = "checked"; setRoleNames(roles); } void VarListModel::addVariable(VarWrapper *varr) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_vars.append(varr); endInsertRows(); } int VarListModel::rowCount(const QModelIndex &parent) const { return m_vars.count(); } QVariant VarListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() < 0 || index.row() > m_vars.count()) return QVariant(); VarWrapper * variable = m_vars[index.row()]; if (role == NameRole) return variable->name(); else if (role == ValueRole) return variable ->value(); else if (role == CheckedRole) return variable->isChecked(); return QVariant(); } VarWrapper * VarListModel::get(int index) { VarWrapper * variable = m_vars[index]; return variable; } Controller.h class Controller : public QObject { Q_OBJECT public: Controller(VarListModel * model, QObject *parent=0); Q_INVOKABLE void test(); Q_INVOKABLE void toggle(int index); private: VarListModel * m_model; }; Controller.cpp Controller::Controller(VarListModel *model, QObject *parent) : QObject(parent), m_model(model) { } Q_INVOKABLE void Controller::test() { cout << "Test Worked!" << endl; return; } Q_INVOKABLE void Controller::toggle(int index) { VarWrapper * var = m_model->get(index); var->toggle_checked(); return; } main.cpp Q_DECL_EXPORT int main(int argc, char *argv[]) { QApplication app(argc, argv); QFile file("../Redo_4/in.txt"); VarListModel model; if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { cout << "uh oh!" << endl; } else { while (!file.atEnd()) { QString line = QString(file.readLine()); cout << line.toStdString(); QStringList linelist = line.split(','); model.addVariable(new VarWrapper(linelist.at(0),linelist.at (1))); } } Controller * controller = new Controller(&model); QDeclarativeView view; QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("cppListModel", &model); ctxt->setContextProperty("controller", controller); view.setSource(QUrl("qml/Redo_2/main.qml")); view.show(); return app.exec(); } main.qml ListView { id: pythonList anchors.fill:parent clip: true boundsBehavior: Flickable.StopAtBounds delegate: Component { Rectangle { id: wholerow width: pythonList.width height: 50 color: (index%2?"#faf8f8":"#c9d0d0") Rectangle { // checkbox for display on chart id: checkboxsquare height: 50 width: 50 anchors { verticalCenter: parent.verticalCenter left: parent.left } color: model.checked?"black":parent.color Text { id: checkbox text: "✤" font.pixelSize: wholerow.height font.bold: true visible: model.checked color: model.checked?"white":parent.color anchors { verticalCenter: parent.verticalCenter horizontalCenter: parent.horizontalCenter } } MouseArea { anchors.fill: parent onClicked: // item is toggled for display and made relevant // the chart is refreshed // the name of tab3 is changed to this variable // single variable pane set as model { // controller.toggled(pythonListModel, model.varItem) // aPieChart.test(model.varItem.name) tabtext3.text = model.name controller.toggle(index); } } } Text { text : model.name anchors.verticalCenter: parent.verticalCenter anchors.left: checkboxsquare.right anchors.rightMargin: 20 } Text { x: 200 text : model.value anchors.verticalCenter: parent.verticalCenter } } } } Up above, when I click on a box on the left hand side, the QML passes the relevant VarWrapper object to the Controller which indeed changes its value. The QML does not update to reflect this. Thank you all for your consideration. Ryan Gomez
_______________________________________________ Qt-qml mailing list Qt-qml@qt.nokia.com http://lists.qt.nokia.com/mailman/listinfo/qt-qml