Qt first project
-
In main.cpp?
-
@nick784512 I would suggest you to create a simple widgets application and play a bit with it. You will see how Qt widgets applications are usually implemented. See http://doc.qt.io/qtcreator/creator-writing-program.html
Asking others to send you an implementation is not really the right way to learn. Best is "learning by doing" and asking specific questions if you're stuck. -
I have tried this one: https://www.youtube.com/watch?v=UD78xyKbrfk
and I connected the arduino to the COM3 with a program that sends 2 3 2 3... but the Qt program does not display the 2 3 2 3 .... -
@nick784512
In that video, he mere set the text he gets to a QLabel
MainWindow::serialReceived .
If it should respond to anything you must program it yourself.Please show what you changed it into to reply ? :)
-
$$$$$$$$$$$$$$$serialGui.pro$$$$$$$$$$$$$$$$$$
#-------------------------------------------------
Project created by QtCreator 2016-12-06T15:42:10
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = serialGui
TEMPLATE = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.h
FORMS += mainwindow.ui
$$$$$$$$$$$$$$$$$$mainwindow.h$$$$$$$$$$$$$$$$$$$$$$$$$
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void serialReceived();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
$$$$$$$$$$$$$$$$$$$$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 <QtSerialPort>
#include <QDebug>QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
serial->write("hello");
serial->close();
connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
}MainWindow::~MainWindow()
{
delete ui;
serial->close();
}void MainWindow::serialReceived()
{
QByteArray ba;
ba=serial->readAll();
ui->label->setText(ba);
qDebug()<<ba;
}$$$$$$$$$$$$$$$$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="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>60</y>
<width>121</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</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> -
Why are you closing your serial port in the constructor ? You won't receive anything doing so.
-
I disabled it. Again, nothing received.
-
You also don't check whether
open
was successful.You should rather move the call to
write
outside the constructor. e.g. in a slot connected to a push button.And make serial a member of MainWindow.
-
Thank you! I cannot understand how the tutorial video in youtube works and the same configuration and coding in my project does not work.
-
Did you check that the settings of the serial ports are really what you need to use with your device ?
-
1)How do I check if the serial opened succefully?
2)I disabled write.
3)Do you mean to make it private member of MainWindow? -
- Please read the documentation, the open function returns a boolean value saying whether it succeeded or not
- Where did you put the call to write ?
- Yes
-
1)Ok, I will try it.
2)I just commented it.
3) I wrote QSerialPort *serial; under private and it shows error -
What error ?
If you comment the call to write then don't expect anything particular to happen.
-
C:\Users\geo\Documents\serialGui\mainwindow.h:27: error: 'QSerialPort' does not name a type
QSerialPort *serial;
^I don't want to write something in COM from Qt, I want to "listen" from Arduino the sequence 232323... and print on the Qt application
-
It's nothing Qt related, that's basic C++. You really should grab a good book on the matter.
Search for "Forward declaration".
-
I know C, not C++. Are they so different?
-
There are some key concepts that are pretty different yes.
-
And I should learn C++ for just this little error?
Can someone help me to make it work?
-
$$$$$$$$$$$$$$$$$$$$$serialGui.pro$$$$$$$$$$$$$$$$$$$$$$$$$$
#-------------------------------------------------
Project created by QtCreator 2016-12-06T15:42:10
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = serialGui
TEMPLATE = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.h
FORMS += mainwindow.ui
$$$$$$$$$$$$$$$$$ mainwindow.h $$$$$$$$$$$$$$$$$$$$$$$$$$
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void serialReceived();void on_pushButton_released();
private:
Ui::MainWindow *ui;QSerialPort *serial;
};
#endif // MAINWINDOW_H
$$$$$$$$$$$$$$$ 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 <QtSerialPort>
#include <QDebug>QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
if (serial->open(QIODevice::ReadWrite))
{
ui->setupUi(this);
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
//serial->write("hello");
serial->close();
connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
}
else
{
QMessageBox::critical(this, tr("Error"), serial->errorString());
showStatusMessage(tr("Open error"));
}
}MainWindow::~MainWindow()
{
delete ui;
serial->close();
}void MainWindow::serialReceived()
{
QByteArray ba;
ba=serial->readAll();
ui->label->setText(ba);
//qDebug()<<ba;
}$$$$$$$$$$$$$$$$$$$ 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="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>60</y>
<width>121</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</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>