[solved] Returnvalue QDialog



  • 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


  • Moderators

    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.



  • 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)


  • Moderators

    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.


  • Moderators

    [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.



  • 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.


  • Moderators

    [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.



  • [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



  • 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.



  • 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


  • Moderators

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.