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. Static struct member declaration
Forum Updated to NodeBB v4.3 + New Features

Static struct member declaration

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

    Hello. From what I understand, static members declared within class will share their value within different class objects. I would like to learn more about this and implement this myself. I have a class called settingswindow:

    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;
        };
    
        static Settings m_currentSettings;
        static Settings m_currentSettings2;
    
        explicit SettingsWindow(QWidget *parent = nullptr);
        ~SettingsWindow();
    
        Settings settings() const;
        Settings settings2() const;
    
    private slots:
        void showPortInfo(int idx);
        void showPortInfo2(int idx);
        void apply();
    
    
    
    
    
    
        void on_serialPortInfoListBox_highlighted(int index);
    
    
    
    private:
    
        void fillPortsParameters();
        void fillPortsParameters2();
        void fillPortsInfo2();
        void fillPortsInfo();
        void updateSettings();
        void updateSettings2();
    
    
        void rectangle_test();
    
    private:
    
        //Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it
    
    
    };
    
    #endif // SETTINGSDIALOG_H
    
    

    settingswindow.cpp:

    
    
    #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)
    
    {
    
    
    
    
        //add the following line:
    
    
    
        //TestTool testtool_class;
        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);
    
        connect(m_ui->serialPortInfoListBox_2, &QComboBox::currentIndexChanged,
                this, &SettingsWindow::showPortInfo2);
    
    
    
    
    
        fillPortsParameters();
        fillPortsInfo();
    
        fillPortsParameters2();
        fillPortsInfo2();
    
        updateSettings();
        updateSettings2();
    
    }
    
    SettingsWindow::~SettingsWindow()
    {
        delete m_ui;
    }
    
    SettingsWindow::Settings SettingsWindow::settings() const
    {
        return m_currentSettings;
    }
    
    
    SettingsWindow::Settings SettingsWindow::settings2() const
    {
        return m_currentSettings2;
    }
    
    
    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::showPortInfo2(int idx)
    {
        if (idx == -1)
            return;
    
        const QStringList list = m_ui->serialPortInfoListBox_2->itemData(idx).toStringList();
        m_ui->descriptionLabel_2->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString)));
        m_ui->manufacturerLabel_2->setText(tr("Manufacturer: %1").arg(list.count() > 2 ? list.at(2) : tr(blankString)));
        m_ui->serialNumberLabel_2->setText(tr("Serial number: %1").arg(list.count() > 3 ? list.at(3) : tr(blankString)));
        m_ui->locationLabel_2->setText(tr("Location: %1").arg(list.count() > 4 ? list.at(4) : tr(blankString)));
        m_ui->vidLabel_2->setText(tr("Vendor Identifier: %1").arg(list.count() > 5 ? list.at(5) : tr(blankString)));
        m_ui->pidLabel_2->setText(tr("Product Identifier: %1").arg(list.count() > 6 ? list.at(6) : tr(blankString)));
    }
    
    void SettingsWindow::apply()
    {
        updateSettings();
        updateSettings2();
        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::fillPortsParameters2()
    {
        m_ui->baudRateBox_2->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
        m_ui->baudRateBox_2->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
        m_ui->baudRateBox_2->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
        m_ui->baudRateBox_2->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
        m_ui->baudRateBox_2->addItem(tr("Custom"));
    
        m_ui->dataBitsBox_2->addItem(QStringLiteral("5"), QSerialPort::Data5);
        m_ui->dataBitsBox_2->addItem(QStringLiteral("6"), QSerialPort::Data6);
        m_ui->dataBitsBox_2->addItem(QStringLiteral("7"), QSerialPort::Data7);
        m_ui->dataBitsBox_2->addItem(QStringLiteral("8"), QSerialPort::Data8);
        m_ui->dataBitsBox_2->setCurrentIndex(3);
    
        m_ui->parityBox_2->addItem(tr("None"), QSerialPort::NoParity);
        m_ui->parityBox_2->addItem(tr("Even"), QSerialPort::EvenParity);
        m_ui->parityBox_2->addItem(tr("Odd"), QSerialPort::OddParity);
        m_ui->parityBox_2->addItem(tr("Mark"), QSerialPort::MarkParity);
        m_ui->parityBox_2->addItem(tr("Space"), QSerialPort::SpaceParity);
    
        m_ui->stopBitsBox_2->addItem(QStringLiteral("1"), QSerialPort::OneStop);
    #ifdef Q_OS_WIN
        m_ui->stopBitsBox_2->addItem(tr("1.5"), QSerialPort::OneAndHalfStop);
    #endif
        m_ui->stopBitsBox_2->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
    
        m_ui->flowControlBox_2->addItem(tr("None"), QSerialPort::NoFlowControl);
        m_ui->flowControlBox_2->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
        m_ui->flowControlBox_2->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::fillPortsInfo2()
    {
        m_ui->serialPortInfoListBox_2->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_2->addItem(list.first(), list);
        }
    
        m_ui->serialPortInfoListBox_2->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();
    }
    
    
    
    void SettingsWindow::updateSettings2()
    {
        m_currentSettings2.name = m_ui->serialPortInfoListBox_2->currentText();
    
    
        if (m_ui->baudRateBox_2->currentIndex() == 4) {
            m_currentSettings2.baudRate = m_ui->baudRateBox_2->currentText().toInt();
        } else {
            m_currentSettings2.baudRate = static_cast<QSerialPort::BaudRate>(
                        m_ui->baudRateBox_2->itemData(m_ui->baudRateBox_2->currentIndex()).toInt());
        }
        m_currentSettings2.stringBaudRate = QString::number(m_currentSettings2.baudRate);
    
        m_currentSettings2.dataBits = static_cast<QSerialPort::DataBits>(
                    m_ui->dataBitsBox_2->itemData(m_ui->dataBitsBox_2->currentIndex()).toInt());
        m_currentSettings2.stringDataBits = m_ui->dataBitsBox_2->currentText();
    
        m_currentSettings2.parity = static_cast<QSerialPort::Parity>(
                    m_ui->parityBox_2->itemData(m_ui->parityBox_2->currentIndex()).toInt());
        m_currentSettings2.stringParity = m_ui->parityBox_2->currentText();
    
        m_currentSettings2.stopBits = static_cast<QSerialPort::StopBits>(
                    m_ui->stopBitsBox_2->itemData(m_ui->stopBitsBox_2->currentIndex()).toInt());
        m_currentSettings2.stringStopBits = m_ui->stopBitsBox_2->currentText();
    
        m_currentSettings2.flowControl = static_cast<QSerialPort::FlowControl>(
                    m_ui->flowControlBox_2->itemData(m_ui->flowControlBox_2->currentIndex()).toInt());
        m_currentSettings2.stringFlowControl = m_ui->flowControlBox_2->currentText();
    
        m_currentSettings2.localEchoEnabled = m_ui->localEchoCheckBox_2->isChecked();
        m_currentSettings2.log_to_file = m_ui->log_to_file_checkbox_2->isChecked();
        m_currentSettings2.hex_encode = m_ui->hex_encode_checkbox_2->isChecked();
    }
    
    
    
    
    
    
    
    
    
    void SettingsWindow::on_serialPortInfoListBox_highlighted(int index)
    {
        qDebug("highligted \n");
        //fillPortsInfo();
        //fillPortsInfo2();
    }
    
    
    
    
    
    

    As you can see from the settingswindow.h I have a struct called Settings:

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

    I would like to understand more about how to create a static struct member. I tried to do:

        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;
        };
    
        static Settings m_currentSettings;
        static Settings m_currentSettings2;
    

    But I get the following error:
    8409292a-d648-4df5-960a-0417fe977bfe-image.png

    Could someone help me understand how to properly create static struct? Thanks in advance..

    JonBJ 1 Reply Last reply
    0
    • L lukutis222

      Hello. From what I understand, static members declared within class will share their value within different class objects. I would like to learn more about this and implement this myself. I have a class called settingswindow:

      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;
          };
      
          static Settings m_currentSettings;
          static Settings m_currentSettings2;
      
          explicit SettingsWindow(QWidget *parent = nullptr);
          ~SettingsWindow();
      
          Settings settings() const;
          Settings settings2() const;
      
      private slots:
          void showPortInfo(int idx);
          void showPortInfo2(int idx);
          void apply();
      
      
      
      
      
      
          void on_serialPortInfoListBox_highlighted(int index);
      
      
      
      private:
      
          void fillPortsParameters();
          void fillPortsParameters2();
          void fillPortsInfo2();
          void fillPortsInfo();
          void updateSettings();
          void updateSettings2();
      
      
          void rectangle_test();
      
      private:
      
          //Ui::SettingsWindow *m_ui = nullptr; //declared public so testool can reach it
      
      
      };
      
      #endif // SETTINGSDIALOG_H
      
      

      settingswindow.cpp:

      
      
      #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)
      
      {
      
      
      
      
          //add the following line:
      
      
      
          //TestTool testtool_class;
          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);
      
          connect(m_ui->serialPortInfoListBox_2, &QComboBox::currentIndexChanged,
                  this, &SettingsWindow::showPortInfo2);
      
      
      
      
      
          fillPortsParameters();
          fillPortsInfo();
      
          fillPortsParameters2();
          fillPortsInfo2();
      
          updateSettings();
          updateSettings2();
      
      }
      
      SettingsWindow::~SettingsWindow()
      {
          delete m_ui;
      }
      
      SettingsWindow::Settings SettingsWindow::settings() const
      {
          return m_currentSettings;
      }
      
      
      SettingsWindow::Settings SettingsWindow::settings2() const
      {
          return m_currentSettings2;
      }
      
      
      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::showPortInfo2(int idx)
      {
          if (idx == -1)
              return;
      
          const QStringList list = m_ui->serialPortInfoListBox_2->itemData(idx).toStringList();
          m_ui->descriptionLabel_2->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString)));
          m_ui->manufacturerLabel_2->setText(tr("Manufacturer: %1").arg(list.count() > 2 ? list.at(2) : tr(blankString)));
          m_ui->serialNumberLabel_2->setText(tr("Serial number: %1").arg(list.count() > 3 ? list.at(3) : tr(blankString)));
          m_ui->locationLabel_2->setText(tr("Location: %1").arg(list.count() > 4 ? list.at(4) : tr(blankString)));
          m_ui->vidLabel_2->setText(tr("Vendor Identifier: %1").arg(list.count() > 5 ? list.at(5) : tr(blankString)));
          m_ui->pidLabel_2->setText(tr("Product Identifier: %1").arg(list.count() > 6 ? list.at(6) : tr(blankString)));
      }
      
      void SettingsWindow::apply()
      {
          updateSettings();
          updateSettings2();
          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::fillPortsParameters2()
      {
          m_ui->baudRateBox_2->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
          m_ui->baudRateBox_2->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
          m_ui->baudRateBox_2->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
          m_ui->baudRateBox_2->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
          m_ui->baudRateBox_2->addItem(tr("Custom"));
      
          m_ui->dataBitsBox_2->addItem(QStringLiteral("5"), QSerialPort::Data5);
          m_ui->dataBitsBox_2->addItem(QStringLiteral("6"), QSerialPort::Data6);
          m_ui->dataBitsBox_2->addItem(QStringLiteral("7"), QSerialPort::Data7);
          m_ui->dataBitsBox_2->addItem(QStringLiteral("8"), QSerialPort::Data8);
          m_ui->dataBitsBox_2->setCurrentIndex(3);
      
          m_ui->parityBox_2->addItem(tr("None"), QSerialPort::NoParity);
          m_ui->parityBox_2->addItem(tr("Even"), QSerialPort::EvenParity);
          m_ui->parityBox_2->addItem(tr("Odd"), QSerialPort::OddParity);
          m_ui->parityBox_2->addItem(tr("Mark"), QSerialPort::MarkParity);
          m_ui->parityBox_2->addItem(tr("Space"), QSerialPort::SpaceParity);
      
          m_ui->stopBitsBox_2->addItem(QStringLiteral("1"), QSerialPort::OneStop);
      #ifdef Q_OS_WIN
          m_ui->stopBitsBox_2->addItem(tr("1.5"), QSerialPort::OneAndHalfStop);
      #endif
          m_ui->stopBitsBox_2->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
      
          m_ui->flowControlBox_2->addItem(tr("None"), QSerialPort::NoFlowControl);
          m_ui->flowControlBox_2->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
          m_ui->flowControlBox_2->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::fillPortsInfo2()
      {
          m_ui->serialPortInfoListBox_2->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_2->addItem(list.first(), list);
          }
      
          m_ui->serialPortInfoListBox_2->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();
      }
      
      
      
      void SettingsWindow::updateSettings2()
      {
          m_currentSettings2.name = m_ui->serialPortInfoListBox_2->currentText();
      
      
          if (m_ui->baudRateBox_2->currentIndex() == 4) {
              m_currentSettings2.baudRate = m_ui->baudRateBox_2->currentText().toInt();
          } else {
              m_currentSettings2.baudRate = static_cast<QSerialPort::BaudRate>(
                          m_ui->baudRateBox_2->itemData(m_ui->baudRateBox_2->currentIndex()).toInt());
          }
          m_currentSettings2.stringBaudRate = QString::number(m_currentSettings2.baudRate);
      
          m_currentSettings2.dataBits = static_cast<QSerialPort::DataBits>(
                      m_ui->dataBitsBox_2->itemData(m_ui->dataBitsBox_2->currentIndex()).toInt());
          m_currentSettings2.stringDataBits = m_ui->dataBitsBox_2->currentText();
      
          m_currentSettings2.parity = static_cast<QSerialPort::Parity>(
                      m_ui->parityBox_2->itemData(m_ui->parityBox_2->currentIndex()).toInt());
          m_currentSettings2.stringParity = m_ui->parityBox_2->currentText();
      
          m_currentSettings2.stopBits = static_cast<QSerialPort::StopBits>(
                      m_ui->stopBitsBox_2->itemData(m_ui->stopBitsBox_2->currentIndex()).toInt());
          m_currentSettings2.stringStopBits = m_ui->stopBitsBox_2->currentText();
      
          m_currentSettings2.flowControl = static_cast<QSerialPort::FlowControl>(
                      m_ui->flowControlBox_2->itemData(m_ui->flowControlBox_2->currentIndex()).toInt());
          m_currentSettings2.stringFlowControl = m_ui->flowControlBox_2->currentText();
      
          m_currentSettings2.localEchoEnabled = m_ui->localEchoCheckBox_2->isChecked();
          m_currentSettings2.log_to_file = m_ui->log_to_file_checkbox_2->isChecked();
          m_currentSettings2.hex_encode = m_ui->hex_encode_checkbox_2->isChecked();
      }
      
      
      
      
      
      
      
      
      
      void SettingsWindow::on_serialPortInfoListBox_highlighted(int index)
      {
          qDebug("highligted \n");
          //fillPortsInfo();
          //fillPortsInfo2();
      }
      
      
      
      
      
      

      As you can see from the settingswindow.h I have a struct called Settings:

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

      I would like to understand more about how to create a static struct member. I tried to do:

          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;
          };
      
          static Settings m_currentSettings;
          static Settings m_currentSettings2;
      

      But I get the following error:
      8409292a-d648-4df5-960a-0417fe977bfe-image.png

      Could someone help me understand how to properly create static struct? Thanks in advance..

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @lukutis222

          static Settings m_currentSettings;
          static Settings m_currentSettings2;
      

      These are static global variables. The linker error indicates you have tried to access these (somewhere not shown?) via Settings::m_currentSettings. That would be a static member variable inside class Settings, which they are not.

      What do you intend? You have two static Settings struct variables, that is not the problem. Do you wish these to be global scope variables or struct scope (really just namespace) variables? Or even static Settings variables inside some other class?

      BTW. static is not always the best choice these days. It has various gotchas/restrictions. For example, yours would work, but if you decided to store any type derived from QObject inside it it would not. Often people use singleton objects for this.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lukutis222
        wrote on last edited by lukutis222
        #3

        @JonB
        Thanks for response.

        What I was trying to achieve is quite simple. I have class SettingsWindow and I create multiple objects:

        SettingsWindow settings_obj1;
        SettingsWindow settings_obj2;
        

        I want both object to share same Settings struct (m_currentSettings and m_currentSettings2). If I change some Settings on one object, the other will change as well. I thought that static struct declaration would be good use for this.

        JonBJ 2 Replies Last reply
        0
        • L lukutis222

          @JonB
          Thanks for response.

          What I was trying to achieve is quite simple. I have class SettingsWindow and I create multiple objects:

          SettingsWindow settings_obj1;
          SettingsWindow settings_obj2;
          

          I want both object to share same Settings struct (m_currentSettings and m_currentSettings2). If I change some Settings on one object, the other will change as well. I thought that static struct declaration would be good use for this.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @lukutis222
          One possibility would be to create a single Settings instance and maintain a pointer to it in both SettingsWindow objects. Or the static you want. I believe the problem with that at present --- from the error message in the screenshot you posted --- is that your static Settings m_currentSettings defines a global object, outside of any class, while you have somewhere (elsewhere) in your code where you are trying to access it via Settings::m_currentSettings. That would be wrong and lead to the linker error you show. So do you have somewhere in code where you try to access that m_currentSettings object, what does that look like?

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

            If your last peice of code in your first post is from the class definition of SettingsWindow then you simply forgot to define your two member variables seomwhere since you only declared them in the header.
            No idea why they need to be static though...

            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
            1
            • L lukutis222

              @JonB
              Thanks for response.

              What I was trying to achieve is quite simple. I have class SettingsWindow and I create multiple objects:

              SettingsWindow settings_obj1;
              SettingsWindow settings_obj2;
              

              I want both object to share same Settings struct (m_currentSettings and m_currentSettings2). If I change some Settings on one object, the other will change as well. I thought that static struct declaration would be good use for this.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @lukutis222
              Further to @Christian-Ehrlicher's response I now see that you seem to have:

              class SettingsWindow : public QDialog
              {
                  ...
                  static Settings m_currentSettings;
                  static Settings m_currentSettings2;
              

              In that case you will need to put into the .cpp file (somewhere at top-level scope, near the beginning but after #include "settingswindow.h"):

              // in next two lines you may need `SettingsWindow::Settings` in place of plain `Settings`
              static Settings SettingsWindow::m_currentSettings;
              static Settings SettingsWindow::m_currentSettings2;
              

              to define storage for these variables, and thereby get rid of the linker error.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                C++ is stupid enough that you have to declare static vars two times :)

                Christian EhrlicherC 1 Reply Last reply
                0
                • M mpergand

                  C++ is stupid enough that you have to declare static vars two times :)

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @mpergand said in Static struct member declaration:

                  C++ is stupid enough that you have to declare static vars two times :)

                  No, you have to declare it once and also define it once.
                  static pod members can be declared and defined in one place nowadays.

                  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
                  0

                  • Login

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