Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Проблема с connect() из-за аргументов
-
Всем привет!
Получилось так, что программа создаётся через QtQuick Controls 2.0 и на форме есть одна кнопка, текстовое поле и слайдер.
По нажатию на кнопку, я передаю данные в класс С++ таким вот образом:onClicked: lControl.lightOn(txt_addressLight.text, lightSlider.value)
где lControl - это экземпляр класса (объявлен в main.cpp, кто связывал qml + c++ меня поймут),
lightOn - функция, которая принимает параметры QString и int
ну и txt_addressLight.text, lightSlider.value собственно и есть текстовым полем и слайдером, значения которых должны попадать в функцию.В принципе всё хорошо. Я на кнопку нажимаю и данные передаются. Только есть одно но, а именно: существует некий таймер, который раз в секунду обновляет слот lightOn (просто в этом слоте происходит передача по СОМ порту и он раз в секунду должен передавать эти параметры).
Вот такой connect() я создаю:
connect(timerFor_LightON, SIGNAL(timeout()), this, SLOT(lightOn(QString,int)));
где timerFor_LightON и есть та переменная, которой задаётся интервал времени и команда start();
А вот и ошибка, когда я компилирую:
QObject::connect: Incompatible sender/receiver arguments QTimer::timeout() --> LightControl::lightOn(QString,int)
Что можете посоветовать, товарищи знающие?
-
Sorry, but I still did not understand your decision and for this, I did another
In QML:
Button { id: btn_lightOn x: 0 y: 55 width: 199 height: 48 text: qsTr("Включить освещение") //onClicked: lControl.lightOn(txt_addressLight.text, lightSlider.value) onClicked: { timer.running = true } } Timer { id: timer interval: 1000; running: false; repeat: true onTriggered: { lControl.lightOn(txt_addressLight.text, lightSlider.value) } }
In my lightcontrol.cpp:
void LightControl::lightOn(QString addressOfLight, int sliderValue) {//some code}
lightOn, it's a SLOT, and all this is done without connect();
How do you like this solution to the problem?
-
В принципе, есть идея, чтобы значения из текстового поля и слайдера передались в обычные переменные типа QString и int, которые объявлены у меня в с++ классе. Только вот вопрос: возможно ли это? И как?
-
@razorqhex
Its not possible to connect in such way. SIGNAL and SLOTs arguments should match.. and your SLOT will get values what ever that signal pass. Here in Qtimer, timeout signal has no argument. so you cannot.You can write a slot on time out and inside that SLOT call for values from the QML and use them.
-
@Tirupathi-Korla said in Проблема с connect() из-за аргументов:
You can write a slot on time out and inside that SLOT call for values from the QML and use them.
How to do this? Apparently, I did not understand you. I'm sorry
-
connect(timerFor_LightON, SIGNAL(timeout()), this, SLOT(lightOn()));
slots:
void lightOn(){
// get values from qml
.....
}
-
@Tirupathi-Korla said in Проблема с connect() из-за аргументов:
connect(timerFor_LightON, SIGNAL(timeout()), this, SLOT(lightOn()));
slots:
void lightOn(){
// get values from qml
.....
}I wrote that. And right now I'm thinking about the question: how to get data from qml?
-
@razorqhex
using signals and slots...
-
@Tirupathi-Korla said in Проблема с connect() из-за аргументов:
using signals and slots...
I created a signal:
getFromQML(QString text, int value);
but, as far as I know, then such a signal is used to send to QML, but not take from QML
-
You can do like this..
Create timer in qml.. and on timeout you can call a cpp signal..
something like this...Timer { interval: 500; running: true; repeat: true onTriggered: { emit lightOn(QString,int) } }
-
@Tirupathi-Korla said in Проблема с connect() из-за аргументов:
You can do like this..
Create timer in qml.. and on timeout you can call a cpp signal..If I understand correctly, then the emit lightOn(QString, int) signal is a signal that is declared in my class?
-
in qml
signal lightOn(var str, var val)
Timer { interval: 500; running: true; repeat: true onTriggered: { emit lightOn(QString,int) } }
In your lightcontrol.h
public slots: void lightOn(QVariant, Variant);
And i guess you know how to connect signals and slots..
-
Sorry, but I still did not understand your decision and for this, I did another
In QML:
Button { id: btn_lightOn x: 0 y: 55 width: 199 height: 48 text: qsTr("Включить освещение") //onClicked: lControl.lightOn(txt_addressLight.text, lightSlider.value) onClicked: { timer.running = true } } Timer { id: timer interval: 1000; running: false; repeat: true onTriggered: { lControl.lightOn(txt_addressLight.text, lightSlider.value) } }
In my lightcontrol.cpp:
void LightControl::lightOn(QString addressOfLight, int sliderValue) {//some code}
lightOn, it's a SLOT, and all this is done without connect();
How do you like this solution to the problem?
-
@razorqhex
This works too...Both are almost same...
-
@Tirupathi-Korla
Thank you very much