How to modify the C code of MainWindow class ?
-
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 QDialogI 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_OBJECTpublic:
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] -
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... -
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 QDialogI 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_OBJECTpublic:
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]@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 QDialogCreate a new
QDialogclass in Designer, copy/paste your code there and exchangeQMainWindowwith your new class. Probably you have to make some more changes in your logic, because a plainQWidgetdoesn't have aQStatusBarby default likeQMainWindowdoes.
Or you change your class in place including youruifile... But you can mess this up very easily, so that your designed widget isn't recognized byuicanymore. -
@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 QDialogCreate a new
QDialogclass in Designer, copy/paste your code there and exchangeQMainWindowwith your new class. Probably you have to make some more changes in your logic, because a plainQWidgetdoesn't have aQStatusBarby default likeQMainWindowdoes.
Or you change your class in place including youruifile... But you can mess this up very easily, so that your designed widget isn't recognized byuicanymore.@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.