Passing variable from one form to another
Hello, I am having trouble passing a value from one function in Qt to another in a different form
Beastiary.cpp
Code:
{
const QString nvalue
= ui
->listWidget
->currentItem
()->text
();
//Value that needs to passed read red;
red.setModal(true);
red.exec();
red.ulk();
}
read.cpp
Code:
void read::ulk()
{
QFile file (nvalue
);
//needs nvalue from beastiary so it knows the name of the file to look for {
QMessageBox::warning (this,
"Error",
"File Could not Be Opened");
}
while (!in.atEnd())
{
line = in.readLine();
ui->lineEdit_name_r->setText(ui->lineEdit_name_r->text()+line);
qDebug() <<line;
}
}
The variable nvalue must be obtained in beastiary.cpp but is needed in read.cpp and im not sure of how to pass the variable to read from beastiary.
Re: Passing variable from one form to another
Hi.
In read.cpp or read.h add variable nvalue (if you like the same name), now in beastiary.cpp before red.exe() assign value to "nvalue", for example: red.nvalue = nvalue.
Hope this help.
Re: Passing variable from one form to another
Thanks for the reply, I tried your suggestion but got the following error: passing 'const QString' as 'this' argument discards qualifiers [-fpermissive]
This is the code I wrote for it
read.h
Code:
#ifndef READ_H
#define READ_H
#include <QDialog>
namespace Ui {
class read;
}
{
Q_OBJECT
friend class beastiary;
public:
void ulk();
explicit read
(QWidget *parent
= 0);
~read();
private:
Ui::read *ui;
};
#endif // READ_H
beastiary.cpp
Code:
{
const QString nvalue
= ui
->listWidget
->currentItem
()->text
();
read red;
red.nvalue=nvalue;
red.setModal(true);
red.exec();
red.ulk();
}
read.cpp
Code:
void read::ulk()
{
{
QMessageBox::warning (this,
"Error",
"File Could not Be Opened");
}
while (!in.atEnd())
{
line = in.readLine();
ui->lineEdit_name_r->setText(ui->lineEdit_name_r->text()+line);
qDebug() <<line;
}
}
Re: Passing variable from one form to another
You can't assign to a "const" member or variable, it is "constant".
You can keep the const if you pass the value as an argument to read's constructor.
Cheers,
_