Как получить QString из одного окна в другое, нажав кнопку в третьем окне - PullRequest
0 голосов
/ 28 октября 2018

Новичок в C ++ и Qt как часть исследовательского проекта (биология) и, по-видимому, боролся с некоторыми довольно простыми вещами. Я действительно ценю чью-то помощь.

Я работаю с графическим интерфейсом для уже существующей программы и пытаюсь передать переменную QString из QLineEdit одного из окон (inputform) в QLineEdit второго окна (выходной формы).

Я застрял в том, что мне нужна форма вывода, с предварительно заполненным LineEdit, когда я нажимаю кнопку в третьем окне (filedialog).

Проблема:

  • При запуске -> появляются два окна: filedialog и inputform.
  • Пользователь вводит данные в QLineEdit формы ввода
  • Пользователь нажимает кнопку TransferButton в окне диалога
  • При нажатии кнопки -> появляется форма вывода, с QLineEdit, предварительно заполненным данными пользователя (из формы ввода).

Я предполагаю, что проблема связана с разновидностью getter / setter, и моя переменная, вероятно, выходит из области видимости, но я пробовал использовать множество похожих примеров, но не могу заставить ее работать.

Спасибо заранее.

Вот мой код:

main.cpp

#include "filedialog.h"
#include "inputform.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FileDialog w;
    InputForm w2;
    w.show();
    w2.show();


    return a.exec();
}

filedialog.h

#ifndef FILEDIALOG_H
#define FILEDIALOG_H

#include <QDialog>

namespace Ui {
class FileDialog;
}

class FileDialog : public QDialog
{
    Q_OBJECT

public:
    explicit FileDialog(QWidget *parent = nullptr);
    ~FileDialog();
    void setFileName();
    QString getFileName();

private slots:
    void on_transferButton_clicked();

private:
    Ui::FileDialog *ui;
    QString fileName;
};

#endif // FILEDIALOG_H

filedialog.ccp

#include "filedialog.h"
#include "ui_filedialog.h"
#include "inputform.h"
#include "ui_inputform.h"
#include "outputform.h"
#include "ui_outputform.h"

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

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

void FileDialog::setFileName()
{
    InputForm *inputform = new InputForm;
    fileName = inputform->ui->inputLineEdit->text();
}

QString FileDialog::getFileName()
{
    return fileName;
}

void FileDialog::on_transferButton_clicked()
{
    setFileName();
    OutPutForm *outputform = new OutPutForm;
    outputform->ui->outputLineEdit->setText(getFileName());
    outputform->show();
}

inputform.h

#ifndef INPUTFORM_H
#define INPUTFORM_H

#include <QWidget>

namespace Ui {
class InputForm;
}

class InputForm : public QWidget
{
    Q_OBJECT

public:
    explicit InputForm(QWidget *parent = nullptr);
    ~InputForm();
    Ui::InputForm *ui;

};

#endif // INPUTFORM_H

inputform.ccp

#include "inputform.h"
#include "ui_inputform.h"

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

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

outputform.h

#ifndef OUTPUTFORM_H
#define OUTPUTFORM_H

#include <QWidget>

namespace Ui {
class OutPutForm;
}

class OutPutForm : public QWidget
{
    Q_OBJECT

public:
    explicit OutPutForm(QWidget *parent = nullptr);
    ~OutPutForm();
    Ui::OutPutForm *ui;

};

#endif // OUTPUTFORM_H

outputform.ccp

#include "outputform.h"
#include "ui_outputform.h"

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

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

1 Ответ

0 голосов
/ 30 октября 2018

Спасибо за ваш краткий указатель.

После некоторой тренировки:

Настройка главного окна (или в моем случае главного диалогового окна). Создайте экземпляр формы ввода, подключите кнопку к форме ввода.

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

    InputForm *inputForm = new InputForm;
    connect(ui->transferButton,SIGNAL(clicked()),inputForm,SLOT(getLineEditTextFunc()));
    inputForm->show();
}

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

void FileDialog::on_transferButton_clicked()
{

}

Затем из формы ввода:

Определить функцию для получения текста LineEdit формы ввода (fileName); а затем также сгенерируйте выходную форму и заполните ее LineEdit переменной fileName.

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

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

void InputForm::getLineEditTextFunc()
{
    fileName = this->ui->inputLineEdit->text();
    OutPutForm *outputform = new OutPutForm;
    outputform->ui->outputLineEdit->setText(fileName);
    outputform->show();
}
...