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 access UI elements of one widget class in another widget class
Forum Updated to NodeBB v4.3 + New Features

How to access UI elements of one widget class in another widget class

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 355 Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lukutis222
    wrote on last edited by lukutis222
    #1

    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:

    0c172157-c681-4ca8-a512-cdf380b2e927-image.png

    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?

    R 1 Reply Last reply
    0
    • L lukutis222

      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:

      0c172157-c681-4ca8-a512-cdf380b2e927-image.png

      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?

      R Offline
      R Offline
      Robert Hairgrove
      wrote on last edited by
      #2

      @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 your mainwindow.cpp file?
      (should be #include "ui_SettingsWindow.h"...)

      L 1 Reply Last reply
      0
      • R Robert Hairgrove

        @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 your mainwindow.cpp file?
        (should be #include "ui_SettingsWindow.h"...)

        L Offline
        L Offline
        lukutis222
        wrote on last edited by
        #3

        @Robert-Hairgrove
        That fixed the issue.. What a silly mistake! Thank you very much.

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @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);
              }
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          3
          • jsulmJ jsulm

            @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);
                }
            
            R Offline
            R Offline
            Robert Hairgrove
            wrote on last edited by
            #5

            @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 allow SettingsWindow to "pull" the necessary data from its parent. Of course, you would have to cast the QWidget* received in the constructor to a MainWindow* 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.

            jsulmJ 1 Reply Last reply
            0
            • R Robert Hairgrove

              @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 allow SettingsWindow to "pull" the necessary data from its parent. Of course, you would have to cast the QWidget* received in the constructor to a MainWindow* 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.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Robert-Hairgrove @lukutis222 Qt way is usually: use signals/slots

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved