How to access UI elements of one widget class in another widget class
-
Hello. I have my MainWindow component:
MainWindow::MainWindow(Serial* serial_ptr,Logging* logging_ptr,TestTool* testtool_ptr,SettingsWindow* settings_ptr,QWidget *parent) : QMainWindow{parent}, ui(new Ui::MainWindow) { settings_local = settings_ptr; serial_local = serial_ptr; logging_local = logging_ptr; testtool_local = testtool_ptr; ui->setupUi(this); QFont font("Courier New"); QApplication::setFont(font); ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40); border:none}"); ui->Console_read->setStyleSheet("QTextEdit { background-color: rgb(11, 33, 40); color : white; border: 1px solid white;}"); //fillPortsParameters(); connect(ui->actionClear, &QAction::triggered, ui->Console_read, &QTextEdit::clear); // show settings window when actionconfigure clicked connect(ui->actionConnect, &QAction::triggered, this, &MainWindow::openSerialPort); // connect when clicked connect connect(ui->actionDisconnect, &QAction::triggered, this, &MainWindow::closeSerialPort); // connect when clicked connect connect(ui->actionConfigure, &QAction::triggered, settings_local, &SettingsWindow::show); // show settings window when actionconfigure clicked connect(&serial_local->serial_connection, &QSerialPort::readyRead, this, &MainWindow::readData); }
MainWindow class constructor takes other class objects as a parameter:
explicit MainWindow(Serial* serial_ptr,Logging* logging_ptr,TestTool* testtool_ptr,SettingsWindow* settings_local = nullptr, QWidget *parent = nullptr);
Notice that it takes SettingsWindow as a parameter.
In my main.cpp I call:Serial s; Logging l; SettingsWindow set; TestTool t(&s); MainWindow w(&s,&l,&t,&set);
My SettingsWindow.cpp is as following:
#include "settingswindow.h" #include "ui_settingswindow.h" #include <QLineEdit> #include <QSerialPortInfo> static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A"); SettingsWindow::SettingsWindow(QWidget *parent) : QDialog(parent), m_ui(new Ui::SettingsWindow) { m_ui->setupUi(this); // ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40); border:none}"); this->setStyleSheet( "QDialog{background-color: rgb(25, 33, 40);}"); m_ui->descriptionLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white; }"); m_ui->baudRateLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white; }"); m_ui->flowControlLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white; }"); m_ui->locationLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->dataBitsLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->stopBitsLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->manufacturerLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->parityLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->pidLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->serialNumberLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->vidLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->localEchoCheckBox->setStyleSheet("QCheckBox { background-color: rgb(11, 33, 40); color : white;}"); m_ui->log_to_file_checkbox->setStyleSheet("QCheckBox { background-color: rgb(11, 33, 40); color : white; }"); m_ui->hex_encode_checkbox->setStyleSheet("QCheckBox { background-color: rgb(11, 33, 40); color : white; }"); m_ui->selectBox->setStyleSheet("QGroupBox::title {color : white;}"); m_ui->parametersBox->setStyleSheet("QGroupBox::title {color : white;}"); m_ui->additionalOptionsGroupBox->setStyleSheet("QGroupBox::title {color : white;}"); //ui->Console_read2->setStyleSheet("QTextEdit { background-color: rgb(11, 33, 40); color : white; border: 1px solid white;}"); m_ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert); connect(m_ui->applyButton, &QPushButton::clicked, this, &SettingsWindow::apply); connect(m_ui->serialPortInfoListBox, &QComboBox::currentIndexChanged, this, &SettingsWindow::showPortInfo); fillPortsParameters(); fillPortsInfo(); updateSettings(); } SettingsWindow::~SettingsWindow() { delete m_ui; } SettingsWindow::Settings SettingsWindow::settings() const { return m_currentSettings; } void SettingsWindow::showPortInfo(int idx) { if (idx == -1) return; const QStringList list = m_ui->serialPortInfoListBox->itemData(idx).toStringList(); m_ui->descriptionLabel->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString))); m_ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.count() > 2 ? list.at(2) : tr(blankString))); m_ui->serialNumberLabel->setText(tr("Serial number: %1").arg(list.count() > 3 ? list.at(3) : tr(blankString))); m_ui->locationLabel->setText(tr("Location: %1").arg(list.count() > 4 ? list.at(4) : tr(blankString))); m_ui->vidLabel->setText(tr("Vendor Identifier: %1").arg(list.count() > 5 ? list.at(5) : tr(blankString))); m_ui->pidLabel->setText(tr("Product Identifier: %1").arg(list.count() > 6 ? list.at(6) : tr(blankString))); } void SettingsWindow::apply() { updateSettings(); hide(); } void SettingsWindow::fillPortsParameters() { m_ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600); m_ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200); m_ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400); m_ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200); m_ui->baudRateBox->addItem(tr("Custom")); m_ui->dataBitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5); m_ui->dataBitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6); m_ui->dataBitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7); m_ui->dataBitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8); m_ui->dataBitsBox->setCurrentIndex(3); m_ui->parityBox->addItem(tr("None"), QSerialPort::NoParity); m_ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity); m_ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity); m_ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity); m_ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity); m_ui->stopBitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop); #ifdef Q_OS_WIN m_ui->stopBitsBox->addItem(tr("1.5"), QSerialPort::OneAndHalfStop); #endif m_ui->stopBitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop); m_ui->flowControlBox->addItem(tr("None"), QSerialPort::NoFlowControl); m_ui->flowControlBox->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl); m_ui->flowControlBox->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl); } void SettingsWindow::fillPortsInfo() { m_ui->serialPortInfoListBox->clear(); QString description; QString manufacturer; QString serialNumber; const auto infos = QSerialPortInfo::availablePorts(); for (const QSerialPortInfo &info : infos) { QStringList list; description = info.description(); manufacturer = info.manufacturer(); serialNumber = info.serialNumber(); list << info.portName() << (!description.isEmpty() ? description : blankString) << (!manufacturer.isEmpty() ? manufacturer : blankString) << (!serialNumber.isEmpty() ? serialNumber : blankString) << info.systemLocation() << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString) << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString); m_ui->serialPortInfoListBox->addItem(list.first(), list); } m_ui->serialPortInfoListBox->addItem(tr("Custom")); } void SettingsWindow::updateSettings() { m_currentSettings.name = m_ui->serialPortInfoListBox->currentText(); if (m_ui->baudRateBox->currentIndex() == 4) { m_currentSettings.baudRate = m_ui->baudRateBox->currentText().toInt(); } else { m_currentSettings.baudRate = static_cast<QSerialPort::BaudRate>( m_ui->baudRateBox->itemData(m_ui->baudRateBox->currentIndex()).toInt()); } m_currentSettings.stringBaudRate = QString::number(m_currentSettings.baudRate); m_currentSettings.dataBits = static_cast<QSerialPort::DataBits>( m_ui->dataBitsBox->itemData(m_ui->dataBitsBox->currentIndex()).toInt()); m_currentSettings.stringDataBits = m_ui->dataBitsBox->currentText(); m_currentSettings.parity = static_cast<QSerialPort::Parity>( m_ui->parityBox->itemData(m_ui->parityBox->currentIndex()).toInt()); m_currentSettings.stringParity = m_ui->parityBox->currentText(); m_currentSettings.stopBits = static_cast<QSerialPort::StopBits>( m_ui->stopBitsBox->itemData(m_ui->stopBitsBox->currentIndex()).toInt()); m_currentSettings.stringStopBits = m_ui->stopBitsBox->currentText(); m_currentSettings.flowControl = static_cast<QSerialPort::FlowControl>( m_ui->flowControlBox->itemData(m_ui->flowControlBox->currentIndex()).toInt()); m_currentSettings.stringFlowControl = m_ui->flowControlBox->currentText(); m_currentSettings.localEchoEnabled = m_ui->localEchoCheckBox->isChecked(); m_currentSettings.log_to_file = m_ui->log_to_file_checkbox->isChecked(); m_currentSettings.hex_encode = m_ui->hex_encode_checkbox->isChecked(); }
and SettingsWindow.h
#ifndef SETTINGSWINDOW_H #define SETTINGSWINDOW_H #include <QDialog> #include <QSerialPort> QT_BEGIN_NAMESPACE namespace Ui { class SettingsWindow; } QT_END_NAMESPACE class SettingsWindow : public QDialog { Q_OBJECT public: Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it struct Settings { QString name; qint32 baudRate; QString stringBaudRate; QSerialPort::DataBits dataBits; QString stringDataBits; QSerialPort::Parity parity; QString stringParity; QSerialPort::StopBits stopBits; QString stringStopBits; QSerialPort::FlowControl flowControl; QString stringFlowControl; bool localEchoEnabled; bool log_to_file; bool hex_encode; }; explicit SettingsWindow(QWidget *parent = nullptr); ~SettingsWindow(); Settings settings() const; private slots: void showPortInfo(int idx); void apply(); private: void fillPortsParameters(); void fillPortsInfo(); void updateSettings(); private: //Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it Settings m_currentSettings; }; #endif // SETTINGSDIALOG_H
As you can see
Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it
Is declared as public.
therefore, in my mainwindow.cpp I try to access SettingsWindow UI elements:
I am quite new to C++ and classes. Could someone point out what could be an issue and what can I do to access the widgets?
-
Hello. I have my MainWindow component:
MainWindow::MainWindow(Serial* serial_ptr,Logging* logging_ptr,TestTool* testtool_ptr,SettingsWindow* settings_ptr,QWidget *parent) : QMainWindow{parent}, ui(new Ui::MainWindow) { settings_local = settings_ptr; serial_local = serial_ptr; logging_local = logging_ptr; testtool_local = testtool_ptr; ui->setupUi(this); QFont font("Courier New"); QApplication::setFont(font); ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40); border:none}"); ui->Console_read->setStyleSheet("QTextEdit { background-color: rgb(11, 33, 40); color : white; border: 1px solid white;}"); //fillPortsParameters(); connect(ui->actionClear, &QAction::triggered, ui->Console_read, &QTextEdit::clear); // show settings window when actionconfigure clicked connect(ui->actionConnect, &QAction::triggered, this, &MainWindow::openSerialPort); // connect when clicked connect connect(ui->actionDisconnect, &QAction::triggered, this, &MainWindow::closeSerialPort); // connect when clicked connect connect(ui->actionConfigure, &QAction::triggered, settings_local, &SettingsWindow::show); // show settings window when actionconfigure clicked connect(&serial_local->serial_connection, &QSerialPort::readyRead, this, &MainWindow::readData); }
MainWindow class constructor takes other class objects as a parameter:
explicit MainWindow(Serial* serial_ptr,Logging* logging_ptr,TestTool* testtool_ptr,SettingsWindow* settings_local = nullptr, QWidget *parent = nullptr);
Notice that it takes SettingsWindow as a parameter.
In my main.cpp I call:Serial s; Logging l; SettingsWindow set; TestTool t(&s); MainWindow w(&s,&l,&t,&set);
My SettingsWindow.cpp is as following:
#include "settingswindow.h" #include "ui_settingswindow.h" #include <QLineEdit> #include <QSerialPortInfo> static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A"); SettingsWindow::SettingsWindow(QWidget *parent) : QDialog(parent), m_ui(new Ui::SettingsWindow) { m_ui->setupUi(this); // ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40); border:none}"); this->setStyleSheet( "QDialog{background-color: rgb(25, 33, 40);}"); m_ui->descriptionLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white; }"); m_ui->baudRateLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white; }"); m_ui->flowControlLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white; }"); m_ui->locationLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->dataBitsLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->stopBitsLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->manufacturerLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->parityLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->pidLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->serialNumberLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->vidLabel->setStyleSheet("QLabel { background-color: rgb(11, 33, 40); color : white;}"); m_ui->localEchoCheckBox->setStyleSheet("QCheckBox { background-color: rgb(11, 33, 40); color : white;}"); m_ui->log_to_file_checkbox->setStyleSheet("QCheckBox { background-color: rgb(11, 33, 40); color : white; }"); m_ui->hex_encode_checkbox->setStyleSheet("QCheckBox { background-color: rgb(11, 33, 40); color : white; }"); m_ui->selectBox->setStyleSheet("QGroupBox::title {color : white;}"); m_ui->parametersBox->setStyleSheet("QGroupBox::title {color : white;}"); m_ui->additionalOptionsGroupBox->setStyleSheet("QGroupBox::title {color : white;}"); //ui->Console_read2->setStyleSheet("QTextEdit { background-color: rgb(11, 33, 40); color : white; border: 1px solid white;}"); m_ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert); connect(m_ui->applyButton, &QPushButton::clicked, this, &SettingsWindow::apply); connect(m_ui->serialPortInfoListBox, &QComboBox::currentIndexChanged, this, &SettingsWindow::showPortInfo); fillPortsParameters(); fillPortsInfo(); updateSettings(); } SettingsWindow::~SettingsWindow() { delete m_ui; } SettingsWindow::Settings SettingsWindow::settings() const { return m_currentSettings; } void SettingsWindow::showPortInfo(int idx) { if (idx == -1) return; const QStringList list = m_ui->serialPortInfoListBox->itemData(idx).toStringList(); m_ui->descriptionLabel->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString))); m_ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.count() > 2 ? list.at(2) : tr(blankString))); m_ui->serialNumberLabel->setText(tr("Serial number: %1").arg(list.count() > 3 ? list.at(3) : tr(blankString))); m_ui->locationLabel->setText(tr("Location: %1").arg(list.count() > 4 ? list.at(4) : tr(blankString))); m_ui->vidLabel->setText(tr("Vendor Identifier: %1").arg(list.count() > 5 ? list.at(5) : tr(blankString))); m_ui->pidLabel->setText(tr("Product Identifier: %1").arg(list.count() > 6 ? list.at(6) : tr(blankString))); } void SettingsWindow::apply() { updateSettings(); hide(); } void SettingsWindow::fillPortsParameters() { m_ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600); m_ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200); m_ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400); m_ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200); m_ui->baudRateBox->addItem(tr("Custom")); m_ui->dataBitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5); m_ui->dataBitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6); m_ui->dataBitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7); m_ui->dataBitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8); m_ui->dataBitsBox->setCurrentIndex(3); m_ui->parityBox->addItem(tr("None"), QSerialPort::NoParity); m_ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity); m_ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity); m_ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity); m_ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity); m_ui->stopBitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop); #ifdef Q_OS_WIN m_ui->stopBitsBox->addItem(tr("1.5"), QSerialPort::OneAndHalfStop); #endif m_ui->stopBitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop); m_ui->flowControlBox->addItem(tr("None"), QSerialPort::NoFlowControl); m_ui->flowControlBox->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl); m_ui->flowControlBox->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl); } void SettingsWindow::fillPortsInfo() { m_ui->serialPortInfoListBox->clear(); QString description; QString manufacturer; QString serialNumber; const auto infos = QSerialPortInfo::availablePorts(); for (const QSerialPortInfo &info : infos) { QStringList list; description = info.description(); manufacturer = info.manufacturer(); serialNumber = info.serialNumber(); list << info.portName() << (!description.isEmpty() ? description : blankString) << (!manufacturer.isEmpty() ? manufacturer : blankString) << (!serialNumber.isEmpty() ? serialNumber : blankString) << info.systemLocation() << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString) << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString); m_ui->serialPortInfoListBox->addItem(list.first(), list); } m_ui->serialPortInfoListBox->addItem(tr("Custom")); } void SettingsWindow::updateSettings() { m_currentSettings.name = m_ui->serialPortInfoListBox->currentText(); if (m_ui->baudRateBox->currentIndex() == 4) { m_currentSettings.baudRate = m_ui->baudRateBox->currentText().toInt(); } else { m_currentSettings.baudRate = static_cast<QSerialPort::BaudRate>( m_ui->baudRateBox->itemData(m_ui->baudRateBox->currentIndex()).toInt()); } m_currentSettings.stringBaudRate = QString::number(m_currentSettings.baudRate); m_currentSettings.dataBits = static_cast<QSerialPort::DataBits>( m_ui->dataBitsBox->itemData(m_ui->dataBitsBox->currentIndex()).toInt()); m_currentSettings.stringDataBits = m_ui->dataBitsBox->currentText(); m_currentSettings.parity = static_cast<QSerialPort::Parity>( m_ui->parityBox->itemData(m_ui->parityBox->currentIndex()).toInt()); m_currentSettings.stringParity = m_ui->parityBox->currentText(); m_currentSettings.stopBits = static_cast<QSerialPort::StopBits>( m_ui->stopBitsBox->itemData(m_ui->stopBitsBox->currentIndex()).toInt()); m_currentSettings.stringStopBits = m_ui->stopBitsBox->currentText(); m_currentSettings.flowControl = static_cast<QSerialPort::FlowControl>( m_ui->flowControlBox->itemData(m_ui->flowControlBox->currentIndex()).toInt()); m_currentSettings.stringFlowControl = m_ui->flowControlBox->currentText(); m_currentSettings.localEchoEnabled = m_ui->localEchoCheckBox->isChecked(); m_currentSettings.log_to_file = m_ui->log_to_file_checkbox->isChecked(); m_currentSettings.hex_encode = m_ui->hex_encode_checkbox->isChecked(); }
and SettingsWindow.h
#ifndef SETTINGSWINDOW_H #define SETTINGSWINDOW_H #include <QDialog> #include <QSerialPort> QT_BEGIN_NAMESPACE namespace Ui { class SettingsWindow; } QT_END_NAMESPACE class SettingsWindow : public QDialog { Q_OBJECT public: Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it struct Settings { QString name; qint32 baudRate; QString stringBaudRate; QSerialPort::DataBits dataBits; QString stringDataBits; QSerialPort::Parity parity; QString stringParity; QSerialPort::StopBits stopBits; QString stringStopBits; QSerialPort::FlowControl flowControl; QString stringFlowControl; bool localEchoEnabled; bool log_to_file; bool hex_encode; }; explicit SettingsWindow(QWidget *parent = nullptr); ~SettingsWindow(); Settings settings() const; private slots: void showPortInfo(int idx); void apply(); private: void fillPortsParameters(); void fillPortsInfo(); void updateSettings(); private: //Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it Settings m_currentSettings; }; #endif // SETTINGSDIALOG_H
As you can see
Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it
Is declared as public.
therefore, in my mainwindow.cpp I try to access SettingsWindow UI elements:
I am quite new to C++ and classes. Could someone point out what could be an issue and what can I do to access the widgets?
@lukutis222 said in How to access UI elements of one widget class in another widget class:
therefore, in my mainwindow.cpp I try to access SettingsWindow UI elements:
Did you include the header file for the
Ui::SettingsWindow
class in yourmainwindow.cpp
file?
(should be#include "ui_SettingsWindow.h"
...) -
@lukutis222 said in How to access UI elements of one widget class in another widget class:
therefore, in my mainwindow.cpp I try to access SettingsWindow UI elements:
Did you include the header file for the
Ui::SettingsWindow
class in yourmainwindow.cpp
file?
(should be#include "ui_SettingsWindow.h"
...)@Robert-Hairgrove
That fixed the issue.. What a silly mistake! Thank you very much. -
@lukutis222 said in How to access UI elements of one widget class in another widget class:
Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it
WHY?!
Other widgets should not know anything about internal implementation details of a widget (or any other class)! This is called encapsulation. What you are doing is very bad design!
Instead of such ugly hacks you should implement public methods in MainWindow, so other classes can communicate with it without knowing anything about its internal implementation detals...
Example:class MainWindow : public QMainWindow { Q_OBJECT public: void setSomeText(const QString &text) { ui->some_label->setText(text); }
-
@lukutis222 said in How to access UI elements of one widget class in another widget class:
Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it
WHY?!
Other widgets should not know anything about internal implementation details of a widget (or any other class)! This is called encapsulation. What you are doing is very bad design!
Instead of such ugly hacks you should implement public methods in MainWindow, so other classes can communicate with it without knowing anything about its internal implementation detals...
Example:class MainWindow : public QMainWindow { Q_OBJECT public: void setSomeText(const QString &text) { ui->some_label->setText(text); }
@jsulm said in How to access UI elements of one widget class in another widget class:
Other widgets should not know anything about internal implementation details of a widget (or any other class)! This is called encapsulation.
Yes, this is definitely a better design.
Another idea might be to add functions to
MainWindow
that allowSettingsWindow
to "pull" the necessary data from its parent. Of course, you would have to cast theQWidget*
received in the constructor to aMainWindow*
in order to do it that way. This can come in handy when several different classes need access to the same data, and only the parent class can provide it. -
@jsulm said in How to access UI elements of one widget class in another widget class:
Other widgets should not know anything about internal implementation details of a widget (or any other class)! This is called encapsulation.
Yes, this is definitely a better design.
Another idea might be to add functions to
MainWindow
that allowSettingsWindow
to "pull" the necessary data from its parent. Of course, you would have to cast theQWidget*
received in the constructor to aMainWindow*
in order to do it that way. This can come in handy when several different classes need access to the same data, and only the parent class can provide it.@Robert-Hairgrove @lukutis222 Qt way is usually: use signals/slots