user2666834
user2666834

Reputation: 31

Share data between two QWidget instances

I would like share a string between two instances of QWidget.

enter image description here

In main.cpp, two objects are instantiated and shown like this:

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

Dialog w1,w2; //Derived from QWidget

w1.show();

w2.show();

return a.exec();
}

Upvotes: 0

Views: 111

Answers (1)

luantkow
luantkow

Reputation: 2959

I would introduce SharedState class:

// shared_state.h
#ifndef SHARED_STATE_HPP
#define SHARED_STATE_HPP

#include <QObject>

class SharedState : public QObject
{
    Q_OBJECT
public:

    SharedState(QString initialValue = "")
        : currentValue(initialValue)
    {}

    QString getCurrentValue()
    {
         return currentValue;
    }

public slots:
    void setValue(QString newValue)
    {
        if(currentValue != newValue)
        {
            currentValue = newValue;
            emit valueChanged(currentValue);
        }
    }

signals:
    void valueChanged(QString);

private:
    QString currentValue;
};

#endif // SHARED_STATE_HPP

Now I would provide reference to SharedState in Dialog's constructor,

// dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QWidget>
#include "shared_state.h"

namespace Ui {
class Dialog;
}

class Dialog : public QWidget
{
    Q_OBJECT

public:
    explicit Dialog(SharedState& state, QWidget *parent = 0);
    ~Dialog();

private slots:
    void handleTextEdited(const QString&);

public slots:
    void handleInternalStateChanged(QString);

private:
    Ui::Dialog *ui;

    SharedState& state;
};

#endif // DIALOG_H

You may have noticed that I have added two slots, one to handle the case when the text is manually edited and one when shared state will inform us that we are out of date.

Now in Dialog's constructor I had to set initial value to textEdit, and connect signals to slots.

// dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(SharedState& state, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Dialog),
    state(state)
{
    ui->setupUi(this);
    ui->textEdit->setText(state.getCurrentValue());

    QObject::connect(ui->textEdit, SIGNAL(textEdited(QString)),
             this, SLOT(handleTextEdited(QString)));
    QObject::connect(&state, SIGNAL(valueChanged(QString)),
             this, SLOT(handleInternalStateChanged(QString)));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::handleTextEdited(const QString& newText)
{
    state.setValue(newText);
}

void Dialog::handleInternalStateChanged(QString newState)
{
    ui->textEdit->setText(newState);
}

Now the change in the main function:

// main.cpp
#include "dialog.h"
#include "shared_state.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    SharedState state("Initial Value");
    Dialog w1(state), w2(state);
    w1.show();
    w2.show();

    return a.exec();
}

Upvotes: 2

Related Questions