Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Need help with C++ syntax - repost
QtWS25 Last Chance

Need help with C++ syntax - repost

Scheduled Pinned Locked Moved Unsolved C++ Gurus
19 Posts 3 Posters 1.1k Views
  • 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by Chris Kawa
    #1

    APPEND

    I am not sure if this is a good idea - adding to older post , so I may copy it into a new post .

    I am again at lost analyzing this syntax.

    I have some understating of the "new" entries, but I do not get this part

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
    

    Would it be possible to get an expiation in English ?

    I am trying to convert QMainWindow class to QWidget.

    //! [0]
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        m_ui(new Ui::MainWindow),
        m_status(new QLabel),
        m_console(new Console),
        m_settings(new SettingsDialog),
    //! [1]
        m_serial(new QSerialPort(this))
    //! [1]
    
    DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent)
    :   QDialog(parent), localDevice(new QBluetoothLocalDevice),
        ui(new Ui_DeviceDiscovery)
    {
    

    Where would be a good resource to learn this?

    I actually need to get info on "localDevice" , and have never used C++ this way.

    Actually this complex constructor is used by passing null pointer , or better - passing default NULL pointer
    as QWidget *parent). Little too complex for me...

    If it is OK - I could probably get started if somebody reads me the code in English...

    Thanks

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      @AnneRanch said in Need help with C++ syntax - repost:

      DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent)
      : QDialog(parent), localDevice(new QBluetoothLocalDevice),
      ui(new Ui_DeviceDiscovery)
      {

      Which part is not clear for you? For your app, the followings are same.

      DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent)
      : QDialog(parent), localDevice(new QBluetoothLocalDevice),
      ui(new Ui_DeviceDiscovery)
      {
      }
      
      same as 
      DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent)
      : QDialog(parent), 
        localDevice(new QBluetoothLocalDevice),
       ui(new Ui_DeviceDiscovery)
      {
      }
      
      or same as
      DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent)
      : QDialog(parent)
      {
        localDevice = new QBluetoothLocalDevice;
        ui = new Ui_DeviceDiscovery;
      }
      
      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        It's exactly the same as here, just different variable and class names.

        I am trying to convert QMainWindow class to QWidget

        There's nothing to convert. QMainWindow is a class that derives from QWidget, so it already is a QWidget.

        I do not get this part
        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),

        MainWindow::MainWindow is the constructor of class MainWindow. It is called when an instance of it is created.
        It takes a parent parameter that points to a widget that is gonna be a parent of that instance. It can be a valid pointer or nullptr, in which case the instance won't have a parent (this is usual for top level windows).
        Class MainWindow derives (or in other words inherits) from class QMainWindow, so the constructor of MainWindow calls constructor of its base class QMainWindow and passes it the parent parameter. That's the QMainWindow(parent) part.

        The chain of inheritance here is as follows:
        MainWindow inherits QMainWindow inherits QWidget inherits QObject.
        So when you create an instance of MainWindow (usually in your main() function) the constructors of all these classes are called:
        MainWindow(parent) calls QMainWindow(parent) calls QWidget(parent) calls QObject(parent).

        A 1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          It's exactly the same as here, just different variable and class names.

          I am trying to convert QMainWindow class to QWidget

          There's nothing to convert. QMainWindow is a class that derives from QWidget, so it already is a QWidget.

          I do not get this part
          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),

          MainWindow::MainWindow is the constructor of class MainWindow. It is called when an instance of it is created.
          It takes a parent parameter that points to a widget that is gonna be a parent of that instance. It can be a valid pointer or nullptr, in which case the instance won't have a parent (this is usual for top level windows).
          Class MainWindow derives (or in other words inherits) from class QMainWindow, so the constructor of MainWindow calls constructor of its base class QMainWindow and passes it the parent parameter. That's the QMainWindow(parent) part.

          The chain of inheritance here is as follows:
          MainWindow inherits QMainWindow inherits QWidget inherits QObject.
          So when you create an instance of MainWindow (usually in your main() function) the constructors of all these classes are called:
          MainWindow(parent) calls QMainWindow(parent) calls QWidget(parent) calls QObject(parent).

          A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #4

          @Chris-Kawa Thanbks , appreciate all of your help.
          I have another issue - appears that my OS is broken again, so I need to keep this short.

          What I actually want is to be able to reuse the project QMainWindow.

          Obviously I cannot just instantiate it - there are other QMainWIndow in my subproject...

          Can I build / add another class to the Terminal project and make MainWindow its member ?

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            QMainWindow is not a project. It's a class. You can have as many instances of it as you want:

            QMainWindow* instance1 = new QMainWindow();
            QMainWindow* instance2 = new QMainWindow();
            ...
            

            If you mean you have multiple classes deriving QMainWindow just name them differently or put them in a namespace:

            class SomeWindow: public QMainWindow
            {
             ...
            };
            
            class SomeOtherWindow: public QMainWindow
            {
             ...
            };
            
            SomeWindow* instance1 = new SomeWindow();
            SomeOtherWindow* instance2 = new SomeOtherWindow();
            

            or

            namespace A
            {
               class MainWindow: public QMainWindow
               {
                ...
               };
            }
            
            namespace B
            {
               class MainWindow: public QMainWindow
               {
                ...
               };
            }
            
            
            A::MainWindow* instance1 = new A::MainWindow();
            B::MainWindow* instance2 = new B::MainWindow();
            
            A 1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              QMainWindow is not a project. It's a class. You can have as many instances of it as you want:

              QMainWindow* instance1 = new QMainWindow();
              QMainWindow* instance2 = new QMainWindow();
              ...
              

              If you mean you have multiple classes deriving QMainWindow just name them differently or put them in a namespace:

              class SomeWindow: public QMainWindow
              {
               ...
              };
              
              class SomeOtherWindow: public QMainWindow
              {
               ...
              };
              
              SomeWindow* instance1 = new SomeWindow();
              SomeOtherWindow* instance2 = new SomeOtherWindow();
              

              or

              namespace A
              {
                 class MainWindow: public QMainWindow
                 {
                  ...
                 };
              }
              
              namespace B
              {
                 class MainWindow: public QMainWindow
                 {
                  ...
                 };
              }
              
              
              A::MainWindow* instance1 = new A::MainWindow();
              B::MainWindow* instance2 = new B::MainWindow();
              
              A Offline
              A Offline
              Anonymous_Banned275
              wrote on last edited by
              #6

              @Chris-Kawa Neat idea - so all I have to do is to rename the MainWindow in "Terminal" project...

              A 1 Reply Last reply
              0
              • A Anonymous_Banned275

                @Chris-Kawa Neat idea - so all I have to do is to rename the MainWindow in "Terminal" project...

                A Offline
                A Offline
                Anonymous_Banned275
                wrote on last edited by
                #7

                @AnneRanch Unfortunately little too complicated - I had to rename both .cpp and .h files....
                Now it is failing when I use / reuse the original "MainNwindow" form - it has references to QMainWindow...
                I'll try "namespace" next ....
                However - I think the ideas to build a class - say "C_Terminal " and make the MainWindow its variable / method may work the best .... trying to make as little changes to the original "MainWindow" class...

                A 1 Reply Last reply
                0
                • A Anonymous_Banned275

                  @AnneRanch Unfortunately little too complicated - I had to rename both .cpp and .h files....
                  Now it is failing when I use / reuse the original "MainNwindow" form - it has references to QMainWindow...
                  I'll try "namespace" next ....
                  However - I think the ideas to build a class - say "C_Terminal " and make the MainWindow its variable / method may work the best .... trying to make as little changes to the original "MainWindow" class...

                  A Offline
                  A Offline
                  Anonymous_Banned275
                  wrote on last edited by Chris Kawa
                  #8

                  I need to "re group". I think I am making it more complicated then it needs to be

                  I think my last attempt to rename "MainWindow" was OK , but I missed the

                  "NAMESPACE" in header ....

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  #include <QSerialPort>
                  
                  QT_BEGIN_NAMESPACE
                  
                  class QLabel;
                  
                  namespace Ui {
                  class MainWindow;
                  }
                  
                  QT_END_NAMESPACE
                  
                  class Console;
                  class SettingsDialog;
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit MainWindow(QWidget *parent = nullptr);
                      ~MainWindow();
                  
                  private slots:
                      void openSerialPort();
                      void closeSerialPort();
                      void about();
                      void writeData(const QByteArray &data);
                      void readData();
                  
                      void handleError(QSerialPort::SerialPortError error);
                  
                  private:
                      void initActionsConnections();
                  
                  private:
                      void showStatusMessage(const QString &message);
                  
                      Ui::MainWindow *m_ui = nullptr;
                      QLabel *m_status = nullptr;
                      Console *m_console = nullptr;
                      SettingsDialog *m_settings = nullptr;
                      QSerialPort *m_serial = nullptr;
                  };
                  
                  #endif // MAINWINDOW_H
                  
                  A 1 Reply Last reply
                  0
                  • A Anonymous_Banned275

                    I need to "re group". I think I am making it more complicated then it needs to be

                    I think my last attempt to rename "MainWindow" was OK , but I missed the

                    "NAMESPACE" in header ....

                    #ifndef MAINWINDOW_H
                    #define MAINWINDOW_H
                    
                    #include <QMainWindow>
                    #include <QSerialPort>
                    
                    QT_BEGIN_NAMESPACE
                    
                    class QLabel;
                    
                    namespace Ui {
                    class MainWindow;
                    }
                    
                    QT_END_NAMESPACE
                    
                    class Console;
                    class SettingsDialog;
                    
                    class MainWindow : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        explicit MainWindow(QWidget *parent = nullptr);
                        ~MainWindow();
                    
                    private slots:
                        void openSerialPort();
                        void closeSerialPort();
                        void about();
                        void writeData(const QByteArray &data);
                        void readData();
                    
                        void handleError(QSerialPort::SerialPortError error);
                    
                    private:
                        void initActionsConnections();
                    
                    private:
                        void showStatusMessage(const QString &message);
                    
                        Ui::MainWindow *m_ui = nullptr;
                        QLabel *m_status = nullptr;
                        Console *m_console = nullptr;
                        SettingsDialog *m_settings = nullptr;
                        QSerialPort *m_serial = nullptr;
                    };
                    
                    #endif // MAINWINDOW_H
                    
                    A Offline
                    A Offline
                    Anonymous_Banned275
                    wrote on last edited by
                    #9

                    @AnneRanch I am trying to understand how is "namespace " used in the "terminal " example.
                    Why is only "Ui" enclosed in "namespace"?

                    Or better yet - how do I "exited the namespace " to the entire class ?

                    1 Reply Last reply
                    0
                    • JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #10

                      you see here:
                      Ui::MainWindow *m_ui = nullptr;
                      MainWindow belongs to namespace Ui. In order to use it, you have to define the class name first. It is the same as QLabel.

                      you have to define
                      class QLabel;
                      then you can use
                      QLabel *m_status = nullptr;
                      Otherwide QLabel is not defined.

                      MainWindow is defined in namespace. therefore you define it inside
                      namespace Ui {
                      class MainWindow;
                      }

                      this may save you a bit time. Same thing.

                          Ui::MainWindow *m_ui{};
                          QLabel *m_status{};
                          Console *m_console{];
                          SettingsDialog *m_settings{};
                          QSerialPort *m_serial{};
                      
                      A 1 Reply Last reply
                      0
                      • JoeCFDJ JoeCFD

                        you see here:
                        Ui::MainWindow *m_ui = nullptr;
                        MainWindow belongs to namespace Ui. In order to use it, you have to define the class name first. It is the same as QLabel.

                        you have to define
                        class QLabel;
                        then you can use
                        QLabel *m_status = nullptr;
                        Otherwide QLabel is not defined.

                        MainWindow is defined in namespace. therefore you define it inside
                        namespace Ui {
                        class MainWindow;
                        }

                        this may save you a bit time. Same thing.

                            Ui::MainWindow *m_ui{};
                            QLabel *m_status{};
                            Console *m_console{];
                            SettingsDialog *m_settings{};
                            QSerialPort *m_serial{};
                        
                        A Offline
                        A Offline
                        Anonymous_Banned275
                        wrote on last edited by
                        #11

                        @JoeCFD ...but that defeats the (desired) task of reusing the existing code .

                        I went and added this piece of code

                        //QT_BEGIN_NAMESPACE TERMINAL_SERIAL
                        
                        **namespace TERMINAL**
                        {
                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                        public:
                            explicit MainWindow(QWidget *parent = nullptr);
                            ~MainWindow();
                        
                        private slots:
                        

                        Then I changed all references to MainWindow
                        to
                        TERMINAL::MainWindow

                        it complied and run BUT when I add TERMINAL
                        to the code / class which actually uses the TERMINAL namespace modified class I get an error

                        /media/nov25-1/MDI_BASE_RAID_5/DIRECT_BACKUP_DEC16_NAMESPACE/Qt-5.15.2/widgets/mainwindows/mdi/mainwindow.cpp:112: error: use of undeclared identifier 'TERMINAL'
                        mainwindow.cpp:112:9: error: use of undeclared identifier 'TERMINAL'
                        TERMINAL::MainWindow *MW = new MainWindow();
                        ^

                        How do I make /mdi/mainwindow.cpp aware of "namespace TERMINAL" ?
                        Normal "include ' does not work ...

                        case 0:
                        {
                        qDebug() <<Q_FUNC_INFO; // "updateWindowMenu() build windows submenu ";
                        qDebug() << "& line " << QString::number(LINE);

                            **TERMINAL::MainWindow *MW = new MainWindow();**  no go 
                        
                        
                            mdiArea->addSubWindow(MW)->
                                    //         setWindowState(Qt::WindowMinimized) ;
                                    setWindowState(Qt::WindowMinimized) ;
                        
                        1 Reply Last reply
                        0
                        • Chris KawaC Offline
                          Chris KawaC Offline
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on last edited by Chris Kawa
                          #12

                          @AnneRanch said:

                          Why is only "Ui" enclosed in "namespace"?

                          MainWindow is your class. Ui::MainWindow is another class, generated from the .ui file. It is put in the Ui namespace to denote that it is a designer generated class. It is instantiated in the constructor via new and assigned to the ui member variable. It holds pointers to all ui widgets and only has the setupUi method that sets your MainWindow with what you created in the designer.

                          As to renaming. Lets say you want to rename MainWindow to TerminalWindow.

                          1. Your MainWindow is a designer class, so it consists of 3 files: mainwindow.h, mainwindow.cpp and mainwindow.ui. Rename these to terminalwindow.h, terminalwindow.cpp and terminalwindow.ui.

                          2. In terminalwindow.h:

                          • rename class MainWindow : public QMainWindow to class TerminalWindow : public QMainWindow
                          • replace namespace Ui { class MainWindow; } with namespace Ui { class TerminalWindow; }
                          • replace Ui::MainWindow *ui; with Ui::TerminalWindow *ui;
                          1. In terminalwindow.cpp:
                          • change #include "mainwindow.h" to #include "terminalwindow.h"
                          • change #include "ui_mainwindow.h" to #include "ui_terminalwindow.h"
                          • change all methods from MainWindow::xxx to TerminalWindow::xxx
                          • change creation of the ui from ui(new Ui::MainWindow) to ui(new Ui::TerminalWindow)
                          1. In terminalwindow.ui you have 2 options:
                          • you can open it in the form designer, select MainWindow in the object hierarchy and change its objectName property from MainWindow to TerminalWindow
                          • or you can open it in a text editor and change the XML directly, replacing MainWindow with TerminalWindow
                          1. in all other files that use that class:
                          • change #include "mainwindow.h" to #include "terminalwindow.h"
                          • replace MainWindow with TerminalWindow
                          1. Last but not least, in your .pro file replace the names in SOURCES, HEADERS and FORMS from mainwindow.h, mainwindow.cpp and mainwindow.ui to terminalwindow.h, terminalwindow.cpp and terminalwindow.ui
                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Anonymous_Banned275
                            wrote on last edited by
                            #13

                            BUIDING PROGRESS

                            I hope this error is due to incomplete rebuidl, however , here itis in case it is not

                            /media/nov25-1/MDI_BASE_RAID_5/DIRECT_BACKUP_DEC15_C_TERMINAL_MAIN/Qt-5.15.2/serialport/terminal/terminal_mainwindow_copy.cpp:76: error: allocation of incomplete type 'Ui::Terminal_MainWindow'
                            terminal_mainwindow_copy.cpp:76:14: error: allocation of incomplete type 'Ui::Terminal_MainWindow'
                                m_ui(new Ui::Terminal_MainWindow),
                                         ^~~~~~~~~~~~~~~~~~~~~~~
                            ./terminal_mainwindow_copy.h:66:7: note: forward declaration of 'Ui::Terminal_MainWindow'
                            class Terminal_MainWindow;
                                  ^
                            

                            this is the actual code posting the first error

                            //! [0]
                            Terminal_MainWindow::Terminal_MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                **m_ui(new Ui::Terminal_MainWindow),**    **source of error** 
                                m_status(new QLabel),
                                m_console(new Console),
                                m_settings(new SettingsDialog),
                            //! [1]
                                m_serial(new QSerialPort(this))
                            //! [1]
                            {
                            //! [0]
                                qDebug() <<Q_FUNC_INFO; // "updateWindowMenu() build windows submenu  ";
                                qDebug() << "& line " << QString::number(__LINE__);
                                m_ui->setupUi(this);
                                m_console->setEnabled(false);
                                setCentralWidget(m_console);
                            
                            
                            
                                m_ui->actionConnect->setEnabled(true);
                            
                            
                                m_ui->actionDisconnect->setEnabled(false);
                                qDebug() <<Q_FUNC_INFO; // "updateWindowMenu() build windows submenu  ";
                                qDebug() << "& line " << QString::number(__LINE__);
                            #ifdef BYPASS
                            

                            Here is the snippet referring to "forward declaration " in error

                            QT_BEGIN_NAMESPACE
                            
                            class QLabel;
                            
                            namespace Ui {
                            //class MainWindow;
                            class Terminal_MainWindow;
                            // replace DONE
                            }
                            
                            QT_END_NAMESPACE
                            
                            class Console;
                            class SettingsDialog;
                            
                            class Terminal_MainWindow : public QMainWindow
                            {
                                Q_OBJECT
                            

                            PS
                            I did use QT "duplicate file" and removed all the references to the original MainWindow

                            1 Reply Last reply
                            0
                            • Chris KawaC Offline
                              Chris KawaC Offline
                              Chris Kawa
                              Lifetime Qt Champion
                              wrote on last edited by Chris Kawa
                              #14

                              The definition of that symbol comes from the ui_terminal_mainwindow.h (or whatever you called it) file that gets generated form the .ui file, so either you didn't change the name in the .ui itself or it wasn't rebuilt. Make sure the change is there, delete your build directory (or just the generated ui_terminal_mainwindow.h) and rebuild. If in doubt you can just open that generated file and see if class Ui::Terminal_MainWindow is there or is it still Ui::MainWindow.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                Anonymous_Banned275
                                wrote on last edited by
                                #15

                                OK, I did give it another try and still getting same error

                                /media/nov25-1/MDI_BASE_RAID_5/DIRECT_BACKUP_DEC17_TERMINAL/Qt-5.15.2/serialport/terminal/TERMINAL_mainwindow_copy.cpp:70: error: allocation of incomplete type 'Ui::TERMINAL_MainWindow'
                                TERMINAL_mainwindow_copy.cpp:70:14: error: allocation of incomplete type 'Ui::TERMINAL_MainWindow'
                                m_ui(new Ui::TERMINAL_MainWindow),
                                ^~~~~~~~~~~~~~~~~~~~~~~
                                ./TERMINAL_mainwindow_copy.h:63:7: note: forward declaration of 'Ui::TERMINAL_MainWindow'
                                class TERMINAL_MainWindow;
                                ^

                                I am including full copies of .cpp and ,h files .

                                I DID Clean IT uP As much as i could, but keep in mind it is a code under construction
                                and it is being posted to help me find the error.

                                #ifndef TERMINAL_MAINWINDOW_COPY_H
                                #define TERMINAL_MAINWINDOW_COPY_H
                                
                                #include <QMainWindow>
                                #include <QSerialPort>
                                
                                QT_BEGIN_NAMESPACE
                                
                                class QLabel;
                                
                                namespace Ui {
                                class TERMINAL_MainWindow;
                                }
                                
                                QT_END_NAMESPACE
                                
                                
                                class Console;
                                class SettingsDialog;
                                
                                
                                class TERMINAL_MainWindow : public QMainWindow
                                {
                                    Q_OBJECT
                                
                                public:
                                #ifdef BYPASS
                                    explicit MainWindow(QWidget *parent = nullptr);
                                    ~MainWindow();
                                #endif
                                    explicit TERMINAL_MainWindow(QWidget *parent = nullptr);
                                    ~TERMINAL_MainWindow();
                                
                                private slots:
                                    void openSerialPort(QT_END_NAMESPACE);
                                    void closeSerialPort();
                                    void about();
                                    void writeData(const QByteArray &data);
                                    void readData();
                                
                                    void handleError(QSerialPort::SerialPortError error);
                                
                                private:
                                    void initActionsConnections();
                                
                                private:
                                    void showStatusMessage(const QString &message);
                                
                                    Ui::TERMINAL_MainWindow *m_ui; //  = nullptr;
                                    QLabel *m_status = nullptr;
                                    Console *m_console = nullptr;
                                    SettingsDialog *m_settings = nullptr;
                                    QSerialPort *m_serial = nullptr;
                                };
                                
                                
                                #endif // TERMINAL_MAINWINDOW_COPY_H
                                
                                
                                //#include "mainwindow.h"
                                #include "TERMINAL_mainwindow_copy.h"
                                // DONE
                                
                                //#include "ui_mainwindow.h"
                                //  change #include "ui_mainwindow.h" to #include "ui_terminalwindow.h"
                                #include "ui_TERMINAL_mainwindow_copy.h"
                                // this was missing
                                
                                #include "console.h"
                                #include "settingsdialog.h"
                                
                                #include <QLabel>
                                #include <QMessageBox>
                                //  change creation of the ui from ui(new Ui::MainWindow) to ui(new Ui::TerminalWindow)
                                //! [0]
                                TERMINAL_MainWindow::TERMINAL_MainWindow(QWidget *parent) :
                                    QMainWindow(parent),
                                    m_ui(new Ui::TERMINAL_MainWindow),
                                    m_status(new QLabel),
                                    m_console(new Console),
                                    m_settings(new SettingsDialog),
                                //! [1]
                                    m_serial(new QSerialPort(this))
                                //! [1]
                                {
                                //! [0]
                                    m_ui->setupUi(this);
                                    m_console->setEnabled(false);
                                    setCentralWidget(m_console);
                                
                                    m_ui->actionConnect->setEnabled(true);
                                    m_ui->actionDisconnect->setEnabled(false);
                                    m_ui->actionQuit->setEnabled(true);
                                    m_ui->actionConfigure->setEnabled(true);
                                
                                    m_ui->statusBar->addWidget(m_status);
                                
                                    initActionsConnections();
                                
                                    connect(m_serial, &QSerialPort::errorOccurred, this, &TERMINAL_MainWindow::handleError);
                                
                                //! [2]
                                    connect(m_serial, &QSerialPort::readyRead, this, &TERMINAL_MainWindow::readData);
                                //! [2]
                                    connect(m_console, &Console::getData, this, &TERMINAL_MainWindow::writeData);
                                //! [3]
                                }
                                //! [3]
                                
                                TERMINAL_MainWindow::~TERMINAL_MainWindow()
                                {
                                    delete m_settings;
                                    delete m_ui;
                                }
                                
                                //! [4]
                                void TERMINAL_MainWindow::openSerialPort()
                                {
                                    const SettingsDialog::Settings p = m_settings->settings();
                                    m_serial->setPortName(p.name);
                                    m_serial->setBaudRate(p.baudRate);
                                    m_serial->setDataBits(p.dataBits);
                                    m_serial->setParity(p.parity);
                                    m_serial->setStopBits(p.stopBits);
                                    m_serial->setFlowControl(p.flowControl);
                                    if (m_serial->open(QIODevice::ReadWrite)) {
                                        m_console->setEnabled(true);
                                        m_console->setLocalEchoEnabled(p.localEchoEnabled);
                                        m_ui->actionConnect->setEnabled(false);
                                        m_ui->actionDisconnect->setEnabled(true);
                                        m_ui->actionConfigure->setEnabled(false);
                                        showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
                                                          .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
                                                          .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
                                    } else {
                                        QMessageBox::critical(this, tr("Error"), m_serial->errorString());
                                
                                        showStatusMessage(tr("Open error"));
                                    }
                                }
                                //! [4]
                                
                                //! [5]
                                void TERMINAL_MainWindow::closeSerialPort()
                                {
                                    if (m_serial->isOpen())
                                        m_serial->close();
                                    m_console->setEnabled(false);
                                    m_ui->actionConnect->setEnabled(true);
                                    m_ui->actionDisconnect->setEnabled(false);
                                    m_ui->actionConfigure->setEnabled(true);
                                    showStatusMessage(tr("Disconnected"));
                                }
                                //! [5]
                                
                                void TERMINAL_MainWindow::about()
                                {
                                    QMessageBox::about(this, tr("About Simple Terminal"),
                                                       tr("The <b>Simple Terminal</b> example demonstrates how to "
                                                          "use the Qt Serial Port module in modern GUI applications "
                                                          "using Qt, with a menu bar, toolbars, and a status bar."));
                                }
                                
                                //! [6]
                                void TERMINAL_MainWindow::writeData(const QByteArray &data)
                                {
                                    m_serial->write(data);
                                }
                                //! [6]
                                
                                //! [7]
                                void TERMINAL_MainWindow::readData()
                                {
                                    const QByteArray data = m_serial->readAll();
                                    m_console->putData(data);
                                }
                                //! [7]
                                
                                //! [8]
                                void TERMINAL_MainWindow::handleError(QSerialPort::SerialPortError error)
                                {
                                    if (error == QSerialPort::ResourceError) {
                                        QMessageBox::critical(this, tr("Critical Error"), m_serial->errorString());
                                        closeSerialPort();
                                    }
                                }
                                //! [8]
                                
                                void TERMINAL_MainWindow::initActionsConnections()
                                {
                                    connect(m_ui->actionConnect, &QAction::triggered, this, &TERMINAL_MainWindow::openSerialPort);
                                    connect(m_ui->actionDisconnect, &QAction::triggered, this, &TERMINAL_MainWindow::closeSerialPort);
                                    connect(m_ui->actionQuit, &QAction::triggered, this, &TERMINAL_MainWindow::close);
                                    connect(m_ui->actionConfigure, &QAction::triggered, m_settings, &SettingsDialog::show);
                                    connect(m_ui->actionClear, &QAction::triggered, m_console, &Console::clear);
                                    connect(m_ui->actionAbout, &QAction::triggered, this, &TERMINAL_MainWindow::about);
                                    connect(m_ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt);
                                }
                                
                                void TERMINAL_MainWindow::showStatusMessage(const QString &message)
                                {
                                    m_status->setText(message);
                                }
                                
                                
                                1 Reply Last reply
                                0
                                • Chris KawaC Offline
                                  Chris KawaC Offline
                                  Chris Kawa
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Like I said - open that generated ui_TERMINAL_mainwindow_copy.h and see what class name is there. There should be a class Ui_TERMINAL_MainWindow and below it, in a namespace Ui class TERMINAL_MainWindow : public Ui_TERMINAL_MainWindow.
                                  If any of those is still just MainWindow then you haven't done the renaming in the .ui file (maybe a typo, wrong place or anything like that).

                                  A 1 Reply Last reply
                                  0
                                  • Chris KawaC Chris Kawa

                                    Like I said - open that generated ui_TERMINAL_mainwindow_copy.h and see what class name is there. There should be a class Ui_TERMINAL_MainWindow and below it, in a namespace Ui class TERMINAL_MainWindow : public Ui_TERMINAL_MainWindow.
                                    If any of those is still just MainWindow then you haven't done the renaming in the .ui file (maybe a typo, wrong place or anything like that).

                                    A Offline
                                    A Offline
                                    Anonymous_Banned275
                                    wrote on last edited by Anonymous_Banned275
                                    #17

                                    @Chris-Kawa said in Need help with C++ syntax - repost:

                                    Like I said - open that generated ui_TERMINAL_mainwindow_copy.h and see what class name is there. There should be a class Ui_TERMINAL_MainWindow and below it, in a namespace Ui class TERMINAL_MainWindow : public Ui_TERMINAL_MainWindow.
                                    If any of those is still just MainWindow then you haven't done the renaming in the .ui file (maybe a typo, wrong place or anything like that).

                                    caorrect - this is the problem

                                    /********************************************************************************
                                    ** Form generated from reading UI file 'TERMINAL_mainwindow_copy.ui'
                                    **
                                    ** Created by: Qt User Interface Compiler version 5.15.2
                                    **
                                    ** WARNING! All changes made in this file will be lost when recompiling UI file!
                                    ********************************************************************************/
                                    
                                    #ifndef UI_TERMINAL_MAINWINDOW_COPY_H
                                    #define UI_TERMINAL_MAINWINDOW_COPY_H
                                    
                                    #include <QtCore/QVariant>
                                    #include <QtWidgets/QAction>
                                    #include <QtWidgets/QApplication>
                                    #include <QtWidgets/QMainWindow>
                                    #include <QtWidgets/QMenu>
                                    #include <QtWidgets/QMenuBar>
                                    #include <QtWidgets/QStatusBar>
                                    #include <QtWidgets/QToolBar>
                                    #include <QtWidgets/QVBoxLayout>
                                    #include <QtWidgets/QWidget>
                                    
                                    QT_BEGIN_NAMESPACE
                                    
                                    class Ui_MainWindow
                                    {
                                    public:
                                        QAction *actionAbout;
                                        QAction *actionAboutQt;
                                        QAction *actionConnect;
                                        QAction *actionDisconnect;
                                        QAction *actionConfigure;
                                        QAction *actionClear;
                                        QAction *actionQuit;
                                        QWidget *centralWidget;
                                        QVBoxLayout *verticalLayout;
                                        QMenuBar *menuBar;
                                        QMenu *menuCalls;
                                        QMenu *menuTools;
                                        QMenu *menuHelp;
                                        QToolBar *mainToolBar;
                                        QStatusBar *statusBar;
                                    
                                        void setupUi(QMainWindow *MainWindow)
                                        {
                                            if (MainWindow->objectName().isEmpty())
                                                MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
                                            MainWindow->resize(400, 300);
                                            actionAbout = new QAction(MainWindow);
                                            actionAbout->setObjectName(QString::fromUtf8("actionAbout"));
                                            actionAboutQt = new QAction(MainWindow);
                                            actionAboutQt->setObjectName(QString::fromUtf8("actionAboutQt"));
                                            actionConnect = new QAction(MainWindow);
                                            actionConnect->setObjectName(QString::fromUtf8("actionConnect"));
                                            QIcon icon;
                                            icon.addFile(QString::fromUtf8(":/images/connect.png"), QSize(), QIcon::Normal, QIcon::Off);
                                            actionConnect->setIcon(icon);
                                            actionDisconnect = new QAction(MainWindow);
                                            actionDisconnect->setObjectName(QString::fromUtf8("actionDisconnect"));
                                            QIcon icon1;
                                            icon1.addFile(QString::fromUtf8(":/images/disconnect.png"), QSize(), QIcon::Normal, QIcon::Off);
                                            actionDisconnect->setIcon(icon1);
                                            actionConfigure = new QAction(MainWindow);
                                            actionConfigure->setObjectName(QString::fromUtf8("actionConfigure"));
                                            QIcon icon2;
                                            icon2.addFile(QString::fromUtf8(":/images/settings.png"), QSize(), QIcon::Normal, QIcon::Off);
                                            actionConfigure->setIcon(icon2);
                                            actionClear = new QAction(MainWindow);
                                            actionClear->setObjectName(QString::fromUtf8("actionClear"));
                                            QIcon icon3;
                                            icon3.addFile(QString::fromUtf8(":/images/clear.png"), QSize(), QIcon::Normal, QIcon::Off);
                                            actionClear->setIcon(icon3);
                                            actionQuit = new QAction(MainWindow);
                                            actionQuit->setObjectName(QString::fromUtf8("actionQuit"));
                                            QIcon icon4;
                                            icon4.addFile(QString::fromUtf8(":/images/application-exit.png"), QSize(), QIcon::Normal, QIcon::Off);
                                            actionQuit->setIcon(icon4);
                                            centralWidget = new QWidget(MainWindow);
                                            centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
                                            verticalLayout = new QVBoxLayout(centralWidget);
                                            verticalLayout->setSpacing(6);
                                            verticalLayout->setContentsMargins(11, 11, 11, 11);
                                            verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
                                            MainWindow->setCentralWidget(centralWidget);
                                            menuBar = new QMenuBar(MainWindow);
                                            menuBar->setObjectName(QString::fromUtf8("menuBar"));
                                            menuBar->setGeometry(QRect(0, 0, 400, 19));
                                            menuCalls = new QMenu(menuBar);
                                            menuCalls->setObjectName(QString::fromUtf8("menuCalls"));
                                            menuTools = new QMenu(menuBar);
                                            menuTools->setObjectName(QString::fromUtf8("menuTools"));
                                            menuHelp = new QMenu(menuBar);
                                            menuHelp->setObjectName(QString::fromUtf8("menuHelp"));
                                            MainWindow->setMenuBar(menuBar);
                                            mainToolBar = new QToolBar(MainWindow);
                                            mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
                                            MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
                                            statusBar = new QStatusBar(MainWindow);
                                            statusBar->setObjectName(QString::fromUtf8("statusBar"));
                                            MainWindow->setStatusBar(statusBar);
                                    
                                            menuBar->addAction(menuCalls->menuAction());
                                            menuBar->addAction(menuTools->menuAction());
                                            menuBar->addAction(menuHelp->menuAction());
                                            menuCalls->addAction(actionConnect);
                                            menuCalls->addAction(actionDisconnect);
                                            menuCalls->addSeparator();
                                            menuCalls->addAction(actionQuit);
                                            menuTools->addAction(actionConfigure);
                                            menuTools->addAction(actionClear);
                                            menuHelp->addAction(actionAbout);
                                            menuHelp->addAction(actionAboutQt);
                                            mainToolBar->addAction(actionConnect);
                                            mainToolBar->addAction(actionDisconnect);
                                            mainToolBar->addAction(actionConfigure);
                                            mainToolBar->addAction(actionClear);
                                    
                                            retranslateUi(MainWindow);
                                    
                                            QMetaObject::connectSlotsByName(MainWindow);
                                        } // setupUi
                                    
                                        void retranslateUi(QMainWindow *MainWindow)
                                        {
                                            MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "Simple Terminal", nullptr));
                                            actionAbout->setText(QCoreApplication::translate("MainWindow", "&About", nullptr));
                                    #if QT_CONFIG(tooltip)
                                            actionAbout->setToolTip(QCoreApplication::translate("MainWindow", "About program", nullptr));
                                    #endif // QT_CONFIG(tooltip)
                                    #if QT_CONFIG(shortcut)
                                            actionAbout->setShortcut(QCoreApplication::translate("MainWindow", "Alt+A", nullptr));
                                    #endif // QT_CONFIG(shortcut)
                                            actionAboutQt->setText(QCoreApplication::translate("MainWindow", "About Qt", nullptr));
                                            actionConnect->setText(QCoreApplication::translate("MainWindow", "C&onnect", nullptr));
                                    #if QT_CONFIG(tooltip)
                                            actionConnect->setToolTip(QCoreApplication::translate("MainWindow", "Connect to serial port", nullptr));
                                    #endif // QT_CONFIG(tooltip)
                                    #if QT_CONFIG(shortcut)
                                            actionConnect->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+O", nullptr));
                                    #endif // QT_CONFIG(shortcut)
                                            actionDisconnect->setText(QCoreApplication::translate("MainWindow", "&Disconnect", nullptr));
                                    #if QT_CONFIG(tooltip)
                                            actionDisconnect->setToolTip(QCoreApplication::translate("MainWindow", "Disconnect from serial port", nullptr));
                                    #endif // QT_CONFIG(tooltip)
                                    #if QT_CONFIG(shortcut)
                                            actionDisconnect->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+D", nullptr));
                                    #endif // QT_CONFIG(shortcut)
                                            actionConfigure->setText(QCoreApplication::translate("MainWindow", "&Configure", nullptr));
                                    #if QT_CONFIG(tooltip)
                                            actionConfigure->setToolTip(QCoreApplication::translate("MainWindow", "Configure serial port", nullptr));
                                    #endif // QT_CONFIG(tooltip)
                                    #if QT_CONFIG(shortcut)
                                            actionConfigure->setShortcut(QCoreApplication::translate("MainWindow", "Alt+C", nullptr));
                                    #endif // QT_CONFIG(shortcut)
                                            actionClear->setText(QCoreApplication::translate("MainWindow", "C&lear", nullptr));
                                    #if QT_CONFIG(tooltip)
                                            actionClear->setToolTip(QCoreApplication::translate("MainWindow", "Clear data", nullptr));
                                    #endif // QT_CONFIG(tooltip)
                                    #if QT_CONFIG(shortcut)
                                            actionClear->setShortcut(QCoreApplication::translate("MainWindow", "Alt+L", nullptr));
                                    #endif // QT_CONFIG(shortcut)
                                            actionQuit->setText(QCoreApplication::translate("MainWindow", "&Quit", nullptr));
                                    #if QT_CONFIG(shortcut)
                                            actionQuit->setShortcut(QCoreApplication::translate("MainWindow", "Ctrl+Q", nullptr));
                                    #endif // QT_CONFIG(shortcut)
                                            menuCalls->setTitle(QCoreApplication::translate("MainWindow", "Calls", nullptr));
                                            menuTools->setTitle(QCoreApplication::translate("MainWindow", "Tools", nullptr));
                                            menuHelp->setTitle(QCoreApplication::translate("MainWindow", "Help", nullptr));
                                        } // retranslateUi
                                    
                                    };
                                    
                                    namespace Ui {
                                        class MainWindow: public Ui_MainWindow {};
                                    } // namespace Ui
                                    
                                    QT_END_NAMESPACE
                                    
                                    #endif // UI_TERMINAL_MAINWINDOW_COPY_H
                                    
                                    

                                    /********************************************************************************
                                    ** Form generated from reading UI file 'TERMINAL_mainwindow_copy.ui'
                                    **
                                    ** Created by: Qt User Interface Compiler version 5.15.2
                                    **
                                    ** WARNING! All changes made in this file will be lost when recompiling UI file!
                                    ********************************************************************************/

                                    Here is the issue - I have NEVER grasped the relations between .ui file and UI files .

                                    Here is the directory structure and the "form" has been renamed .

                                    cff7643c-ea64-4be9-8d54-8e5a3cce031b-image.png

                                    The (form) . ui has be renamed and the CREATED ui file still has the original class name ....

                                    So - where is the missing link /m source of error ?

                                    PS
                                    here is my terminal.pro

                                    QT += widgets serialport
                                    requires(qtConfig(combobox))
                                    
                                    TARGET = terminal
                                    #TEMPLATE = app
                                    TEMPLATE = lib
                                    SOURCES += \
                                        TERMINAL_mainwindow_copy.cpp \
                                        class_terminal.cpp \
                                        main.cpp \
                                    #    mainwindow.cpp \
                                        settingsdialog.cpp \
                                        console.cpp
                                    
                                    HEADERS += \
                                        TERMINAL_NOTES.h \
                                        TERMINAL_mainwindow_copy.h \
                                        class_terminal.h \
                                    #    mainwindow.h \
                                        settingsdialog.h \
                                        console.h
                                    
                                    FORMS += \
                                        TERMINAL_mainwindow_copy.ui \
                                     #   mainwindow.ui \
                                        settingsdialog.ui
                                    
                                    RESOURCES += \
                                        terminal.qrc
                                    
                                    target.path = $$[QT_INSTALL_EXAMPLES]/serialport/terminal
                                    INSTALLS += target
                                    
                                    
                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      Anonymous_Banned275
                                      wrote on last edited by
                                      #18

                                      PROGRESS (?) REPORT
                                      I have removed all the contents of the "generated" Ui_ file.
                                      After "rebuild" of the entire project - it fails same way .

                                      The "rebuild" file still DOES NOT reflect the renaming of the header file.

                                      I can try to find the ui_ file and actually delete it....

                                      or

                                      it looks there is something else missing in rebuilding the renamed header.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        Anonymous_Banned275
                                        wrote on last edited by
                                        #19

                                        SUCCESS SOLVED CASE CLOSED (?)

                                        51e1b75a-dfc2-4748-9669-76d5b1651aa4-image.png

                                        The moral of the story

                                        changing the name of the header file has no effect in changing the object name in the "form"

                                        80abd440-6029-415b-9d4f-ee10c2c720b5-image.png

                                        Thanks to all for much needed assistance , especially Chis.

                                        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