0

As a beginner in Qt with C++, I am having difficulty in accessing members of my MainWindow object from a Dialog object.

My dialog object is from a class called Month, and below is my code:

I have a comboBox in MainWindow where I want the user to select a month, and then store the string for that month in a private variable called monthSelected as follows:

void MainWindow::on_monthComboBox_activated(const QString &arg1)
{
    monthSelected = arg1;
    qDebug()<<"Month Selected is " << monthSelected << endl;
}

I get the correct output after selecting the month, but when I try to retrieve the value of monthSelected in my Month dialog class via a getter function, then I get an empty string as output.

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

    MainWindow *mainWindow = new MainWindow();

    qDebug()<<mainWindow->getMonthSelected();
}

As you can see, I created a pointer to MainWindow in order to access the getter function in my Month class, and I suspect this is where my error lies.

Is this the correct way to access members from the MainWindow class in a dialog, and if not, how would I go about doing it correctly?

  • 2
    Why are you creating a new instance of MainWindow? That is not going to help at all this is a totally new object having nothing to do with the already existing MainWindow from your application. – drescherjm Nov 20 '18 at 13:37
  • 1
    Maybe the `parent` pointer is already the MainWindow? Who creates the `Month` object? – drescherjm Nov 20 '18 at 13:39
  • @drescherjm The Month object is created by the MainWindow when the user clicks on a button in the main window. **Month *monthDialog = new Month(this);** – CodeCanary Nov 20 '18 at 13:51
  • Then you already have a pointer to your MainWindow. – drescherjm Nov 20 '18 at 14:03
  • `MainWindow* pMain = qobject_cast(parent);` – drescherjm Nov 20 '18 at 14:04
  • https://stackoverflow.com/questions/43994584/what-is-qobject-cast – drescherjm Nov 20 '18 at 14:05
  • Thanks for this. How would I then go about referencing the already existing pointer (and accessing its members) in the Month class? – CodeCanary Nov 20 '18 at 14:12
  • parent() returns the parent pointer at any time. See here: http://doc.qt.io/qt-5/qobject.html#parent – drescherjm Nov 20 '18 at 14:15
  • With this said you probably want to use signals and slots for what you are doing. – drescherjm Nov 20 '18 at 14:22
  • I have always just retrieved the value chosen by the dialog from the parent. I believe this is the design intent and so easy. Setting a value in the parent would be, as you have found, much more difficult. – Michael Surette Nov 20 '18 at 14:28
  • @drescherjm I've tried using parent to access MainWindow's member functions, but to no avail. None of the functions in MainWindow is available when using parent. – CodeCanary Nov 20 '18 at 15:51
  • Did you do this? `MainWindow* pMain = qobject_cast(parent);` – drescherjm Nov 20 '18 at 15:52
  • Or when outside the constructor `MainWindow* pMain = qobject_cast(parent());` – drescherjm Nov 20 '18 at 15:52
  • @drescherjm I've used **MainWindow* pMain = qobject_cast(parent);** and it works now, thank you so much. Still new to QT and C++, so I'll dive into why this worked, but in the meantime just happy that it does. Cheers! – CodeCanary Nov 20 '18 at 16:08

0 Answers0