Created a project that consists of two windows: in the first window we fill in variables, in the second we display the filled variables. In one executable file (dialog.cpp), I take variables and set parameters for them, take them from the header file (peremennie1.h). After filling them in, I want to output them in an executable file (mainwindow.cpp), BUT this does not happen, they are completely zeroed 0_o.

What am I doing wrong ????

// -----------------------------------------------

Here is the whole code, it is very simple there are three buttons and a couple of lines of code, the main thing for me is to understand the mechanism.

File welik_oshibka.pro

QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = welik_oshibka TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ dialog.cpp \ peremennie1.cpp HEADERS += mainwindow.h \ dialog.h \ peremennie1.h FORMS += mainwindow.ui \ dialog.ui 

Peremennie1.h file

  #ifndef PEREMENNIE1_H #define PEREMENNIE1_H class peremennie1 { public: // peremennie1(); explicit peremennie1(); virtual ~peremennie1(); int WRE ; int WRE_PR; int wre_izL; double WRE_DABL; int *K2I_IZ; }; #endif // PEREMENNIE1_H 

peremennie1.cpp

  #include "peremennie1.h" peremennie1::peremennie1() { WRE = 0; WRE_PR = 0; wre_izL = 0; WRE_DABL = 0; // массивчик K2I_IZ=0; } peremennie1::~peremennie1() { if(K2I_IZ) delete []K2I_IZ; } 

Dialog.h file

 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); signals: void signal_dialog_show(); private: Ui::Dialog *ui; }; #endif // DIALOG_H 

Dialog.cpp file

 #include "dialog.h" #include "ui_dialog.h" #include "peremennie1.h" #include "QDebug" peremennie1 per; Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_pushButton_clicked() { qDebug() << "per.WRE = " << per.WRE; qDebug() << "per.WRE_PR =" << per.WRE_PR; qDebug() << "per.wre_izL =" << per.wre_izL; qDebug() << "per.WRE_DABL =" << per.WRE_DABL; emit signal_dialog_show(); this->hide(); } void Dialog::on_pushButton_2_clicked() { // тут заполняем данные per.WRE = 25; per.WRE_PR = 26; per.wre_izL = 27; per.WRE_DABL = 28.55; ui->label->setText("zapolnili"); } 

Main.cpp file

 #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; // w.show(); return a.exec(); } 

Mainwindows.h file

  #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void select_peremennie1(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H 

Mainwindows.cpp file

 #include "mainwindow.h" #include "ui_mainwindow.h" #include "dialog.h" #include "peremennie1.h" #include "QDebug" Dialog *glsso1_d = 0; peremennie1 *per = new peremennie1; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // this->hide(); // Инициализировать диалоги выбора режима и типа антенны glsso1_d = new Dialog(this); glsso1_d->show(); qDebug() << "!!!per->WRE = " << per->WRE; qDebug() << "!!!per->WRE_PR =" << per->WRE_PR; qDebug() << "!!!per->wre_izL =" << per->wre_izL; qDebug() << "!!!per->WRE_DABL =" << per->WRE_DABL; // штука что бы прыгать между кнопками connect(glsso1_d, SIGNAL(signal_dialog_show()), this, SLOT(select_peremennie1())); } // выбирается режим в котором будет работать основной блок программы void MainWindow::select_peremennie1() { this->show(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { // возвращаем диалог glsso1_d->show(); this->hide(); } void MainWindow::on_pushButton_2_clicked() { ui->spinBox->setValue(per->WRE); ui->spinBox_2->setValue(per->WRE_PR); ui->spinBox_3->setValue(per->wre_izL); ui->doubleSpinBox->setValue(per->WRE_DABL); } void MainWindow::on_pushButton_3_clicked() { qDebug() << "!!!+_+!!!per->WRE = " << per->WRE; qDebug() << "!!!+_+!!!per->WRE_PR =" << per->WRE_PR; qDebug() << "!!!+_+!!!per->wre_izL =" << per->wre_izL; qDebug() << "!!!+_+!!!per->WRE_DABL =" << per->WRE_DABL; } 

Dialog.ui file

 <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Dialog</class> <widget class="QDialog" name="Dialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>305</width> <height>352</height> </rect> </property> <property name="windowTitle"> <string>Dialog</string> </property> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>80</x> <y>60</y> <width>111</width> <height>41</height> </rect> </property> <property name="text"> <string>задаем данные</string> </property> </widget> <widget class="QPushButton" name="pushButton_2"> <property name="geometry"> <rect> <x>80</x> <y>150</y> <width>111</width> <height>71</height> </rect> </property> <property name="text"> <string>заполняем данные</string> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>90</x> <y>260</y> <width>111</width> <height>31</height> </rect> </property> <property name="text"> <string/> </property> </widget> </widget> <resources/> <connections/> </ui> 

Mainwindows.ui file

 <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>40</x> <y>20</y> <width>101</width> <height>51</height> </rect> </property> <property name="text"> <string>возвращение</string> </property> </widget> <widget class="QPushButton" name="pushButton_2"> <property name="geometry"> <rect> <x>40</x> <y>110</y> <width>75</width> <height>23</height> </rect> </property> <property name="text"> <string>вывод</string> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>160</x> <y>100</y> <width>61</width> <height>21</height> </rect> </property> <property name="text"> <string>WRE</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>160</x> <y>130</y> <width>71</width> <height>21</height> </rect> </property> <property name="text"> <string>WRE_PR</string> </property> </widget> <widget class="QLabel" name="label_3"> <property name="geometry"> <rect> <x>160</x> <y>160</y> <width>61</width> <height>21</height> </rect> </property> <property name="text"> <string>WRE_izl</string> </property> </widget> <widget class="QLabel" name="label_4"> <property name="geometry"> <rect> <x>160</x> <y>190</y> <width>71</width> <height>21</height> </rect> </property> <property name="text"> <string>WRE_DABL</string> </property> </widget> <widget class="QSpinBox" name="spinBox"> <property name="enabled"> <bool>false</bool> </property> <property name="geometry"> <rect> <x>250</x> <y>100</y> <width>42</width> <height>22</height> </rect> </property> </widget> <widget class="QSpinBox" name="spinBox_2"> <property name="enabled"> <bool>false</bool> </property> <property name="geometry"> <rect> <x>250</x> <y>130</y> <width>42</width> <height>22</height> </rect> </property> </widget> <widget class="QSpinBox" name="spinBox_3"> <property name="enabled"> <bool>false</bool> </property> <property name="geometry"> <rect> <x>250</x> <y>160</y> <width>42</width> <height>22</height> </rect> </property> </widget> <widget class="QDoubleSpinBox" name="doubleSpinBox"> <property name="enabled"> <bool>false</bool> </property> <property name="geometry"> <rect> <x>240</x> <y>190</y> <width>62</width> <height>22</height> </rect> </property> </widget> <widget class="QPushButton" name="pushButton_3"> <property name="geometry"> <rect> <x>40</x> <y>150</y> <width>75</width> <height>23</height> </rect> </property> <property name="text"> <string>разведка</string> </property> </widget> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>21</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui> 

    3 answers 3

    When you include a header file, this is, if roughly, a simple substitution of the entire file into the place where the include directive is written. That is, included in two places, in two places you had the string int var; . To use the same variable in several places, you need to declare it in one place as usual ( int var; ), and in others extern int var; Something like this.

    • Do not be offended at me please, but this is not the answer. You did not say where to insert this code and how to insert it. For example, to build a ship for flights between the stars, you need to install a nuclear star engine there. Just wherever I tried to shove it extern everywhere he gave me an error - timob256

    In different translation units (read .cpp files) you work with different instance variables of the peremennie1 class.

    In dialog.cpp you define one global variable:

     peremennie1 per; 

    and in mainwindows.cpp - the second (and for some reason you place it in the "heap"):

     peremennie1 *per = new peremennie1; 

    Define this variable somewhere in one place and access it from different objects.

    For example, define your variable in the function main and pass it to your MainWindow object:

     int main(int argc, char *argv[]) { QApplication a(argc, argv); peremennie1 per; MainWindow w; w.setContext(&per); w.show(); return a.exec(); } 

    Define the setContext(peremennie1* per) method setContext(peremennie1* per) and the field peremennie1* m_per in the MainWindow class:

     class Mainwindow ... { private: peremennie1* m_per = nullptr; ... public: void setContext(peremennie1* per) { m_per = per; } }; 

    Further from the MainWindow methods, access this variable via the local m_per pointer.

    You can pass this variable to your Dialog class in a similar way.

    • aleks. How would I understand what you are saying but I do not understand how to do it. - timob256

    aleks.andr and Alexey Sarovsky thank you for your answers. But I still give a detailed answer.

    in peremennie1.h

      #ifndef PEREMENNIE1_H #define PEREMENNIE1_H class peremennie1 { public: // peremennie1(); explicit peremennie1(); virtual ~peremennie1(); int WRE ; int WRE_PR; int wre_izL; double WRE_DABL; int *K2I_IZ; }; extern peremennie1 per; 

    In the dialog.cpp file, replace the code (with this: 3)

     #include "dialog.h" #include "ui_dialog.h" #include "peremennie1.h" #include "mainwindow.h" #include "QDebug" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_pushButton_clicked() { qDebug() << "per.WRE = " << per.WRE; qDebug() << "per.WRE_PR =" << per.WRE_PR; qDebug() << "per.wre_izL =" << per.wre_izL; qDebug() << "per.WRE_DABL =" << per.WRE_DABL; emit signal_dialog_show(); this->hide(); } void Dialog::on_pushButton_2_clicked() { // тут заполняем данные per.WRE = 25; per.WRE_PR = 26; per.wre_izL = 27; per.WRE_DABL = 28.55; ui->label->setText("zapolnili"); } 

    In the mainwindow.cpp file

    delete the line

     peremennie1 *per = new peremennie1; 

    And replace the strings with the call per-> WRE with this variant per.WRE.

    Example: It was

     void MainWindow::on_pushButton_2_clicked() { ui->spinBox->setValue(per->WRE); ui->spinBox_2->setValue(per->WRE_PR); ui->spinBox_3->setValue(per->wre_izL); ui->doubleSpinBox->setValue(per->WRE_DABL); } void MainWindow::on_pushButton_3_clicked() { qDebug() << "!!!+_+!!!per->WRE = " << per->WRE; qDebug() << "!!!+_+!!!per->WRE_PR =" << per->WRE_PR; qDebug() << "!!!+_+!!!per->wre_izL =" << per->wre_izL; qDebug() << "!!!+_+!!!per->WRE_DABL =" << per->WRE_DABL; } 

    It became

     void MainWindow::on_pushButton_2_clicked() { ui->spinBox->setValue(per.WRE); ui->spinBox_2->setValue(per.WRE_PR); ui->spinBox_3->setValue(per.wre_izL); ui->doubleSpinBox->setValue(per.WRE_DABL); } void MainWindow::on_pushButton_3_clicked() { qDebug() << "!!!+_+!!!per.WRE = " << per.WRE; qDebug() << "!!!+_+!!!per.WRE_PR =" << per.WRE_PR; qDebug() << "!!!+_+!!!per.wre_izL =" << per.wre_izL; qDebug() << "!!!+_+!!!per.WRE_DABL =" << per.WRE_DABL; }