【问题标题】:Qt5 getting access to data in one form from anotherQt5以一种形式从另一种形式访问数据
【发布时间】:2018-05-06 11:54:10
【问题描述】:

如何以一种形式从另一种形式访问数据?

我有两种形式: 主要形式:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "manualform.h"
#include "key.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

Key cryptKey;

void MainWindow::on_autoKeyBtn_clicked()
{
    cryptKey.createAuto();
    QString output = cryptKey.toStrg();
    ui->keyField->setText(output);
}

void MainWindow::on_manualKeyBtn_clicked()
{
    ManualForm form;
    form.setModal(true);
    form.exec();
} 

第二个:

#include "manualform.h"
#include "ui_manualform.h"
#include "key.h"

ManualForm::ManualForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ManualForm)
{
    ui->setupUi(this);
}

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

Key key;

void ManualForm::on_confirmBtn_clicked()
{
    this->close();    
}

void ManualForm::on_resetBtn_clicked()
{

}

void ManualForm::on_checkBox00_toggled(bool checked)
{
        Coord coord(0,0);
        ui->checkBox09->setDisabled(checked);
        ui->checkBox99->setDisabled(checked);
        ui->checkBox90->setDisabled(checked);
        key.add(coord);
}

假设 Key 对象将在 ManualForm 中创建并传输到 MainWindow 或 ManualForm 将访问 MainWindow 的 cryptKey。但这是一个我无法解决的问题。

【问题讨论】:

    标签: c++ qt data-transfer


    【解决方案1】:

    您可以在堆上创建cryptKey,然后使用signals and slots 将其传递给您的新表单。此外,您可以使用QPointer 进行保护,以防您的其他表单删除该对象。

    您必须在 MainWindow 中定义一个信号,在 ManualForm 和 cryptKey 中定义一个插槽,最好作为 ManualForm 中的类对象。然后您使用emit 将对象发送到您的ManualForm。您可能还必须使用qRegisterMetaType 来注册对象。

    【讨论】:

    • Key 对象是 myClass 对象的 可能有什么问题吗?
    • 您不应将本地对象从 MainWindow 传递到 ManualForm。如果要修改 Coord 并从 ManualForm 发出,解决方案是在 MainWindow 中创建一个插槽。
    • 从堆中删除 Key 对象怎么样?使用“delete cryptKey;”时出现“C2059:语法错误:'delete'”
    • delete cryptKey; 在析构函数中应该没问题。或者您将其分配为父级(this)。语法错误意味着您可能拼错了一些东西,
    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多