Qt first project

  • Hello,



    it says:

    The on_findButton_clicked() slot is called automatically in the uic generated ui_textfinder.h file by this line of code:


    Where do I put this?

  • Qt Champions 2016


    @nick784512 said in Qt first project:


    Assuming you're using forms for your widgets, you don't. The moc will call that for you in Ui::FormClass::setupUi(QWidget *).

  • Moderators


    Hi and welcome to devnet

    When you have followed the example it should work. This is only some explanation for your understanding, but apparently not really helpful.

    The file ui_*.h are automatically generated before the compilation. You can checkout this file for your understanding. You may even change it, but this is really not advisable, since it is frequently regenerated and therefore changed automatically.

  • Thank you. Where can I find a website or a tutorial that has Qt examples?

  • Qt Champions 2016

  • Qt Champions 2017

    Also in Creator, on the welcome page.
    There is "Examples"

  • Thank you,

    I am running the Blocking Master Example. Does anyone know how do I change the default COM1 to another COM, so that I can communicate with my arduino?

  • Qt Champions 2017

    @nick784512 said in Qt first project:

    Blocking Master Example

    this line
    sets it.

    But that sample lets u select all detected com ports ?

    const auto infos = QSerialPortInfo::availablePorts();
    for (const QSerialPortInfo &info : infos)

    what comport should arduino be on?

  • @mrjj said in Qt first project:

    It does not let me select other port than COM1. Arduino should is connected on COM3.

  • Qt Champions 2017

    You should try with other serial tool.
    and see if it can see it.
    Or any other method to verify that the board is seen.

  • Finally it worked! I disconnected the mouse's usb receipter, I connected the arduino there, it didn't work and when I disconnected from there and connected to the original usb port, the Qt application displayed a COM3 port to choose and now it works!!!

  • Qt Champions 2017

    Ok. super.
    Sometimes is possible to use Device Manager and right click the comport and there change what comport it uses.

  • Hello, I want to build an application that will have a simple button and when I press it to send the command "START" through COM. I am looking the Blocking Master Example but I cannot find the .ui file in order to see how they have programmed the buttons. Any ideas???

  • Qt Champions 2017

    Just Select File -> New Project -> Desktop Widget application
    in this new project goto Form and click the mainwindow Ui file
    Then drag a button from right left to the design area (main window)
    Right click the button and select goto Slot
    Select the "released()" signal from the list
    You should now be in the code showing a slot for the button.
    This code is called when button is clicked.

  • Hi, thank you. And which part of Blocking Master Example code do I choose to put inside the brackets of button released?

  • Qt Champions 2017

    The part you want to call when button is pushed.
    In the sample, Dialog.cpp is the window. There is no UI file.

    It calls
    void Dialog::transaction()

    But the real work in done is MasterThread.
    So you cannot just put it into the button click.

    If you are trying something simple,
    this sample is maybe a bit complex to start with
    using a thread with more.

    To just open a comport and write "start", all you need it.
    (Note. you should add more error checking code.)

    so if you put in slot

    QSerialPort serial;
    serial.setBaudRate(QSerialPort::Baud115200); // MAKE SURE ITS CORRECT
    serial.setDataBits(QSerialPort::Data8); // MAKE SURE ITS CORRECT
    serial.setParity(QSerialPort::NoParity); // MAKE SURE ITS CORRECT
    serial.setStopBits(QSerialPort::OneStop); // MAKE SURE ITS CORRECT
    serial.setFlowControl(QSerialPort::HardwareControl); // MAKE SURE ITS CORRECT
    if (serial.isOpen() && serial.isWritable())
     qDebug() << "Serial is open";
      output = "START";
    qDebug() << "sent";

    if you need a design where "the other end" answers, QSerialPort serial should live as member
    of mainwindow.

    this sample
    Can send text and show what comes back. its super for testing.

  • Thank you. What version of C++ does the Qt use? It is the "classic" C++ or C++11?

  • Qt Champions 2017

    Both. I think from 5.7 it must be c++11 but 5.6 LTS can be build with pre c++11.

  • Hello, Can someone send me a Qt program with one button that when I press it, it sends the command "START" through COM ? I am trying to figure out from Qt examples, but the more I read the more I am confused.

  • Lifetime Qt Champion


    What exactly is confusing you ?

  • The "terminal" example has so many functions, but too little explanation. How am I supposed to learn how it works, and what functions to use in my simple program (see above)?

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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?

  • Lifetime Qt Champion

    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?


    are all these enough?

  • Lifetime Qt Champion

    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?

  • Lifetime Qt Champion

    In your QWidget subclass.

  • In main.cpp?

  • Moderators

    @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 ....

  • Qt Champions 2017

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

    SOURCES += main.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    private slots:
    void serialReceived();

    Ui::MainWindow *ui;

    #endif // MAINWINDOW_H


    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;

    return a.exec();



    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort>
    #include <QDebug>

    QSerialPort *serial;

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    serial = new QSerialPort(this);

    delete ui;

    void MainWindow::serialReceived()
    QByteArray ba;


    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <property name="windowTitle">
    <widget class="QWidget" name="centralWidget">
    <widget class="QLabel" name="label">
    <property name="geometry">
    <property name="text">
    <widget class="QMenuBar" name="menuBar">
    <property name="geometry">
    <widget class="QToolBar" name="mainToolBar">
    <attribute name="toolBarArea">
    <attribute name="toolBarBreak">
    <widget class="QStatusBar" name="statusBar"/>
    <layoutdefault spacing="6" margin="11"/>

  • Lifetime Qt Champion

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

  • @SGaist

    I disabled it. Again, nothing received.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.