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. Cannot convert 'QString' to 'char*'
Forum Updated to NodeBB v4.3 + New Features

Cannot convert 'QString' to 'char*'

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 21.8k 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.
  • D Offline
    D Offline
    dmyuser
    wrote on 31 Jan 2012, 14:50 last edited by
    #1

    Hi.
    First of all, I'm a beginner in Qt, and I'm in need of help with a project.

    This is a very simple form application, whose main code is below:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    int decode(char pass[5], char digits[11]);

    void MainWindow::on_pushButton_clicked()
    {

    ui->secondText->setText( decode(ui->firstText->text(), "1234567890") );
    

    }

    int decode(char pass[5], char digits[11])
    {

    int functionReturn;
    
    functionReturn = 1234;
    
    return functionReturn;
    

    }

    int main()
    {
    return 0;
    }

    @

    I have two QLineEdit and one QPushButton.When pressing the button, the secondText object would receive the function's return (integer)

    But when compiling, I get a compiler error:

    "cannot convert 'QString' to 'char*' for argument '1' to 'int decode(char*, char*)'"
    at
    ui->secondText->setText( decode(ui->firstText->text(), "1234567890") );

    I've tried to convert QString to a char, like so:

    @void MainWindow::on_pushButton_clicked()
    {

    //QString str1 = "Test";
    
    QByteArray ba = ui->firstText->text().toLocal8Bit();
    char *c_str2 = ba.data();
    char *digits = "1234567890";
    
    ui->secondText->setText( decode( c_str2  , digits) );
    

    }@

    But wasn't succesfully either:

    "error: invalid conversion from 'int' to 'const char*'"
    at

    ui->secondText->setText( decode( c_str2 , digits) );

    Can anyone please shed a light on this?

    Thanks

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on 31 Jan 2012, 15:02 last edited by
      #2

      Try converting int that you are trying to display into string.

      @int a = 100;
      QString::number(a); // returns QString, in this case 100
      @

      When covnerting QString to char*, try converting int first to std::string, and then to C string

      @QString a = "String";
      char b[20];
      strcpy( b, a.toStdString().c_str();
      @

      Otherwise, I would recommend, that you start using only Qt strings ( QString).
      Makes your life much easier ;).


      Code is poetry

      1 Reply Last reply
      1
      • A Offline
        A Offline
        andre
        wrote on 31 Jan 2012, 15:07 last edited by
        #3

        Please reference this "FAQ":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa on string converstions. Note that you probalbay don't need them though for this case.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmyuser
          wrote on 31 Jan 2012, 19:18 last edited by
          #4

          [quote author="Jake007" date="1328022120"]...
          When covnerting QString to char*, try converting int first to std::string, and then to C string

          @QString a = "String";
          char b[20];
          strcpy( b, a.toStdString().c_str();
          @

          ...[/quote]

          Thanks for your valuable help, i could compile by using:

          @QString a = "String";
          char b[20];
          strcpy( b, a.toStdString().c_str();
          @

          But I've got to change function's type to return an QString (instead integer)

          My question:

          Is there any way to succeed, but while maintaining the function type as an integer?

          I'm thinking about portability:

          If I want to port a form-based C++ application to QT, I wouldn't want to change every single function to return a QString....the changes in the code should be as less evasible as possible....

          Or Am I wrong on this one?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on 31 Jan 2012, 19:41 last edited by
            #5

            [quote author="dmyuser" date="1328021435"]
            @
            void MainWindow::on_pushButton_clicked()
            {

            ui->secondText->setText( decode(ui->firstText->text(), "1234567890") );
            

            }
            @
            [/quote]

            I guess your concern is the method shown right above.
            If you want to set a text in the gui you need to use QString or a char pointer.
            If your function is returning an integer you need to transform it to a QString. But since you are migrating to Qt you have to "touch" the whole gui stuff anyhow. So it is not clear why this is a problem then.

            A possibility could be to derive a class from QLabel, QLineEdit or whatever secondText is, and supply your method where you could have a method setInt (int i). However, I am not sure, if this is really helpful.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dmyuser
              wrote on 1 Feb 2012, 10:48 last edited by
              #6

              [quote author="koahnig" date="1328038860"]...
              If you want to set a text in the gui you need to use QString or a char pointer.
              If your function is returning an integer you need to transform it to a QString. But since you are migrating to Qt you have to "touch" the whole gui stuff anyhow. So it is not clear why this is a problem then.

              A possibility could be to derive a class from QLabel, QLineEdit or whatever secondText is, and supply your method where you could have a method setInt (int i). However, I am not sure, if this is really helpful.
              [/quote]

              I understand your point.

              But my idea was touching in QT-related GUI code, and keep rest of the code (i.e. an inner C++ function) intact.

              In my case, avoid touching the "decode" function, and do the stuff where I must do (button action)

              This would make porting some function easier, since I wouldn't have to change it.

              For example, I would manipulate only MainWindow::on_pushButton_clicked(), and avoid touching internal functions.

              Is it possible anyhow?

              Thanks

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on 1 Feb 2012, 11:46 last edited by
                #7

                Well, as already suggested the derived class option is a way.

                However, I am still not sure, if this is really a way to go. You need to supply a derived class for all the classes of Qt you are using in that manner. Personally I would not get that route, but this is certainly your choice. But it is possible.

                The question is at the day's end how many functions you have to adapt, because you require to write ints or other numerical value to string fields. If those are 10 or 20 functions (most probably less), that seems to be less a problem to change. Presumably you are using the same functions not only for display in your gui. In that case you may use a wrapper function.

                Something like:
                @
                QString decodeToString ( char * ptr1, char * ptr2 )
                {
                return QString::number ( decode ( ptr1, ptr2 ) );
                }
                @

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  love8879201
                  wrote on 23 Oct 2014, 09:59 last edited by
                  #8

                  try this

                  @Qstring str = "hello";
                  QByteArray byteArray = str.body().toUtf8();
                  char* data = byteArray.data();@

                  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