Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QMessageBox and QInputDialog
QtWS25 Last Chance

QMessageBox and QInputDialog

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
22 Posts 6 Posters 8.8k Views
  • 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.
  • D Offline
    D Offline
    DivyaMuthyala
    wrote on last edited by
    #1

    Hi,

    I,am trying to implement QMessageBox and qInputDialog without button,but I am unable to do it.
    Can anyone please help to implent it.

    Thanks in advance.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Without what button ?

      1 Reply Last reply
      1
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @DivyaMuthyala For QMessageBox :

        setStandardButtons(0);
        

        For QInputDialog:

        setOption(QInputDialog::NoButtons);
        

        157

        1 Reply Last reply
        2
        • D Offline
          D Offline
          DivyaMuthyala
          wrote on last edited by
          #4

          Hi,

          For Input Dialog default ok,cancel buttons will come,I am trying to disable that buttons, but i am failing to do it.

          I have tried for QMessageBox by using "setStandardButtons(0)", it is not working, when I am trying to use it,it is hanging there.
          I am attaching code:
          QMessageBox *box=new QMessageBox;
          box->setText("Clicked albel");
          box->setStandardButtons(0);
          QTimer::singleShot(2000, box, SLOT(close()));
          box->exec();
          In this case its not closing,according to timer I have written in my code,it is hanging after executing messagebox.
          In case of QInputDialog, I have use used setOption(QInputDialog::NoButtons), its not working

          p3c0P 1 Reply Last reply
          0
          • D Offline
            D Offline
            DivyaMuthyala
            wrote on last edited by
            #5

            Hi,

            Iam attaching example code of QInputDialog:

            QApplication app(argc,argv);
            bool ok;
            QInputDialog* inputDialog = new QInputDialog();
            //inputDialog->setOptions(QInputDialog::NoButtons);
            inputDialog->setOption(QInputDialog::NoButtons);
            QString text = inputDialog->getText(NULL ,"QInputDialog::getText() Example",
            "User name:", QLineEdit::Normal,
            QDir::home().dirName(), &ok);

             if (ok && !text.isEmpty())
             {
                std::cout<<text.toStdString()<<std::endl;
             }
            

            return app.exec();
            Note:Here also its not working,buttons are coming .

            mrjjM 1 Reply Last reply
            0
            • D DivyaMuthyala

              Hi,

              Iam attaching example code of QInputDialog:

              QApplication app(argc,argv);
              bool ok;
              QInputDialog* inputDialog = new QInputDialog();
              //inputDialog->setOptions(QInputDialog::NoButtons);
              inputDialog->setOption(QInputDialog::NoButtons);
              QString text = inputDialog->getText(NULL ,"QInputDialog::getText() Example",
              "User name:", QLineEdit::Normal,
              QDir::home().dirName(), &ok);

               if (ok && !text.isEmpty())
               {
                  std::cout<<text.toStdString()<<std::endl;
               }
              

              return app.exec();
              Note:Here also its not working,buttons are coming .

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @DivyaMuthyala

              Hi
              This is one of the tricky ones. ;)
              getText is actually a static function and its not really related to the ( new QInputDialog() )
              so that is why it has NO effect :)

              This works
              QInputDialog* inputDialog = new QInputDialog();
              inputDialog->setOption(QInputDialog::NoButtons);
              inputDialog->setWindowTitle("Stage 404");
              inputDialog->setLabelText("I love dogs");
              inputDialog->exec();

              1 Reply Last reply
              3
              • D Offline
                D Offline
                DivyaMuthyala
                wrote on last edited by
                #7

                Hi,

                Here I want to store text in string whatever enterd in linedit,how can i do that?

                p3c0P 1 Reply Last reply
                0
                • D DivyaMuthyala

                  Hi,

                  For Input Dialog default ok,cancel buttons will come,I am trying to disable that buttons, but i am failing to do it.

                  I have tried for QMessageBox by using "setStandardButtons(0)", it is not working, when I am trying to use it,it is hanging there.
                  I am attaching code:
                  QMessageBox *box=new QMessageBox;
                  box->setText("Clicked albel");
                  box->setStandardButtons(0);
                  QTimer::singleShot(2000, box, SLOT(close()));
                  box->exec();
                  In this case its not closing,according to timer I have written in my code,it is hanging after executing messagebox.
                  In case of QInputDialog, I have use used setOption(QInputDialog::NoButtons), its not working

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @DivyaMuthyala said in QMessageBox and QInputDialog:

                  Hi,

                  For Input Dialog default ok,cancel buttons will come,I am trying to disable that buttons, but i am failing to do it.

                  I have tried for QMessageBox by using "setStandardButtons(0)", it is not working, when I am trying to use it,it is hanging there.
                  I am attaching code:
                  QMessageBox *box=new QMessageBox;
                  box->setText("Clicked albel");
                  box->setStandardButtons(0);
                  QTimer::singleShot(2000, box, SLOT(close()));
                  box->exec();
                  In this case its not closing,according to timer I have written in my code,it is hanging after executing messagebox.
                  In case of QInputDialog, I have use used setOption(QInputDialog::NoButtons), its not working

                  In that case a little bit more effort is required. You can installEventFilter on the messagebox. Then inside eventFilter watch for that messagebox's QEvent::Close event. This event is triggered when you click X . Once triggered you can accept or reject the messagebox.

                  QMessageBox *msg = new QMessageBox(this);
                  msg->setObjectName("MessageBox");
                  msg->installEventFilter(this);
                  msg->setStandardButtons(0);
                  msg->exec();
                  
                  bool MainWindow::eventFilter(QObject *o, QEvent *e)
                  {
                      if(o->objectName()=="MessageBox")
                          if(e->type()==QEvent::Close) {
                              QMessageBox *box = qobject_cast<QMessageBox*>(o);
                              box->accept(); // accept or reject closes the message box
                          }
                  
                      return QMainWindow::eventFilter( o, e);
                  }
                  

                  157

                  1 Reply Last reply
                  2
                  • D DivyaMuthyala

                    Hi,

                    Here I want to store text in string whatever enterd in linedit,how can i do that?

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    @DivyaMuthyala said in QMessageBox and QInputDialog:

                    Hi,

                    Here I want to store text in string whatever enterd in linedit,how can i do that?

                    Try textValue after exec.

                    157

                    1 Reply Last reply
                    1
                    • dheerendraD Offline
                      dheerendraD Offline
                      dheerendra
                      Qt Champions 2022
                      wrote on last edited by
                      #10

                      You can create your own messageBox inheriting from dialog with appropriate design. You can customise with your own stuff. We can send that dialog code offline if you share your email id.

                      Dheerendra
                      @Community Service
                      Certified Qt Specialist
                      http://www.pthinks.com

                      1 Reply Last reply
                      5
                      • D Offline
                        D Offline
                        DivyaMuthyala
                        wrote on last edited by
                        #11

                        Hi,
                        divyamuthyala755@gmail.com
                        Thankyou

                        1 Reply Last reply
                        1
                        • D Offline
                          D Offline
                          DivyaMuthyala
                          wrote on last edited by
                          #12

                          Hi,

                          After Now I am able to get text from input dialog, now how to close input dialog by pressing enter key.

                          Thanks in advance.

                          jsulmJ 1 Reply Last reply
                          1
                          • D DivyaMuthyala

                            Hi,

                            After Now I am able to get text from input dialog, now how to close input dialog by pressing enter key.

                            Thanks in advance.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @DivyaMuthyala Well, documentation: http://doc.qt.io/qt-5/qmessagebox.html
                            See "Default and Escape Keys" there.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              DivyaMuthyala
                              wrote on last edited by
                              #14

                              Hi,

                              Now My QInputDialog is not closing with Enter Key.
                              Can anyone please give me solution.

                              jsulmJ 1 Reply Last reply
                              0
                              • D DivyaMuthyala

                                Hi,

                                Now My QInputDialog is not closing with Enter Key.
                                Can anyone please give me solution.

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @DivyaMuthyala Did you check the link I posted?

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                1
                                • D Offline
                                  D Offline
                                  DivyaMuthyala
                                  wrote on last edited by
                                  #16

                                  Hi,
                                  Yes I have checked , its working for messagebox but not for inputdialog.

                                  jsulmJ D 2 Replies Last reply
                                  0
                                  • D DivyaMuthyala

                                    Hi,
                                    Yes I have checked , its working for messagebox but not for inputdialog.

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @DivyaMuthyala Since QInputDialog is derived from QDialog you should read http://doc.qt.io/qt-5/qdialog.html
                                    "Default Button".

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • D DivyaMuthyala

                                      Hi,
                                      Yes I have checked , its working for messagebox but not for inputdialog.

                                      D Offline
                                      D Offline
                                      Devopia53
                                      wrote on last edited by Devopia53
                                      #18

                                      @DivyaMuthyala

                                      Hi.

                                      This is one of the tricky methods.

                                      QInputDialog    dlg(this);
                                      
                                      dlg.setWindowTitle(tr("QInputDialog::getText()"));
                                      dlg.setLabelText(tr("User name:"));
                                      dlg.setTextValue(QDir::home().dirName());
                                      dlg.setOption(QInputDialog::NoButtons);
                                      dlg.open();
                                      QLineEdit   *lineEdit = dlg.findChild<QLineEdit *>();
                                      if (lineEdit)
                                          connect(lineEdit, &QLineEdit::returnPressed, [&dlg](){
                                              dlg.accept();
                                          });
                                      if (dlg.exec() == QDialog::Accepted)
                                          qDebug() << dlg.textValue();
                                      
                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        DivyaMuthyala
                                        wrote on last edited by
                                        #19

                                        Hi,

                                        Can we create QInputDialog with only one "ok" button???

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • D DivyaMuthyala

                                          Hi,

                                          Can we create QInputDialog with only one "ok" button???

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @DivyaMuthyala Maybe you can. But the documentation says:
                                          "Escape Key
                                          If the user presses the Esc key in a dialog, QDialog::reject() will be called. This will cause the window to close: The close event cannot be ignored."
                                          That means: even if there is no Cancel button the user will still be able to use Esc key to close the dialog. Why do you want to remove Cancel button?

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          • Login

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