PDA

View Full Version : Accessing UI Widget data from .ccp file in .c file



020394
28th June 2013, 11:11
Hello all,
I am trying to get data input from a line widget and use it in a .c file , is there a way to do it ? I am able to pass int data from .c file to cpp file and show it in the UI using extern"C"{c codes};
I tried using it with the C++ codes , but to no avail , there are plenty of errors.
Please Teach me a way to pass data from .cpp file to .c file Thank you

anda_skoa
28th June 2013, 16:56
You retrieve the QString from the QLineEdit in C++, convert it to an 8-bit encoding, e.g. using QString::toUtf8(), and then pass the char* or const char* data from the resulting QByteArray to the C function.

The char* data is valid as long as the QByteArray object is.

Cheers,
_

020394
30th June 2013, 07:43
Hello thanks for the immediate reply , i did this , when i click a button it will do this
QString a=QString::toUtf8(ui->lineEdit->text());
I am unsure of how to pass the char* to c function . Please help

anda_skoa
30th June 2013, 22:37
First, toUtf8() is not a static method, it is an instance method and has to be called on a QString instance.
Second, assigning its result value to a QString object again would get you nothing other than a needless (and possibly wrong) double conversion.

As I said, toUtf8() (and other methods to convert to 8-bit encodings such as toLocal8Bit()) return a QByteArray



QByteArray data = ui->lineEdit->text().toUtf8();
printf( "%s", data.constData() );

using printf() as an example for a C API.


Cheers,
_

020394
1st July 2013, 11:16
First, toUtf8() is not a static method, it is an instance method and has to be called on a QString instance.
Second, assigning its result value to a QString object again would get you nothing other than a needless (and possibly wrong) double conversion.

As I said, toUtf8() (and other methods to convert to 8-bit encodings such as toLocal8Bit()) return a QByteArray



QByteArray data = ui->lineEdit->text().toUtf8();
printf( "%s", data.constData() );

using printf() as an example for a C API.


Cheers,
_

Thanks Again , I now understand how to convert the QString to a array.
Now the Probelm is passing it to the C file.
this is what i did
Codes:
#include <stdio.h>
#include "mainwindow.h"
extern "C++" {QByteArray data;}
char hi()

{
printf( "%s", data.constData() );
}
i gotten many errors about namespace.
Please advice me on how to do the codes in C.
The thing i dont know is how to pass C++ object(eg. QByteArray) to C codes.
Thank you in advance !
Really sorry i am not good in Qt or c codes. I am a newbie.

Santosh Reddy
1st July 2013, 13:16
Linking C and C++ code (http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B#Linking_C_and_C.2B. 2B_code)

anda_skoa
1st July 2013, 18:30
Thanks Again , I now understand how to convert the QString to a array.

Then why don't you use that?



void hi( const char *data )
{
printf( "%s", data );
}

QString s = "....";
QByteArray d = s.toUtf8();
hi( d.constData() );


Cheers,
_

020394
2nd July 2013, 04:35
Then why don't you use that?

I am using that in my .cpp file.
I am getting the String from a LineEdit in my cpp file called mainwindow
I followed ur code by setting this

QByteArray data=ui->lineEdit->text().toUtf8();

Now I want to pass the QByteArray data to the c file .
I am passing it to the c file to query it from the database using sqllite statment.

I tried calling the object from the cpp file to c file
Using

extern "C" QByteArray data;

But i dont think i am able to retrieve the data from the cpp file

I am getting errors such as unknown type "namespace".
.

020394
2nd July 2013, 07:33
void hi( const char *data )
{
printf( "%s", data );
}

QString s = "....";
QByteArray d = s.toUtf8();
hi( d.constData() );


Cheers,
_[/QUOTE]

Btw this code is the .cpp or the .c file ?
I do not need to declare a QString now . as i am just using the ui->lineEdit->text(); to get the data

anda_skoa
2nd July 2013, 15:10
Btw this code is the .cpp or the .c file ?

In cpp of course, it contains C++ code (usage of classes).



Now I want to pass the QByteArray data to the c file .

No, you want to pass the const char* to the C file. The QByteArray type is a C++ class not a C type

Cheers,
_

020394
4th July 2013, 13:53
Thanks anda_soka for your help, I now understand how to pass the QByteArray to my .c file already . The main problem is I was not sure of how to use the extern properly. Thanks to Santosh Reddy for giving me the link to the extern information . Really appreciate your help