Qt first project
-
Do you mean explanation are missing from here ?
-
If you try to read all the files ( that are below )of the example, the explanation is too little.
-
Too little about what ? The key functions are explained in the presentation page and the rest is rather nicely written. So please be more specific about which part is giving you a hard time.
-
What this http://doc.qt.io/qt-5/qtserialport-terminal-settingsdialog-cpp.html code does?
From C language I know that we have to also call a function. Where are the functions called? -
The dialog is shown when clicking on the configure button in main dialog.
Then when you click on the connect button, the openSerialPort method will retrieve the current setting for the serial port and update the
serial
member variable properties with the settings from the dialog. -
Thank you. Now that I want to make the simple project as I said before. How many files do I need?
mainwindow.h
mainwindow.cpp
main.cpp
mainwindow.uiare all these enough?
-
Based on your requirements, you only need a QWidget, a QPushButton and a QVBoxLayout to put the button nicely in the widget.
-
But where will I put the code?
-
In your QWidget subclass.
-
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?