简体   繁体   中英

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:

};
#endif // MAINWINDOW_H

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()
{
}

Have the function initiating the QDialog object pass the QString argument in the constructor and override accept() .

#include <QtWidgets/qdialog.h>
#include "ui_WinApp.h"

class WinApp : public QDialog, public Ui::WinApp
{
    Q_OBJECT

public:
    WinApp(QString& stringToModify, QWidget *parent = Q_NULLPTR);
    ~WinApp();

private slots:
void accept() override;
private:
    Ui::WinApp ui;
};

and define the acccept() slot which will get the QString from lineEdit and update the stringToModify with it.

void WinApp::accept(){
//do stuff with QString
this->close();
}

you need to add in the WinApp dialog class the test getter, it should be something like:

QString getFavouriteHotel() const {
    return ui.<favourite-hotel-label>.text()
}

In the MainWindow after the line winApp.exec();I suggest adding an if to check if the user accepted or declined the dialog ( docs ). If the user accepted the dialog then you can retrieve the text with the getters you just added.

Tips: avoid using exec , as said in the docs use the open

-- UPDATE -- QDialog accept --

Always check the docs

connect(&WinApp, &QDialog::finished, this, [this](int result) {
    if(result == QDialog::Accepted){
       // do aswomness
       return
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM