I just tried the following code. But the slot function is not working. The connection is OK and I found out it by qDubug. The console output is as follows.
[ZDSGuard] 32 DllMain hook strProductName2 : C:\qt_example\build-
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debug\debug\serial_test.exe-1
ddd
ss
ccc
As you can find, aaa is not printed out. If the slot function works fine, it should be printed.
Please let me know if somebody finds out what is wrong.
Thanks in advance.
[ZDSGuard] 32 DllMain hook strProductName2 : C:\qt_example\build-
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debug\debug\serial_test.exe-1
ddd
ss
ccc
Serial Communication Code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QLabel>
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort();
serial->setPortName("COM4");
serial->setBaudRate(QSerialPort::Baud115200);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::NoParity);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
if (serial->open(QIODevice::ReadWrite))
ui->label->setText("bb");
if (QObject::connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived())))
qDebug()<< "ddd";
//ui->label->setText("aa");
qDebug() << "ss";
}
MainWindow::~MainWindow()
{
delete ui;
serial->close();
}
void MainWindow::serialReceived()
{
QByteArray BA;
BA=serial->readAll();
ui->label->setText("aa");
//printf(BA);
qDebug()<<"aaa";//BA;
}
QSP has bug in Qt 5.13.1. Use or Qt 5.13.0, or wait for a newest versions (5.13.2 / 5.12.6).
Related
Im trying to use QTserialport to connect to a port via uart
I get QIODevice::read (QSerialPort): device not open this but device is connect as i tested with other applications and with Qserialportinfo I was able to detect the serial port but not connect to it and read from it which part am i doing wrong ??
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m.setBaudRate(QSerialPort::Baud115200);
m.setDataBits(QSerialPort::Data8);
m.setPortName("COM2");
m.setFlowControl(QSerialPort::NoFlowControl);
m.setParity(QSerialPort::NoParity);
m.setStopBits(QSerialPort::OneStop);
m.setReadBufferSize(1000);
qDebug()<<m.portName();
while(1){
qDebug()<<m.readAll();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QSerialPort>
#include <QMainWindow>
#include <QSerialPortInfo>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QSerialPort m;
QSerialPortInfo info;
};
#endif // MAINWINDOW_H
ps. I tried to use //./ & \.\ & ////.// & \\.\ before the port but it didnt worked
I believe you have to open the port before reading from it. Try something like this:
if (m.open(QIODevice::ReadOnly)){
while (1){
if (m.waitForReadyRead(1000)){
qDebug() << m.readAll();
}
}
}
after adding m.open() worked perfectly
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m.setBaudRate(QSerialPort::Baud115200);
m.setDataBits(QSerialPort::Data8);
m.setPortName("COM2");
m.setFlowControl(QSerialPort::NoFlowControl);
m.setParity(QSerialPort::NoParity);
m.setStopBits(QSerialPort::OneStop);
m.setReadBufferSize(1000);
m.open(QIODevice::ReadOnly); //after adding this worked perfectly
qDebug()<<m.portName();
while(1){
qDebug()<<m.readAll();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
So i am using the combobox on QT, in the combobox i added two devices names and i wanted to change the name in combobox to send data via radiobox to my port i tried the if statment but it doesn't work.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>
QSerialPort serial;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial.setPortName("COM3");
serial.setBaudRate(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
ui->comboBox->addItem("SP157B");
ui->comboBox->addItem("JSP");
}
void MainWindow::on_pushButton_clicked()
{
if(ui->comboBox->currentText())
{
if(ui->radioButton->isChecked())
{
QByteArray Rouge("\x15\x7B\x1A\xAA");
serial.write(Rouge);
QString t = ui->label->text();
ui->label->setText(t+"\n15 7B 1A AA");
}
if(ui->radioButton_2->isChecked())
{
QByteArray Vert("\x15\x7B\x19\xA9"); //allume en vert
serial.write(Vert);
}
if(ui->radioButton_3->isChecked())
{
QByteArray Orange("\x15\x7B\x1B\xAB");
serial.write(Orange);
}
}
}
I am making an application that adds a new user account on Windows, in Qt Creator. The application asks for a username and password, then opens the cmd and enters the command net user username password /all. I want to be able to make a label that is always equal to what the cmd returns after the command has been input, for example "This account already exists". My code is below.
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QProcess"
#include "QTextStream"
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QProcess process1;
QStringList arguments1;
arguments1 << "net user" << ui->lineEdit->text() << ui->lineEdit_2->text() << "/add";
QString path = "C:/Windows/system32/WindowsPowerShell/v1.0/powershell.exe";
process1.execute(path, arguments1);
QString output(process1.readAllStandardOutput());
ui->label_4->setText(output);
}
process1.QProcess::waitForFinished(-1);
QString output(process1.readAllStandardOutput());
ui->label_4->setText(output);
I met a strange problem and I have searched for several hours but cannot find solution.
I am using Qt to write a Windows desktop application and I want to download a file from the Internet so I use QNetworkAccessManager. Following is my test code, which is in the MainWindow's constructor:
QNetworkRequest request;
request.setUrl(QUrl("www.example.org"));
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
if(manager->networkAccessible() == QNetworkAccessManager::Accessible){
qDebug() << "Network accessible";
}
else{
qDebug() << "Network is not accessible";
}
manager->get(request);
connect(manager, manager->finished, this, connFinished);
And next is connFinished function:
void MainWindow::connFinished(QNetworkReply *r){
int statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << statusCode;
}
After running the code, the output is:
Network accessible
0
I'm sure that the www.example.org can be accessed in my machine and there is no redirection. The HTTP status code is 0 even my PC has disconnected from the Internet. The problem still happens in a new project so it's not only this project's problem.
My Qt version: Qt 5.5.1 (MSVC 2013, 32 bit)
Compiler: gcc version 5.1.0 (tdm-1)
Is there anyone knows why this happened? Thanks!
Project files and code:
test.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void connFinished(QNetworkReply *r);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QNetworkRequest request;
request.setUrl(QUrl("www.example.org"));
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
if(manager->networkAccessible() == QNetworkAccessManager::Accessible){
qDebug() << "Network accessible";
}
else{
qDebug() << "Network is not accessible";
}
manager->get(request);
connect(manager, manager->finished, this, connFinished);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connFinished(QNetworkReply *r){
int statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << statusCode;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Before call QNetworkAccessManager::get() function you have to connects it signals to slot.
QNetworkRequest request;
request.setUrl(QUrl("www.example.org"));
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),this,SLOT(connFinished(QNetworkReply*)));
manager->get(request);
Okay, I have found where the problem is. I have to use http://www.exmaple.org rather than www.example.org, otherwise the QNAM will report a ProtocolUnknownError error. I assumed that the QNAM will guess the protocol type. But sadly it won't.
If using https protocol in your URL, you should add the following dll files beside your executable file.
libeay32.dll
ssleay32.dll
I want to do dbus-send from shell/console to a qt application.
This is the code for a simple QT app
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtDBus>
#include <QDBusConnection>
#include <QDebug>
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
bool ret = QDBusConnection::sessionBus().connect(
"MyHome", //service
"/", //path
"com.mydomain.mcp", //interface
"usb", //name
this, //receiver
SLOT(messageSlot(QString)));
}
void MainWindow::messageSlot(const QString &t1)
{
qDebug("%s", QString("%1").arg(t1).toUtf8().data());
}
From the terminal, I and sending this command
dbus-send --session --print-reply --reply-timeout=2000 --type=method_call / com.mydomain.mcp.usb string:'a'
I get this error: Method "usb" with signature "s" on interface "com.mydomain.mcp" doesn't exist
What am I doing wrong?
Thanks