Alan Alan - 29 days ago 6
C++ Question

pass bool to qdialog

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();

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