Send Variable values from QDialog to QMainwindow function
-
My UI MainWindow has a QListWidget and , upon clicking of its items ,a corresponding QDialog box must pop up , which prompts the user to enter some values in QDialog box, The values entered into line_edits of QDialog box must stored into a QString variable and this variable should be accessed/used in a function of MainWindow,
for Example: I have a QListWidget, with 3 items "New York","Glasgow","Mumbai", and when i double click item named "New York", a pop-up shows up asking me this
and after i enter 3 and Hilton , the item in QListWidget which was initially "New York" must be changed to "New York -3 , The safehouse is in Hilton" my code for MainWindow.cpp is
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtCore> #include <QtGui> #include <sstream> #include <QtWidgets/qmessagebox.h> #include <QtWidgets/qlistwidget.h> using namespace std; MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->My_listwidget->addItem("New York"); ui->My_listwidget->addItem("Glasgow"); ui->My_listwidget->addItem("Mumbai"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_My_listwidget_itemDoubleClicked(QListWidgetItem* item) { QString test = item->text(); std::string test_s = test.toStdString(); if (test_s.find("New York") != std::string::npos) // check if item contains text "New York" { WinApp winApp; winApp.setModal(true); //Displaying the window here winApp.exec(); } if (test_s.find("Glasgow") != std::string::npos) { // show another dialog box asking some questions } if (test_s.find("Mumbai") != std::string::npos) { // show another dialog box asking some questions } }
My code for mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtWidgets/QMainWindow> #include "ExecutionContext.h" #include <QtWidgets/qlistwidget.h> //#include "secdialog.h" #include <qregexp.h> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT //Used to handle events public: MainWindow(QWidget* parent = 0); ~MainWindow(); //Destructor used to free resources private slots: void on_xml_scripts_textbox_itemDoubleClicked(QListWidgetItem* item); Ui::MainWindow* ui; // pointing to UI class private: };
my code for WinApp.h , winapp is the name of my dialog
#include <QtWidgets/qdialog.h> #include "ui_WinApp.h" class WinApp : public QDialog, public Ui::WinApp { Q_OBJECT public: WinApp(QWidget *parent = Q_NULLPTR); ~WinApp(); private slots: private: Ui::WinApp ui; };
WinApp.cpp
#include "WinApp.h" WinApp::WinApp(QWidget *parent) : QDialog(parent) { ui.setupUi(this); } WinApp::~WinApp() { }
-
@learnist said in Send Variable values from QDialog to QMainwindow function:
and using modal is okay, or should i go for modal less ??
This is something you should know. It is your app.
But there is one important thing when using non-modal dialog - show() is NOT blocking. That means you can't access the values from dialog just after show(). You will need to do this in a slot connected to https://doc.qt.io/qt-5/qdialog.html#accepted -
@jsulm and thanks for the answer, this really narrows down my search but,
i placed a getter method in
winApp.cppQString WinApp::getDialogueValue() { return ui.lineEdit->text(); }
i also added its declaration in winApp.h
private slots: QString getDialogueValue();
, now how should i call and get its value in the mainwindow here after this
WinApp winApp; winApp.setModal(true); winApp.exec(); ``
-
@learnist said in Send Variable values from QDialog to QMainwindow function:
now how should i call and get its value in the mainwindow here after this
Like any other method:
WinApp winApp; winApp.setModal(true); winApp.exec(); winApp.getDialogueValue();
-
@learnist said in Send Variable values from QDialog to QMainwindow function:
so how can i deal with that situation?
You can add getter for each line edit...
-
@learnist said in Send Variable values from QDialog to QMainwindow function:
Awesome, it works great, but function can return only 1 QString, it might be from 1 line edit, but i have multiple line edits in my QDialog box, so how can i deal with that situation?
I will just throw this thought in. You may not fancy changing your architecture, but it is good to know about if you start having a lot of widgets to deal with.
https://doc.qt.io/qt-5/qdatawidgetmapper.html is a very nice interface between multiple widgets and back-end data, without you having to write lots of dedicated getters/setters. But it requires you changing so that your data is all kept in a model. Bear it in mind.