[Solved] Passing data to dialog
-
I'm getting a value from a dialog, put sending data does not work.
First the mainwindow: it passes the string "this is a test" to the dialog and receives the data into a qstring "fileName"
@
usbfileDialog sddialog;
sddialog.setModal(true);
sddialog.setCstring("this is a test");if(sddialog.exec() == QDialog::Accepted) fileName = sddialog.binfileName(); // this works great! else return;
@
usbfiledialog.h:
@
public:
void setCstring(const QString &text);public:
QString binfileName();@
usbfiledialog.cpp
@
QString cstr1; //declared after the #includes, so it's global to the dialog.
QString usbfileDialog::binfileName() {
return ui->usblistWidget->currentItem()->text();
}void usbfileDialog::setCstring(const QString &text) {
cstr1 = text;
}usbfileDialog::usbfileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::usbfileDialog)
{ui->setupUi(this); QMessageBox::critical(this,"",cstr1); // always blank!
}
usbfileDialog::~usbfileDialog()
{
delete ui;
}@
the cstring cstr1 is always empty. I'm puzzled as to why.
-
That's because usbfileDialog(QWidget * parent) is a constructor and it called first of all when you create object of usbfileDialog. If you want use cstr1 do this in setCstring(const QString &text) or after calling it.
By the way try not use global variables, make cstr1 as member of class.
-
Hi,
In your setString update the ui member holding the string data. That should do the trick for now. What qxoz says is very important. Place the QString in your class definition as private member. Or in your case, delete in total this string. You should place it direct in your ui string.
Something like this:
@
void usbFileDialog::setString(QString &NewString)
{
QMessageBox::critical(this, NewString);
}
@
But maybe this should be placed before generating the Dialog as a child of MainWindow instead of the usbFileDialog. Then when the user might be able to call <cancel> and doesn't execute the dialog. -
qxoz, Jeroentje, thanks for the replies.
Ok. Can you explain what do you want to do?
Sure. As seen in the code in the OP, I want to pass text to a dialog's qstring when the dialog is called.
I took the:
@
QString cstr1; //declared after the #includes, so it's global to the dialog.
@And placed it in my .h file
@
private:
QString cstr1;
@but I don't know what the following means, a code example would be very helpful:
"That’s because usbfileDialog(QWidget * parent) is a constructor and it called first of all when you create object of usbfileDialog. If you want use cstr1 do this in setCstring(const QString &text) or after calling it."
-
Well, sorry for my English :)
First of all, remove:
@ QMessageBox::critical(this,"",cstr1); // always blank!@Other parts of your code are ok for now.
When you use your dialog
@usbfileDialog sddialog;//usbfileDialog(QWidget * parent) called here
sddialog.setModal(true);
sddialog.setCstring("this is a test");
//now cstr1 == "this is a test"
sddialog.doSomeActionWithCstr1();//
if(sddialog.exec() == QDialog::Accepted)
fileName = sddialog.binfileName(); // this works great!
else return;@
i hope it is clearer now. However, i recommend you read some C++ book.
for example
"Introduction to Design Patterns in C++ with Qt":https://qt-project.org/books/view/introduction-to-design-patterns-in-c-with-qt
"http://www.cplusplus.com/":http://www.cplusplus.com/
"http://voidrealms.com/":http://voidrealms.com/