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
Forum Updated to NodeBB v4.3 + New Features

Qt first project

Scheduled Pinned Locked Moved Qt Creator and other tools
59 Posts 7 Posters 24.9k 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.
  • N Offline
    N Offline
    nick784512
    wrote on last edited by
    #35

    $$$$$$$$$$$$$$$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();

    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>

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

      Why are you closing your serial port in the constructor ? You won't receive anything doing so.

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

      N 1 Reply Last reply
      1
      • SGaistS SGaist

        Why are you closing your serial port in the constructor ? You won't receive anything doing so.

        N Offline
        N Offline
        nick784512
        wrote on last edited by
        #37

        @SGaist

        I disabled it. Again, nothing received.

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

          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.

          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
            #39

            Thank you! I cannot understand how the tutorial video in youtube works and the same configuration and coding in my project does not work.

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

              Did you check that the settings of the serial ports are really what you need to use with your device ?

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

              N 1 Reply Last reply
              0
              • SGaistS SGaist

                Did you check that the settings of the serial ports are really what you need to use with your device ?

                N Offline
                N Offline
                nick784512
                wrote on last edited by
                #41

                @SGaist

                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?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #42
                  1. Please read the documentation, the open function returns a boolean value saying whether it succeeded or not
                  2. Where did you put the call to write ?
                  3. Yes

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

                  N 1 Reply Last reply
                  0
                  • SGaistS SGaist
                    1. Please read the documentation, the open function returns a boolean value saying whether it succeeded or not
                    2. Where did you put the call to write ?
                    3. Yes
                    N Offline
                    N Offline
                    nick784512
                    wrote on last edited by nick784512
                    #43

                    @SGaist

                    1)Ok, I will try it.
                    2)I just commented it.
                    3) I wrote QSerialPort *serial; under private and it shows error

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

                      What error ?

                      If you comment the call to write then don't expect anything particular to happen.

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

                      N 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        What error ?

                        If you comment the call to write then don't expect anything particular to happen.

                        N Offline
                        N Offline
                        nick784512
                        wrote on last edited by
                        #45

                        @SGaist

                        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

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

                          It's nothing Qt related, that's basic C++. You really should grab a good book on the matter.

                          Search for "Forward declaration".

                          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
                            #47

                            I know C, not C++. Are they so different?

                            1 Reply Last reply
                            0
                            • 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

                                          • Login

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