-1

I'm trying to pass a bool to a qdialog via a setter method; the code compiles, but the value doesn't seem to take in the dialog. I've included just the pertinent code, I hope it's readable. What am I missing here?

// forcequitDialog h file

public:
setAction(const bool &takeaction);

private:
     bool m_action;

// forcequitDialog.cpp

  forcequitDialog::forcequitDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::forcequitDialog)
{
    ui->setupUi(this);  

       if (m_action)
          ui->title->setText("Start Application");
        else
       ui->title->setText("Stop Application");

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



  void forcequitDialog::setAction(const bool &takeaction)
    {
        m_action = takeaction;



    }

// MainWindow.cpp

 forcequitDialog dialog;

    dialog.setAction(true);  // pass value to dialog

    dialog.exec();
Alan
  • 1,265
  • 4
  • 22
  • 44
  • 1
    Where is the code commented "doesn't work"? Surely not in the global scope? – krzaq Nov 10 '16 at 02:28
  • @krzaq I've added code. – Alan Nov 10 '16 at 02:38
  • 2
    The code still looks a little wonky as pasted, but you appear to be using `m_action` in the constructor then calling `setAction(true)` afterwards. – Ken Y-N Nov 10 '16 at 02:39
  • @ken-y-n my mainwindow.cpp passes the value via dialog.setAction(true); then calls dialog.exec() I don't understand what's wonky. – Alan Nov 10 '16 at 02:49
  • @Alan the value passed in `setAction` is being passed long after the title has been set. – krzaq Nov 10 '16 at 03:05
  • 1
    @Alan, please do not edit the question to correct the issue we have pointed out, as it will confuse later readers and makes the comments and answer redundant. – Ken Y-N Nov 10 '16 at 03:39

1 Answers1

2
forcequitDialog dialog; // constructor called here

Your lines:

if (m_action)  // doesn't work
    ui->title->setText("Start Application");
else
    ui->title->setText("Stop Application");

are executed before your call to:

dialog.setAction(true);  // pass value to dialog

Either move these instructions into setAction() function or pass takeaction to your constructor.

btw: using a const reference for a bool is useless, it's not a heavy object.

O'Neil
  • 3,790
  • 4
  • 16
  • 30