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!".

  • Where, where and what pointer do you pass? A simple copy-paste of the code does not add to the probability of getting a sensible answer without explanatory comments from the author of the question. - alexis031182
  • In main.cpp create a window A. When I click on a button in window A, a second window is created B *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

2 answers 2

You have quite a few problems in the code. From what is seen in the unpretentious consideration.

First of all, this is the lack of indication of parents for widgets in class B The fact that you add them to the placement manager does not mean that they will have a parent, which in turn hints at a potential memory leak.

Your class B inherited from QMainWindow , which in use is slightly different from the dialog or even just the widget. QMainWindow has a special setCentralWidget() method. This is the method you need to use to place your QWidget *window in the main window. Otherwise, you will see an empty QMainWindow window.

The A::getData(QString, QString, QString) declared incorrectly. The arguments must be references or pointers to variable strings, otherwise you will not get the desired values ​​for them when the execution context leaves this method. Simply put, replace the code in the declaration and implementation, for example, as follows:

 //! Объявление. void getData(QString&, QString&, QString&); //! Реализация. void A::getData(QString &str1, QString &str2, QString &str3) {...} 

When you create a class B you define as the parent class A hiding the latter, but then showing the child. This is not quite logical, because visibility state is also inherited along with the parent-child relationship. If you already want to create independent windows throughout, do not make them dependent on each other by the parent-child relationship. This is, in general, not a mistake, Qt will work correctly, but just a slight discomfort appears.

With regards to the "empty" pointer (as I understand it, you are hinting at this ), I did not quite understand how you determined that it was empty. Judging by the way it is transmitted in your code, it should not be empty by definition.

  • Thank! You are absolutely right. Yes, I forgot about setCentralWidget() . Changed function arguments to links, and it all worked! - Cirran

I think you need to specify the window.

 QWidget *window = new QWidget (this);