Cannot convert 'QString' to 'char*'
-
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*'"
atui->secondText->setText( decode( c_str2 , digits) );
Can anyone please shed a light on this?
Thanks
-
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 ;). -
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.
-
[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?
-
[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.
-
[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
-
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 ) );
}
@ -
try this
@Qstring str = "hello";
QByteArray byteArray = str.body().toUtf8();
char* data = byteArray.data();@