Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to modify the C code of MainWindow class ?
Forum Update on Monday, May 27th 2025

How to modify the C code of MainWindow class ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 337 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 Anonymous_Banned275
    #1

    ADDNDUM

    Question

    If I build an new class - using QTDesigner - can it reuse same ui form as the original QMainWindow class?
    I would actually build standard QT designer form class that change the ui.

    ` I am trying to use QT "terminal" example as a subwindow in MDI class.

    I do not know HOW to modify the existing class - from QMainWIndow to
    QWidget or QDialog

    I did try just to replace MainWindow and it was a disaster.

    I really need help with the actual C code .

    #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

    //! [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]
    {
    //! [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, &MainWindow::handleError);
    

    //! [2]
    connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
    //! [2]
    connect(m_console, &Console::getData, this, &MainWindow::writeData);
    //! [3]
    }
    //! [3]

    Pl45m4P 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      I don't see c code anywhere (only c++ code) and also no question from your side.
      Also you should be long enough here to know that you have to tag your code pieces so it gets readable...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • A Anonymous_Banned275

        ADDNDUM

        Question

        If I build an new class - using QTDesigner - can it reuse same ui form as the original QMainWindow class?
        I would actually build standard QT designer form class that change the ui.

        ` I am trying to use QT "terminal" example as a subwindow in MDI class.

        I do not know HOW to modify the existing class - from QMainWIndow to
        QWidget or QDialog

        I did try just to replace MainWindow and it was a disaster.

        I really need help with the actual C code .

        #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

        //! [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]
        {
        //! [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, &MainWindow::handleError);
        

        //! [2]
        connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
        //! [2]
        connect(m_console, &Console::getData, this, &MainWindow::writeData);
        //! [3]
        }
        //! [3]

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @AnneRanch said in How to modify the C code of MainWindow class ?:

        I do not know HOW to modify the existing class - from QMainWIndow to
        QWidget or QDialog

        Create a new QDialog class in Designer, copy/paste your code there and exchange QMainWindow with your new class. Probably you have to make some more changes in your logic, because a plain QWidget doesn't have a QStatusBar by default like QMainWindow does.
        Or you change your class in place including your ui file... But you can mess this up very easily, so that your designed widget isn't recognized by uic anymore.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        A 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @AnneRanch said in How to modify the C code of MainWindow class ?:

          I do not know HOW to modify the existing class - from QMainWIndow to
          QWidget or QDialog

          Create a new QDialog class in Designer, copy/paste your code there and exchange QMainWindow with your new class. Probably you have to make some more changes in your logic, because a plain QWidget doesn't have a QStatusBar by default like QMainWindow does.
          Or you change your class in place including your ui file... But you can mess this up very easily, so that your designed widget isn't recognized by uic anymore.

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

          @Pl45m4 I did try that already - did not work well since I was not sure what to copy...
          I am currently fixing my code so it compiles / runs again ....
          I am thinking of making a C_Terminal class and add current MainWindow to it as a member....
          Not sure if that is proper or what to call such convoluted setup.

          However, I just want to reuse exiting code as much as feasible.

          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