How to use QProcess for some program like "bluetoothctl"
-
wrote on 2 Mar 2020, 09:00 last edited by
Hi
I want to use Qt Bluetooth but it's not useful on Qt 5.6.2( event on ubuntu or imx6 device ). It just can scan but can't pair and connect. But Qt 5.12.0 is ok I know.So I use
QProcess
to runbluetoothctl
for control bluetooth, But I can't write and read anything. What should I do ?Regards
Mihan -
Hi
I want to use Qt Bluetooth but it's not useful on Qt 5.6.2( event on ubuntu or imx6 device ). It just can scan but can't pair and connect. But Qt 5.12.0 is ok I know.So I use
QProcess
to runbluetoothctl
for control bluetooth, But I can't write and read anything. What should I do ?Regards
MihanLifetime Qt Championwrote on 2 Mar 2020, 09:05 last edited by jsulm 3 Feb 2020, 09:06@Mihan said in How to use QProcess for some program like "bluetoothctl":
What should I do ?
Read documentation: https://doc.qt.io/qt-5/qprocess.html
Connect needed signals to slots:- https://doc.qt.io/qt-5/qprocess.html#errorOccurred
- https://doc.qt.io/qt-5/qprocess.html#readyReadStandardError
- https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput
"But I can't write and read anything" - what about showing the code, so others can actually see what could be the problem?
-
Hi
I want to use Qt Bluetooth but it's not useful on Qt 5.6.2( event on ubuntu or imx6 device ). It just can scan but can't pair and connect. But Qt 5.12.0 is ok I know.So I use
QProcess
to runbluetoothctl
for control bluetooth, But I can't write and read anything. What should I do ?Regards
Mihan -
@Mihan said in How to use QProcess for some program like "bluetoothctl":
What should I do ?
Read documentation: https://doc.qt.io/qt-5/qprocess.html
Connect needed signals to slots:- https://doc.qt.io/qt-5/qprocess.html#errorOccurred
- https://doc.qt.io/qt-5/qprocess.html#readyReadStandardError
- https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput
"But I can't write and read anything" - what about showing the code, so others can actually see what could be the problem?
wrote on 2 Mar 2020, 09:22 last edited by@jsulm Sure, I have connected the signals to the slots, here is the test code
#------------------------------------------------- # # Project created by QtCreator 2020-02-28T10:02:44 # #------------------------------------------------- QT += core gui \ multimedia \ bluetooth \ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = untitled4 TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 CONFIG += c++11 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QProcess> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void BluetoothctlRecevie(); void BluetoothctlError(); private: Ui::MainWindow *ui; QProcess *m_Bluetoothctl; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_Bluetoothctl = new QProcess(this); m_Bluetoothctl->setProgram("bluetoothctl"); connect(m_Bluetoothctl,SIGNAL(readyRead()),this,SLOT(BluetoothctlRecevie())); connect(m_Bluetoothctl,SIGNAL(readyReadStandardError()),this,SLOT(BluetoothctlError())); m_Bluetoothctl->start(); m_Bluetoothctl->write("scan on"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::BluetoothctlRecevie() { qDebug()<<m_Bluetoothctl->read(1024); } void MainWindow::BluetoothctlError() { qDebug()<<m_Bluetoothctl->readAllStandardError(); }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"/> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>28</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
-
@jsulm Sure, I have connected the signals to the slots, here is the test code
#------------------------------------------------- # # Project created by QtCreator 2020-02-28T10:02:44 # #------------------------------------------------- QT += core gui \ multimedia \ bluetooth \ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = untitled4 TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 CONFIG += c++11 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QProcess> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void BluetoothctlRecevie(); void BluetoothctlError(); private: Ui::MainWindow *ui; QProcess *m_Bluetoothctl; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_Bluetoothctl = new QProcess(this); m_Bluetoothctl->setProgram("bluetoothctl"); connect(m_Bluetoothctl,SIGNAL(readyRead()),this,SLOT(BluetoothctlRecevie())); connect(m_Bluetoothctl,SIGNAL(readyReadStandardError()),this,SLOT(BluetoothctlError())); m_Bluetoothctl->start(); m_Bluetoothctl->write("scan on"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::BluetoothctlRecevie() { qDebug()<<m_Bluetoothctl->read(1024); } void MainWindow::BluetoothctlError() { qDebug()<<m_Bluetoothctl->readAllStandardError(); }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"/> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>28</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
@Mihan said in How to use QProcess for some program like "bluetoothctl":
QProcess *m_Bluetoothctl;
Why not simply allocate on the stack?
You should wait until the process started before writing (https://doc.qt.io/qt-5/qprocess.html#started):
m_Bluetoothctl->start(); m_Bluetoothctl->write("scan on");
1/6