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. [solved] Returnvalue QDialog
QtWS25 Last Chance

[solved] Returnvalue QDialog

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 13.4k 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.
  • H Offline
    H Offline
    huckfinn
    wrote on last edited by
    #1

    Hi people,

    I have written a dialog and I can start that properly. This dialog onyl shows some primitve values in a few QLineEdits, etc. I have three Buttons on that Dialog: Ok, Abort and Delete.
    @
    MyDialog myDlg();
    int retCode = myDlg.exec();
    if ( retCode==QDialog::Accepted ) {
    // store possible changes
    } else if ( retCode==QDialog::Rejected ) {
    // discard possible changes
    } else {
    // when Delete is pressed, i would like to jump here
    // see connect below
    pDataController.removeDataItem(myDlg.bla());
    pDataDetails->hideRow(tRow);
    }
    @

    The connects in that MyDialog class:
    @
    connect(btnOK, SIGNAL(clicked(bool)), this, SLOT(accept()));
    connect(btnAbort, SIGNAL(clicked(bool)), this, SLOT(reject()));
    connect(btnDelete, SIGNAL(clicked(bool)), this, SLOT(deleteItem()));
    @

    DO I have to emit own signal to trigger deleteItem() ?
    Is there an easier way?

    Cheers Huck

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Hi Huck,

      the parameter lists of signals and slots do not match. Therefore, all three connects shall fails. Check the return value of connect and "consult":http://doc.qt.nokia.com/4.7/signalsandslots.html for more details.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • H Offline
        H Offline
        huckfinn
        wrote on last edited by
        #3

        Hi koahnig,

        u sure? The standardbuttons (btnOk and btnAbort) work as expected. bool is emitted, but not received by the slots. The other way around, would be problematic, when a slot expects a singal which is not emitted.. (SO I have understood it ?!) --> The if statements @if ( retCode==QDialog::Accepted)@ are reached..

        But when I click Delete, nothing happens. (Due to a stupid mistake by my side I assume)

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Signals and slots are working in many cases as a call to a particular method. In such cases you have often even the possibility to substitute the emit with a direct call to the slot. From point of view, it seem to be better to have matching parameter lists. However, possibly the moc does some adjustments when it is obvious. I do not know.

          However, if I understood correctly at least the last of the connects does not work?
          Does this connect return as well?

          In your first code section you are checking the return value of exec. Based on the return value you want to do some actions. Are these actions duplicates to the slot functionality? Looks a little confusing to me at the moment.

          Edit: Looks you have edited your post while I was writing.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            [quote author="huckfinn" date="1311235794"] --> The if statements @if ( retCode==QDialog::Accepted)@ are reached..

            But when I click Delete, nothing happens. (Due to a stupid mistake by my side I assume)[/quote]

            This part of your post was not there, when I answered with the post above. It looks now even more that you have no implementations of the slots. Please post your class definition.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • H Offline
              H Offline
              huckfinn
              wrote on last edited by
              #6

              Ok here we go:
              Dialog header DlgDataDetails.h
              @
              #pragma once

              #include <QObject>
              #include <QDialog>
              #include <QString>
              #include <QtGui/QLineEdit>
              #include <QtGui/QLabel>
              #include <QtGui/QGridLayout>
              #include <QtGui/QHBoxLayout>
              #include <QtGui/QDialogButtonBox>
              #include <QtGui/QPushButton>
              #include "DataItem.h"
              #include "DataController.h"

              class DlgDataDetails : public QDialog
              {
              Q_OBJECT

              public:
              DlgDataDetails(const QString &title;, QWidget *parent, const DataItem * pPI, DataController *tPC);
              ~DlgDataDetails(void);

              QPushButton *btnOK, *btnAbort, *btnDelete;
              QLabel *lblID, *lblName, *lblCategorie;
              QLineEdit *leID, *leName, *leCategorie;
              QDialogButtonBox *btnHBox;
              QHBoxLayout *topLO;
              QGridLayout *leftLO, *rightLO;
              const DataItem * rPI;
              DataController * rPC;

              public slots:
              

              quint32 deleteItem();
              };
              @

              and the corresponding source DlgDataDetails.cpp:
              @
              #include "DlgDataDetails.h"

              DlgDataDetails::DlgDataDetails(const QString &title;, QWidget *parent, const DataItem * pPI, DataController * tPC)
              : QDialog(parent)
              {
              topLO = new QHBoxLayout;
              leftLO = new QGridLayout;
              rightLO = new QGridLayout;

              lblID = new QLabel(tr("ID"));
              leID = new QLineEdit;
              leID->setText(tr("%1").arg(pPI->getDataID()));
              leID->setReadOnly(true);
              lblID->setBuddy(leID);

              lblName = new QLabel(tr("Name"));
              leName = new QLineEdit;
              leName->setText(tr("%1").arg(pPI->getDataName()));
              lblName->setBuddy(leName);

              lblCategorie = new QLabel(tr("Categorie"));
              leCategorie = new QLineEdit;
              leCategorie->setText(tr("%1").arg(pPI->getDataCat()));
              lblCategorie->setBuddy(leCategorie);

              leftLO->addWidget(lblID, 0, 0);
              leftLO->addWidget(leID, 0, 1);
              leftLO->addWidget(lblName, 1, 0);
              leftLO->addWidget(leName, 1, 1);
              leftLO->addWidget(lblCategorie, 2, 0);
              leftLO->addWidget(leCategorie, 2, 1);
              

              btnOK = new QPushButton(tr("OK"));
              btnOK->setObjectName("btnOK");
              btnOK->setDefault(true);

              btnAbort = new QPushButton(tr("Abort"));
              btnAbort->setObjectName("btnAbort");
              btnAbort->setCheckable(true);

              btnDelete = new QPushButton(tr("Delete"));
              btnDelete->setObjectName("btnDelete");
              btnDelete->setCheckable(true);

              connect(btnOK, SIGNAL(clicked(bool)), this, SLOT(accept()));

              connect(btnAbort, SIGNAL(clicked(bool)), this, SLOT(reject()));

              connect(btnDelete, SIGNAL(clicked(bool)), this, SLOT(deleteItem()));

              /// \Vertical Buttons on the right
              btnHBox = new QDialogButtonBox(Qt::Vertical);
              btnHBox->addButton(btnDelete, QDialogButtonBox::ActionRole);
              btnHBox->addButton(btnAbort, QDialogButtonBox::RejectRole);
              btnHBox->addButton(btnOK, QDialogButtonBox::ApplyRole);

              rightLO->addWidget(btnHBox);

              topLO->addLayout(this->leftLO);
              topLO->addLayout(this->rightLO);

              this->rPI = pPI;
              this->rPC = tPC;

              // set Top Layout
              setLayout(topLO);

              // last set Window title
              setWindowTitle(title);
              } // end constructor

              quint32 DlgDataDetails::deleteItem()
              {
              int i = this->rPI->getDataID();
              return this->rPC->removeDataItem( i );
              }

              DlgDataDetails::~DlgDataDetails(void)
              {
              }
              @

              clicked(bool) is public signal from QAbstractButton
              accept and reject() are public slots from QDialog, whereas both of them I haven't implemented by myself.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                [quote author="huckfinn" date="1311245982"]
                @
                quint32 DlgDataDetails::deleteItem()
                {
                int i = this->rPI->getDataID();
                return this->rPC->removeDataItem( i );
                }
                @
                [/quote]

                Where should the return value go?

                You should have a look to this "thread":http://developer.qt.nokia.com/forums/viewthread/7675/P15

                This is about slot returning a value to the emitting routine and why it does not work.

                You need to have a different construct in your case. You have to write an overlay for exec to return your specific value.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  huckfinn
                  wrote on last edited by
                  #8

                  [quote author="koahnig" date="1311250486"]Where should the return value go?[/quote]
                  I tried to return a special ID, after I finished with my Dialog. Therefore the return quint32, which I have changed to void now..
                  According to http://doc.qt.nokia.com/4.7-snapshot/qdialog.html#done I shall be able to set a result code r.. Isn't that int r returned by exec() after all?

                  Cheers

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aureshinite
                    wrote on last edited by
                    #9

                    I believe the issue is that you don't set the result in your custom slot. There is this method : "QDialog::setResult(int)":http://doc.qt.nokia.com/4.7/qdialog.html#setResult. You should probably use it.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      huckfinn
                      wrote on last edited by
                      #10

                      Confused, do not bahave as expected..

                      @
                      connect(this, SIGNAL(finished(int)), this, SLOT(done(int)));
                      connect(btnDelete, SIGNAL(clicked(bool)), this, SLOT(deleteItem()));
                      //..
                      void DlgDataDetails::deleteItem()
                      {
                      this->setResult( this->rPI->getDataID() );
                      this->rPC->removeDataItem( this->result() );
                      }
                      @
                      I appreciate any advice.
                      Cheers Huck

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #11

                        Hi Huck,

                        I guess the following sample code does not return?
                        [quote author="huckfinn" date="1311582611"]
                        @
                        connect(this, SIGNAL(finished(int)), this, SLOT(done(int)));
                        connect(btnDelete, SIGNAL(clicked(bool)), this, SLOT(deleteItem()));
                        //..
                        void DlgDataDetails::deleteItem()
                        {
                        this->setResult( this->rPI->getDataID() );
                        this->rPC->removeDataItem( this->result() );
                        }
                        @
                        [/quote]

                        I guess you should change to:
                        @
                        void DlgDataDetails::deleteItem()
                        {
                        this->done ( this->rPI->getDataID() );
                        this->rPC->removeDataItem( this->result() );
                        }
                        @
                        Then it should work. BTW: the construct "this->" is not required. You may leave it away.

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          huckfinn
                          wrote on last edited by
                          #12

                          Thanks, I mixed that up. So I also could comment the connect line, its not more needed fopr this purpose. Works!

                          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