Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] can't return a qstring

[SOLVED] can't return a qstring

Scheduled Pinned Locked Moved General and Desktop
14 Posts 6 Posters 10.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      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.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by JohanSolo
          #4

          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.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          0
          • O Offline
            O Offline
            octal
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • JohanSoloJ Offline
                JohanSoloJ Offline
                JohanSolo
                wrote on last edited by
                #7

                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!

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Robbin
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Robbin
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kalster
                      wrote on last edited by
                      #10

                      ya that worked octal. thank you

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Rahul Das
                        wrote on last edited by
                        #11

                        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]


                        Declaration of (Platform) independence.

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kalster
                          wrote on last edited by
                          #12

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

                          yes octal solved this topic.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

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

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              kalster
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved