GUI app and QTcpSocket
-
Hi everyone,
Before describing my problem I have to say I am completely new to Qt and my experience with C++ is limited to firmware for micro-controllers so please don't be mean :)
I am trying to build a small GUI app able to connect to a power supply through TCP/IP, read its status and change the output current.
What I need on the GUI is tow push buttons: connect disconnect, and a scroll bar to set the output current of the power supply.I followed these tutorials on youtube C++ Qt 65 - QTcpSocket basics, C++ Qt 66 - QTCPSocket using signals and slots and I manage to establish a connection and also send some commands to the power supply.
My problem now is that I am not able to use signals and slots between the gui and the QTcpSocket commands.I know this seems a pretty basic problem and is probably related to my pure Cpp knowledge than Qt it self, but I red lots of other tutorials and wikis and still I am very confused and don't really know from where to start in the code.
There are probably other simpler ways of doing this but I am very keen on learning Qt.
Any Kind of help would be very much appreciated.
Cheers
-
Hi and welcome to devnet,
I will assume you are using a QSlider to change the output current of your power supply.
One of the usual way to do it is to add a slot to your widget and connect your slider to it
MyWindow::MyWindow(QWidget *parent) : QWidget(parent) { QSlider *currentSlider = new QSlider; QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(currentSlider); connect(currentSlider, &QSlider::valueChanged, this, &MyWindow::onSliderValueChanged); } void MyWindow::onSliderValueChanged(int currentValue) { QByteArray cmd = "something" + QByteArray::number(currentValue); // To be replaced by the real command you need to send to the device. _myTcpSocket->write(cmd); }
Hope it helps.