I am connecting the projector to Quectel QCOM application. Now I want to skip that application and create my customized application to control the projector.
I wrote a program based on the guidelines of Quectel to switch off the projector. Following is the code. Even I used the QSerialPort code. But the projector couldn't switch off. However using the command prompt of the Quectel QCOM application, I could control the projector.
Please help me in this guys! I am stuck in this since a month.
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.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include<QSerialPort>
#include<string>
#include<QtGui>
#include <QMessageBox>
using namespace std;
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow() //
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QMessageBox::information(this,"Title here","Hello World");
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);
if(!serial->open(QIODevice::ReadWrite))
{
qDebug()<<"error: can't open com port";
}
QString command = "WT+LEDE=0\n";
QByteArray x = command.toLocal8Bit();
serial->write(x);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
QMake project file:
#-------------------------------------------------
Project created by QtCreator 2019-11-02T10:55:21
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GUISerialPort
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
Related
I am using QStateMachine framework for a device controller class. It works fine in debug mode. But, in the release mode QStateMachine::started() signal is not being emitted. A simple widget project for the problem (form is empty) is below.
Qt Version 5.14.1
Compiler : MSVC 2017, MinGW (both are 64-bit and results are same)
Test.pro
QT += core gui widgets
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
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 <QStateMachine>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void stateMachineStarted();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QStateMachine *stateMachine;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
void MainWindow::stateMachineStarted()
{
qDebug() << "MainWindow::stateMachineStarted()";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
qDebug() << "MainWindow::MainWindow()";
ui->setupUi(this);
stateMachine = new QStateMachine;
QState *closed = new QState;
QState *setup = new QState;
QState *opened = new QState;
QState *closing = new QState;
stateMachine->addState(closed);
stateMachine->addState(setup);
stateMachine->addState(opened);
stateMachine->addState(closing);
Q_ASSERT(connect(stateMachine, &QStateMachine::started, this, &MainWindow::stateMachineStarted));
stateMachine->setInitialState(closed);
stateMachine->start();
}
MainWindow::~MainWindow()
{
qDebug() << "MainWindow::~MainWindow()";
delete ui;
}
Application output in debug mode (I closed the form after few seconds.)
12:39:09: Starting C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\Test.exe ...
MainWindow::MainWindow()
MainWindow::stateMachineStarted()
MainWindow::~MainWindow()
12:39:11: C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\Test.exe exited with code 0
Application output in release mode (I closed the form after few seconds.)
12:27:51: Starting C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Release\release\Test.exe ...
MainWindow::MainWindow()
MainWindow::~MainWindow()
12:27:53: C:\bin\build-Test-Desktop_Qt_5_14_2_MSVC2017_64bit-Release\release\Test.exe exited with code 0
In release mode, it is likely that the connection is not made because you wrapped it inside a Q_ASSERT macro.
See Q_ASSERT release build semantics for more informations.
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
When using Qt::FramelessWindowHint with setSizeConstraint my widget does not recieve close events anymore, is this a bug? Minimal test example:
pro
QT += core gui
TARGET = QT-BUG
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget(){}
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QDebug>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QCloseEvent>
Widget::Widget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::Tool
| Qt::FramelessWindowHint
| Qt::WindowStaysOnTopHint);
QVBoxLayout *l2 = new QVBoxLayout(this);
l2->setSizeConstraint(QLayout::SetFixedSize);
this->setLayout(l2);
QLineEdit* _inputLine = new QLineEdit(this);
l2->addWidget(_inputLine);
}
main .cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Window events like Close and Minimize are actually tied to the existence of buttons. Bit of a poor design choice by the Qt folks.
It seems that Qt::FramelessWindowHint removes/disables the buttons, but you can re-enable them (without displaying them) via other window hints. For close events, add Qt::WindowCloseButtonHint.
I want to try QWinThumbnailToolBar in Qt 5.2 but it doesn't work !(Program runs but there is no thumbnail !!!!)
//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 <QWinThumbnailToolButton>
#include <QWinThumbnailToolBar>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QWinThumbnailToolBar* thumbnailToolBar;
QWinThumbnailToolButton *playToolButton;
QWinThumbnailToolButton *forwardToolButton;
QWinThumbnailToolButton *backwardToolButton;
};
#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);
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
playToolButton->setEnabled(false);
playToolButton->setToolTip(tr("true"));
playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
forwardToolButton->setEnabled(true);
forwardToolButton->setToolTip(tr("Fast forward"));
forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));
backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
backwardToolButton->setEnabled(true);
backwardToolButton->setToolTip(tr("Rewind"));
backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));
thumbnailToolBar->addButton(backwardToolButton);
thumbnailToolBar->addButton(playToolButton);
thumbnailToolBar->addButton(forwardToolButton);
}
MainWindow::~MainWindow()
{
delete ui;
}
//pro file :
QT += core gui winextras multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I think the problem is with these two line :
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
I also tried to use QWidget instead of QMainWindow...
How can I fix it ??
Your code to create QWinThumbnailToolBar is correct, the problem is where you create it. I think creating it in the window constructor is the problem (Maybe because the window handle is not ready yet). You can make something like this:
// main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrmMain w;
w.show();
w.createThmbBar();
return a.exec();
}
Where createThumbBar() is a public function where you create the QWinThumbnailToolBar as in:
// MainWindow.cpp
void MainWindow::createThmbBar()
{
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
playToolButton->setEnabled(false);
playToolButton->setToolTip(tr("true"));
playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
forwardToolButton->setEnabled(true);
forwardToolButton->setToolTip(tr("Fast forward"));
forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));
backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
backwardToolButton->setEnabled(true);
backwardToolButton->setToolTip(tr("Rewind"));
backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));
thumbnailToolBar->addButton(backwardToolButton);
thumbnailToolBar->addButton(playToolButton);
thumbnailToolBar->addButton(forwardToolButton);
}
Late answer but hopefully it could help anyone faces the same issue later.
The fix that #Ramez proposed works however I am getting a crash on application shutdown due to the windows extra. Is there something special I need to do myself in the destructor? The QWindow has already been deleted when the QWinThumbnailToolBarPrivate::hasHandle() checks for the handle.
Exception thrown: read access violation.
d was 0xFFFFFFFFFFFFFF7F.
QPlatformWindow *QWindow::handle() const
{
Q_D(const QWindow);
return d->platformWindow;
}
Stack Trace Below:
Qt5Guid.dll!QWindow::handle() Line 1929 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::hasHandle() Line 460 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::handle() Line 465 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::nativeEventFilter(const QByteArray & __formal, void * message, long * result) Line 549 C++
Qt5Cored.dll!QAbstractEventDispatcher::filterNativeEvent(const QByteArray & eventType, void * message, long * result) Line 484 C++
[External Code]
Qt5Guid.dll!QWindowPrivate::destroy() Line 1914 C++
Qt5Guid.dll!QWindow::destroy() Line 1864 C++
Qt5Widgetsd.dll!QWidgetPrivate::deleteTLSysExtra() Line 1891 C++
Qt5Widgetsd.dll!QWidget::destroy(bool destroyWindow, bool destroySubWindows) Line 12515 C++
Qt5Widgetsd.dll!QApplication::~QApplication() Line 798 C++
i want to make a program like msn messenger , i have use qt5 with network , when i open a new connection with my local server , it doesn't work , it gave back to me that my server not connected i don't know the reason
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void newConnection ();
private:
Ui::MainWindow *ui;
QTcpServer *server;
};
#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);
server = new QTcpServer(this);
connect(server , SIGNAL(newConnection()) , this , SLOT(newConnection()));
if(server->listen(QHostAddress::Any , 5050))
{
ui->label->setText("Not Start");
}
else
{
ui->label->setText("Server Started Now");
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
socket->write("Hello Islam");
socket->flush();
socket->waitForBytesWritten(3000);
socket->close();
}
void MainWindow::on_pushButton_clicked()
{
newConnection();
}
this for call library
#-------------------------------------------------
#
# Project created by QtCreator 2013-11-03T10:00:37
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TCPTEST
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
i tried to call new connection in mainwindow constructor but it didn't work
i think the error was in
if(server->listen(QHostAddress::Any , 1234))
which it worked but it must be
if(!server->listen(QHostAddress::Any , 1234))
to get server started