Здравствуйте! Помогите разобраться, почему когда я передаю указатель, он пустой.
main.cpp
#include <QApplication>
#include "a.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
A a;
a.show();
return app.exec();
}
a.h
#ifndef A_H
#define A_H
#include <QDialog>
#include <QLineEdit>
class A : public QDialog
{
Q_OBJECT
public:
A(QDialog *parent = 0);
void getData(QString, QString, QString);
private slots:
void start();
private:
QLineEdit *edit1;
QLineEdit *edit2;
QLineEdit *edit3;
};
#endif // A_H
a.cpp
#include "a.h"
#include "b.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QDebug>
A::A(QDialog *parent) : QDialog(parent)
{
QVBoxLayout *first = new QVBoxLayout;
edit1 = new QLineEdit;
edit2 = new QLineEdit;
edit3 = new QLineEdit;
QPushButton *button = new QPushButton("Start");
first->addWidget(edit1);
first->addWidget(edit2);
first->addWidget(edit3);
first->addWidget(button);
setLayout(first);
setWindowTitle("Class A");
connect(button, SIGNAL(clicked()), this, SLOT(start()));
}
void A::start()
{
B *b = new B(this);
hide();
b->show();
}
void A::getData(QString str1, QString str2, QString str3)
{
str1 = edit1->text();
str2 = edit2->text();
str3 = edit3->text();
qDebug() << str1 << str2 << str3;
}
b.h
#ifndef B_H
#define B_H
#include <QMainWindow>
#include "a.h"
class B : public QMainWindow
{
Q_OBJECT
public:
B(A *parent);
private:
A *a;
};
#endif // B_H
b.cpp
#include "b.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QDebug>
B::B(A *parent) : QMainWindow(parent)//, a(*parent)
{
//a = parent;
QString str1, str2, str3;
parent->getData(str1, str2, str3);
qDebug() << str1 << str2 << str3;
QWidget *window = new QWidget;
QVBoxLayout *second = new QVBoxLayout;
QLabel *title = new QLabel("This is class B!");
QLabel *label1 = new QLabel(str1);
QLabel *label2 = new QLabel(str2);
QLabel *label3 = new QLabel(str3);
second->addWidget(title);
second->addWidget(label1);
second->addWidget(label2);
second->addWidget(label3);
window->setLayout(second);
window->setWindowTitle("Class B");
}
Меня также смущает, что когда всплывает второе окно, оно пустое, хотя должен быть заголовок и текстовая метка "This is class B!".
main.cpp
создаю окно A. При клике на кнопку в окне А создается второе окноB *b = new B(this);
. Указатель нужен, чтобы значение из QLineEdit'ов окна A передались в QLabel'ы окна B. При инициализации окна B пытаюсь обратиться к родителю:parent->getData(str1, str2, str3);
, чтобы потом использовать эти значения.