Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Dialog form with a part id,qty, & Price. I need to pass values for these from my main form to the dialog. I found a couple suggestions via google which I've tried. One way was to add getters & setters. I was able to set the values before dialog.exec but how do I get these values to display in the dailog?

Here is my editorderline.h


C++
#ifndef EDITORDERLINE_H
#define EDITORDERLINE_H

#include <QDialog>
#include "Globals.h"
#include "helpers.h"
#include <part.h>
namespace Ui {
class EditOrderLine;
}

class EditOrderLine : public QDialog
{
    Q_OBJECT

public:

    void setPart(const QString &part);
    void setPartKey (const int &partkey);
    void setPartName(const QString &partname);
    void setQty (const int &qty);
    void setPrice(const double &price);
    <pre lang="C#">QString getPart();
public:
    explicit EditOrderLine(QWidget *parent = 0);
    ~EditOrderLine();

private:
    Ui::EditOrderLine *ui;
    QString m_part;
    QString m_partname;
    int m_partkey;
    int m_qty;
    double m_price;

};

#endif // EDITORDERLINE_H</pre>

<pre lang="text">

Here is the editorderline.cpp

C#
<pre lang="C++">

&lt;pre&gt;#include &quot;editorderline.h&quot;
#include &quot;ui_editorderline.h&quot;

EditOrderLine::EditOrderLine(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditOrderLine)
{

    ui-&amp;gt;setupUi(this);
      
    qDebug()&amp;lt;&amp;lt;&quot;m_part = &quot; &amp;lt;&amp;lt;m_part;
    qDebug()&amp;lt;&amp;lt;&quot;getPart = &quot; &amp;lt;&amp;lt;getPart();
    qDebug()&amp;lt;&amp;lt;&quot;this-&amp;gt;m_part = &quot; &amp;lt;&amp;lt; this-&amp;gt;m_part;



}

EditOrderLine::~EditOrderLine()
{
    delete ui;
}

void EditOrderLine::setPart(const QString &amp;amp;part)
{     
    m_part = part;

}
QString EditOrderLine::getPart()
{

    return m_part;

}
void EditOrderLine::setPartName(const QString &amp;amp;partname)
{
    m_partname = partname;
}

void EditOrderLine::setPartKey (const int &amp;amp;partkey)
{
    m_partkey = partkey;
}
void EditOrderLine::setQty(const int &amp;amp;qty)
{
    m_qty = qty;
}
void EditOrderLine::setPrice(const double &amp;amp;price)
{
    m_price = price;
}&lt;/pre&gt;
<pre lang="text">

As you can see above I've used a few debug statements to try to see show to access the values for part id but each way returns blank. How can this be done?
Posted

1 solution

The problem is that you are trying to show the values in the constructor. The constructor is called when the dialog object is created, before you call the getters and setters and the exec() function.

When the dialog is displayed (with the exec function) the values should be on the form.
 
Share this answer
 
Comments
JohnnyG62 30-Dec-15 13:25pm    
So where would I put the code to set the text of the dialog fields (or the debug statements) since it cant be done in the constructor?
Eftimoski Mende 4-Jan-16 2:49am    
First you need to create instance of the dialog, than call all the setters, and finally the exec function.

Your code should something like this:

EditOrderLine *dialogToShow = new EditOrderLine(); //creating instance

dialogToShow->setPart("PartName");//calling the setters

dlg->exec(); //showing the dialog

delete dialogToShow; //deleting the instance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900