Hello! Help me figure out why when I pass a pointer, it's empty.
main.cpp
#include <QApplication> #include "ah" int main(int argc, char *argv[]) { QApplication app(argc, argv); A a; a.show(); return app.exec(); }
ah
#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 "ah" #include "bh" #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; }
bh
#ifndef B_H #define B_H #include <QMainWindow> #include "ah" class B : public QMainWindow { Q_OBJECT public: B(A *parent); private: A *a; }; #endif // B_H
b.cpp
#include "bh" #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"); }
I am also confused that when the second window pops up, it is empty, although there should be a title and a text label "This is class B!".
main.cpp
create a window A. When I click on a button in window A, a second window is createdB *b = new B(this);
. The pointer is needed so that the value from QLineEdit of window A is transferred to QLabel of window B. When initializing window B, I try to contact the parent:parent->getData(str1, str2, str3);
to use these values later. - Cirran