Get variable from a dialog to MainWindow
-
Hello, I know that this "type" of post has already been created several times, but me being the noob I am, can't understand these...
So, I just want to know, how to get a value from a dialog, and give it to MainWindow.
Thanks@Sucharek said in Get variable from a dialog to MainWindow:
get a value from a dialog, and give it to MainWindow.
A
QDialog
class?Either with signals & slots or as return value.
// MainWindow, where you start your dialog Dialog dlg; if (dlg.exec() == QDialog::Accepted) { // simply call the getters from your dialog class to get the values int x = dlg.getMyNum(); QString s = dlg.getMyString(); }
-
int x = dlg.getMyNum();
QString s = dlg.getMyString();These are already the function calls.
You have to have
getMyNum()
andgetMyString()
as public (getter) function in your dialog class. Then just return whatever you want to pass to your mainWindow (the return types have to match of course).One of those functions could look like this:
// Dialog header public: // ctor // ... int getMyNum(){ return m_myNumber; } private: int m_myNumber; // the private variable, you want to return
-
int x = dlg.getMyNum();
QString s = dlg.getMyString();These are already the function calls.
You have to have
getMyNum()
andgetMyString()
as public (getter) function in your dialog class. Then just return whatever you want to pass to your mainWindow (the return types have to match of course).One of those functions could look like this:
// Dialog header public: // ctor // ... int getMyNum(){ return m_myNumber; } private: int m_myNumber; // the private variable, you want to return
-
Ok, so I don't have any errors now, but if I click OK on my dialog, nothing happens.
I think that the
if (setWorkingDirectory.exec() == QDialog::Accepted)
doesn't work for me, because I put qDebug in there, and it doesn't print anything.@Sucharek said in Get variable from a dialog to MainWindow:
if I click OK on my dialog, nothing happens.
Is your Ok Button connected to
accept()
?? If you use the default button box, it should be the case by default. If you add your own buttons, you have to do it manually.Add
accept()
at the end of the slot, which is called when you click your Ok button to finish your dialog. -
Ok, so I don't have any errors now, but if I click OK on my dialog, nothing happens.
I think that the
if (setWorkingDirectory.exec() == QDialog::Accepted)
doesn't work for me, because I put qDebug in there, and it doesn't print anything.@Sucharek said in Get variable from a dialog to MainWindow:
if (setWorkingDirectory.exec() == QDialog::Accepted)
BTW: It looks like yoou want to set a directory there. You can use
QFileDialog
class for this. Then you dont have to worry about the return function and your dialog design.- https://doc.qt.io/qt-5/qfiledialog.html
- https://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory
For example:
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
This can be called directly from your mainWindow without any additional
QDialog
class. -
@Sucharek said in Get variable from a dialog to MainWindow:
if (setWorkingDirectory.exec() == QDialog::Accepted)
BTW: It looks like yoou want to set a directory there. You can use
QFileDialog
class for this. Then you dont have to worry about the return function and your dialog design.- https://doc.qt.io/qt-5/qfiledialog.html
- https://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory
For example:
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
This can be called directly from your mainWindow without any additional
QDialog
class.