Common variables

hi friends,
I amusing Qt Creature and I have two form ,this two form must use common variables that I must access these variables.I will get a value from outside in these vaariables in any form.I need like global values.
Hello ZekDe,

A global variable would work, but should be avoided. It is better to define your variable in main and pass the needed variable(s) to the functions that need them. If you need to change the value you could either return this new value to to the variable or pass the variable by reference, so that the change is seen in main.

Passing the variable(s) to a function will also help you figure out which function may be causing a problem.

Hope that helps,

Andy
It’s hard to give a complete answer with so little information.
You could try by using the Qt signal-slot mechanism.

This is a basic example:
main.cpp:
1
2
3
4
5
6
7
8
9
#include "basic.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Basic b;
    return a.exec();
}


basic.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef BASIC_H
#define BASIC_H

#include "form1.h"
#include "form2.h"
#include <QWidget>


// To use connect, a class must inherit from QObject

class Basic : public QWidget
{
    Q_OBJECT
public:
    explicit Basic(QWidget *parent = nullptr);
    Form1 f1;
    Form2 f2;

private:
};

#endif // BASIC_H 


basic.cpp:
1
2
3
4
5
6
7
8
9
#include "basic.h"

Basic::Basic(QWidget *parent) : QWidget(parent)
{
    connect( &f1, &f1.sendQteContent,
             &f2, &f2.displayText      );
    f1.move(-5, -5);
    f1.show();
}


form1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef FORM1_H
#define FORM1_H

#include <QDialog>
#include <QPushButton>
#include <QString>
#include <QTextEdit>
#include <QVBoxLayout>

class Form1 : public QDialog
{
    Q_OBJECT
public:
    explicit Form1(QWidget *parent = nullptr);
    QTextEdit* qte;
    QPushButton* qpb;
    QVBoxLayout* qvbl;

signals:
    void sendQteContent(const QString& s);

public slots:
    // When button pressed... (send QEditText content)
    void buttonPressed();
};

#endif // FORM1_H 


form1.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "form1.h"

Form1::Form1(QWidget *parent) : QDialog(parent)
{
    qte = new QTextEdit("Write your text here");
    qpb = new QPushButton("Ok");
    qvbl = new(QVBoxLayout);
    qvbl->addWidget(qte);
    qvbl->addWidget(qpb);
    setLayout(qvbl);

    connect( qpb,  QPushButton::pressed,
             this, buttonPressed          );
}

void Form1::buttonPressed()
{
    emit sendQteContent(qte->toPlainText());
}


form2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef FORM2_H
#define FORM2_H

#include <QDialog>
#include <QLabel>
#include <QString>
#include <QVBoxLayout>

class Form2 : public QDialog
{
    Q_OBJECT
public:
    explicit Form2(QWidget *parent = nullptr);
    void setLabelContent(const QString& s);  // QString suggested

private:
    QLabel* ql;
    QVBoxLayout* qvbl;

signals:

public slots:
    void displayText(const QString& s) { setLabelContent(s); }
};

#endif // FORM2_H 


form2.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "form2.h"

Form2::Form2(QWidget *parent) : QDialog(parent)
{
    ql = new QLabel();
    qvbl = new(QVBoxLayout);
    qvbl->addWidget(ql);
    setLayout(qvbl);
}

void Form2::setLabelContent(const QString& s)
{
    ql->setText(s);
    show();
}

Registered users can post here. Sign in or register to post.