简体   繁体   English

在两个QWidget实例之间共享数据

[英]Share data between two QWidget instances

I would like share a string between two instances of QWidget. 我想在两个QWidget实例之间共享一个字符串。

在此处输入图片说明

In main.cpp, two objects are instantiated and shown like this: 在main.cpp中,实例化了两个对象,如下所示:

#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();
}

I would introduce SharedState class: 我将介绍SharedState类:

// 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的构造函数中提供对SharedState的引用,

// 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的构造函数中,我必须将初始值设置为textEdit,并将信号连接到插槽。

// 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功能:

// 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM