Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do i use okay and cancel buttons on QDialogbox
Forum Updated to NodeBB v4.3 + New Features

How do i use okay and cancel buttons on QDialogbox

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    learnist
    wrote on last edited by learnist
    #1

    i have a QDialog box named "WinApp" , it looks like this 09bdbfee-460d-495a-9924-97e60ab8c59a-image.png , 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();
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Where are you doing that assignment ?
      Are you taking into account whether you clicked on Ok or Cancel ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        learnist
        wrote on last edited by learnist
        #3

        @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
            }
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          1
          • SGaistS SGaist

            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.

            L Offline
            L Offline
            learnist
            wrote on last edited by
            #5

            @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 ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              L 1 Reply Last reply
              3
              • SGaistS SGaist

                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.

                L Offline
                L Offline
                learnist
                wrote on last edited by learnist
                #7

                @SGaist thanks, this did the trick.

                WinApp winob;
                winob.exec();
                if (winob.result() == QDialog::Accepted) {
                   String = dialog.getDialogueValue();
                }
                
                Pl45m4P 1 Reply Last reply
                0
                • L learnist

                  @SGaist thanks, this did the trick.

                  WinApp winob;
                  winob.exec();
                  if (winob.result() == QDialog::Accepted) {
                     String = dialog.getDialogueValue();
                  }
                  
                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @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 call result() at all.

                  WinApp winob;
                  if (winob.exec() == QDialog::Accepted) // This works as well
                  {
                     QString val = winob.getDialogueValue();
                  }

                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  3

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved