Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt first project
Qt 6.11 is out! See what's new in the release blog

Qt first project

Scheduled Pinned Locked Moved Qt Creator and other tools
59 Posts 7 Posters 28.2k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #48

    There are some key concepts that are pretty different yes.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nick784512
      wrote on last edited by
      #49

      And I should learn C++ for just this little error?

      Can someone help me to make it work?

      JKSHJ 1 Reply Last reply
      0
      • N Offline
        N Offline
        nick784512
        wrote on last edited by nick784512
        #50

        $$$$$$$$$$$$$$$$$$$$$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 = app

        SOURCES += main.cpp
        mainwindow.cpp

        HEADERS += mainwindow.h

        FORMS += mainwindow.ui

        $$$$$$$$$$$$$$$$$ mainwindow.h $$$$$$$$$$$$$$$$$$$$$$$$$$

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>

        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

        public:
        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>

        1 Reply Last reply
        0
        • N nick784512

          And I should learn C++ for just this little error?

          Can someone help me to make it work?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #51

          @nick784512 said in Qt first project:

          And I should learn C++ for just this little error?

          You should learn C++ to use a C++ toolkit.

          It feels like a "little error" now, but you will encounter this in most (if not all) of your C++ projects.

          Can someone help me to make it work?

          @SGaist already gave you the answer: You need to add a Forward Declaration for QSerialPort in mainwindow.h.

          Also remember to #include <QSerialPort> in mainwindow.cpp

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          3
          • N Offline
            N Offline
            nick784512
            wrote on last edited by
            #52

            Hello!

            I managed to make it work and send symbols over the serial. The problem now is that when I send from the arduino (to Qt application) 2 or 3 symbols it prints them OK! But when I want to send the sentence "Hello World!" it breaks it into pieces and prints them, as you can see in the photo.

            http://www.picpaste.com/pics/serial_problem-V7LlwzUC.1483458098.jpg

            (Can the moderator attach the photo to this message?)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #53

              That' why you usually have a protocol to know when you received all the data that are part of a "frame" and only then parse them for further processing.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nick784512
                wrote on last edited by
                #54

                How I do this? I think of using a "flag" that when the Qt application reads it, then prints the sentence. Right? For example "Hello World!#" and when it reads the "#", it prints the "Hello World!". Am I correct?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #55

                  More or less, yes.

                  You buffer the data you can read when readyRead is emitted and once you find your # you take the amount of data needed from the buffer and do whatever you want with it.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • N Offline
                    N Offline
                    nick784512
                    wrote on last edited by nick784512
                    #56

                    For buffer is better to use an array or a data structure? List for example and each node to save a letter? Is there a source code to study?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #57

                      Don't over-engineer it, just use a QByteArray.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • N Offline
                        N Offline
                        nick784512
                        wrote on last edited by
                        #58

                        I use an array with a fixed length that when it become full I read it, or an array with dynamic length?

                        mrjjM 1 Reply Last reply
                        0
                        • N nick784512

                          I use an array with a fixed length that when it become full I read it, or an array with dynamic length?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #59

                          @nick784512
                          Hi
                          a ByteArray ( dynamic ) would be the best. :)

                          void SerialPortReader::handleReadyRead()
                          {
                          m_readData.append(m_serialPort->readAll());
                          ...

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved