[SOLVED] can't return a qstring
-
when i return i value in the changePixmap function, the value returns it incorrectly. i am calling the function changePixmap from within the mainwindow constructor. yet when it returns, i get a blank message. it should return "test". what am i doing wrong?
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
changePixmap();
ui->textEdit->setText(message);
}QString const MainWindow::changePixmap(message)
{
QString message="test";
return message;
}@ -
Well, your method should look like
QString const MainWindow::changePixmap( type message ) { QString message="test"; return message; }
With the correct type you need (I can't guess...), but then the argument message will be shadowed by the QString message which is declared in the method's body.
-
I guess what you're trying to do is :
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
const QString message = changePixmap();
ui->textEdit->setText(message);
}QString MainWindow::changePixmap()
{
QString message = "test";
return message;
}
@Here, you call the changePixmap member function and put the return into a local (const-qualified) message variable.
-
The method declaration should be absolutely the same in your header file and in your implementation file,
If you have
@
QString changePixmap(QString message);
@
in your header file, your implementation should be
@
QString MainWindow::changePixmap(QString message)
@
Match the types exactly..... -
Calling function and the fuction you are expecting to be called are different [in signature].
changePixmap() is NOT changePixmap(message)
You should define changePixmap() separately, if you are looking for polymorphism.And Like Octal said, there is a missing type for the formal parameter.
EDIT : post was bit late, as the window was not refreshed :(
[quote author="kalster" date="1314771307"]when i return i value in the changePixmap function, the value returns it incorrectly. i am calling the function changePixmap from within the mainwindow constructor. yet when it returns, i get a blank message. it should return "test". what am i doing wrong?@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
changePixmap();
ui->textEdit->setText(message);
}QString const MainWindow::changePixmap(message)
{
QString message="test";
return message;
}
@
[/quote]