[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;
    }@



  • Apparently you're overwriting the content of message, since it's the method argument AND a local variable. But the method declaration seems weird to me... at least a type is expected, you're only giving a variable.



  • i don't understand what you are saying. i am getting an error expected ',' or ';' before '{' token at the beginning of the function.



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



  • i changed it do the following...

    @QString const MainWindow::changePixmap( QString message )@
    now i get an error no matching function for call to 'MainWindow::changePixmap()'

    in the head i have, QString changePixmap(QString message);



  • If your method is just returning a QString, it doesn't need to have any argument at all! Octal's suggestion should just be fine!



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



  • uhm, and yeah, octal is right, you don't need the argument at all in this case



  • ya that worked octal. thank you



  • 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]



  • thank you Rahul Das for your post. every bit of information helps me to understand qt a bit more. :)

    yes octal solved this topic.



  • Hi kalster, just as a ide node. Those are no Qt issues, those are basic C++ issues.



  • yes i know. i made a mistake when i posted the message about understanding qt a bit more. i should have said c++.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.