Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QML plugin #216

Merged
merged 11 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/components/qmlexecutor/examples/simple_forwarder.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import QtQuick 2.0

/*
* This example receives a frame or signal modifies and transmitt it
*/

Item {
function frameReceived(frameId, framePayload) {
// Increment frame ID of received CAN
frameId += 1

// LogWindow is QPlainTextEdit widget exsposed to QML
// You can use it to append text or control its visibility
LogWindow.appendPlainText("Frame: " + frameId + " Forwarded")

// Transmit frame
//
// CANBusModel is an QObject exposed to QML
// You can use it to receive and transmit frames and signals
CANBusModel.sendFrame(frameId, framePayload)
}

function signalReceived(signalId, signalValue) {
// Increment signal value
signalValue += 1

// Transmit signal
CANBusModel.sendSignal(signalId, signalValue)
}

Component.onCompleted: {
// We have no GUI to display so LogWindow can be visible
LogWindow.setVisible(true)

// Connect QML functions to CANBusModel signals
CANBusModel.frameReceived.connect(frameReceived)
CANBusModel.signalReceived.connect(signalReceived)
}
}


27 changes: 18 additions & 9 deletions src/components/qmlexecutor/gui/qmlexecutorguiimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class QMLExecutorGuiImpl : public QMLExecutorGuiInt
constexpr int size = 1234;
_ui->splitter->setSizes(QList<int>({size,size}));

QObject::connect(_ui->quickWidget, &QQuickWidget::statusChanged, this, &QMLExecutorGuiImpl::handleStatusChange);
_ui->fileName->setReadOnly(true);

QObject::connect(_ui->editQMLButton, SIGNAL(clicked()), this, SLOT(editQML()));
QObject::connect(_ui->loadQMLButton, SIGNAL(clicked()), this, SLOT(browseAndOpenQML()));
QObject::connect(&_qmlUpdateCheckTimer, SIGNAL(timeout()), this, SLOT(checkQMLModification()));
Expand Down Expand Up @@ -96,27 +97,36 @@ public slots:
{
if(url.isValid() && !url.isEmpty())
{
_qmlUrl = url;
// The easiest way to reload QML seems to be creating new QQuickWidget
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadQML function is aware of that so no fuss is needed here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an expert here but I was getting a lot of warnings about NULL reference when reloading QML (although new QML was working properly...). It seems to me that clearComponentCache that was used to clear engine was not clearing context properties.

auto splitter = static_cast<QSplitter*>(_ui->quickWidget->parentWidget());
auto splitState = splitter->saveState();

_ui->editQMLButton->setEnabled(true);
delete _ui->quickWidget;
_ui->quickWidget = new QQuickWidget();
updateUIColor();
splitter->insertWidget(0, _ui->quickWidget);
splitter->restoreState(splitState);

log("Loading qml file: " + url.toLocalFile());
QObject::connect(_ui->quickWidget, &QQuickWidget::statusChanged, this, &QMLExecutorGuiImpl::handleStatusChange);

_ui->quickWidget->setSource(QUrl());
_ui->quickWidget->engine()->clearComponentCache();
_qmlUrl = url;

log("Loading qml file: " + url.toLocalFile());

assert(_CANBusModel != nullptr);

_ui->quickWidget->rootContext()->setContextProperty("CANBusModel", _CANBusModel);
_ui->quickWidget->rootContext()->setContextProperty("LogWindow", _ui->logWindow);

_ui->quickWidget->setSource(url);

_ui->fileName->setText(url.url());

_ui->fileName->setText(url.toLocalFile());

emit QMLLoaded(_qmlUrl);

startQMLFileModificationChecks();

_ui->editQMLButton->setEnabled(true);
}

}
Expand Down Expand Up @@ -161,7 +171,6 @@ public slots:
if(askForQMLReload())
{
loadQML(_qmlUrl);

}

startQMLFileModificationChecks();
Expand Down