Unsolved How do i use okay and cancel buttons on QDialogbox
-
i have a QDialog box named "WinApp" , it looks like this , so , when i click ok , the values entered inside two lineedits must be assigned to two QStrings, How can i achieve this ? Because as of now , even if i click "cancel" the values are still being assigned to QStrings.
but if i click cancel the values entered should not be assigned to QStrings.
the code for my WinApp.h is
#pragma once #include <QtWidgets/qdialog.h> #include "ui_WinApp.h" class WinApp : public QDialog, public Ui::WinApp { Q_OBJECT public: WinApp(QWidget *parent = Q_NULLPTR); ~WinApp(); QString getDialogueValue(); private slots: private: Ui::WinApp ui; };
the code for my WinApp.cpp is
#include "WinApp.h" WinApp::WinApp(QWidget *parent) : QDialog(parent) { ui.setupUi(this); } WinApp::~WinApp() { } QString WinApp::getDialogueValue() { return ui.lineEdit->text(); }
-
Hi,
Where are you doing that assignment ?
Are you taking into account whether you clicked on Ok or Cancel ? -
@SGaist
Thanks for reply, I am doing that assignment in a function in my mainwindow
here is the code#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(); QString testo =winApp.getDialogueValue(); // Getting the value from 1st line edit here from getter function and assignment is happening here. item->setText(testo); item->setData(CapitalRole, testo); } 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 } }
-
You don't take into account the return value of exec. Use it to know whether the dialog was accepted or rejected and act accordingly.
-
@SGaist Im sorry , i do get a bit of idea what you are indicating but kinda confused where can i chnange something ,can you show me any changes in code ?
-
QDialog::exec returns a DialogCode. Store the returned value of your exec call and then use an if statement to check if the value is Accepted and of so, do your processing.
-
@SGaist thanks, this did the trick.
WinApp winob; winob.exec(); if (winob.result() == QDialog::Accepted) { String = dialog.getDialogueValue(); }
-
@learnist said in How do i use okay and cancel buttons on QDialogbox:
WinApp winob;
winob.exec();
if (winob.result() == QDialog::Accepted) {
String = dialog.getDialogueValue();
}This may work, but it's more code than you actually need ;)
As @SGaist said before,dialog.exec()
returns the exit code. So you don't need to callresult()
at all.WinApp winob; if (winob.exec() == QDialog::Accepted) // This works as well { QString val = winob.getDialogueValue(); }