Results 1 to 11 of 11

Thread: Accessing UI Widget data from .ccp file in .c file

  1. #1
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Accessing UI Widget data from .ccp file in .c file

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    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,
    _

  3. #3
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    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

    Qt Code:
    1. QByteArray data = ui->lineEdit->text().toUtf8();
    2. printf( "%s", data.constData() );
    To copy to clipboard, switch view to plain text mode 
    using printf() as an example for a C API.


    Cheers,
    _

  5. #5
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    Quote Originally Posted by anda_skoa View Post
    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

    Qt Code:
    1. QByteArray data = ui->lineEdit->text().toUtf8();
    2. printf( "%s", data.constData() );
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Accessing UI Widget data from .ccp file in .c file

    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    Quote Originally Posted by 020394 View Post
    Thanks Again , I now understand how to convert the QString to a array.
    Then why don't you use that?

    Qt Code:
    1. void hi( const char *data )
    2. {
    3. printf( "%s", data );
    4. }
    5.  
    6. QString s = "....";
    7. QByteArray d = s.toUtf8();
    8. hi( d.constData() );
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. #8
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    Quote Originally Posted by anda_skoa View Post
    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
    Qt Code:
    1. QByteArray data=ui->lineEdit->text().toUtf8();
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. extern "C" QByteArray data;
    To copy to clipboard, switch view to plain text mode 

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

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

  9. #9
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    Qt Code:
    1. void hi( const char *data )
    2. {
    3. printf( "%s", data );
    4. }
    5.  
    6. QString s = "....";
    7. QByteArray d = s.toUtf8();
    8. hi( d.constData() );
    To copy to clipboard, switch view to plain text mode 

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    Quote Originally Posted by 020394 View Post
    Btw this code is the .cpp or the .c file ?
    In cpp of course, it contains C++ code (usage of classes).

    Quote Originally Posted by 020394 View Post
    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,
    _

  11. #11
    Join Date
    May 2013
    Posts
    47
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Accessing UI Widget data from .ccp file in .c file

    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

Similar Threads

  1. Accessing data of .qml file
    By jeff28 in forum Newbie
    Replies: 1
    Last Post: 29th August 2012, 15:15
  2. accessing .ui file from another C file
    By nackasha in forum Newbie
    Replies: 2
    Last Post: 27th July 2011, 23:25
  3. Replies: 3
    Last Post: 8th June 2011, 06:36
  4. Multiple File Data accessing
    By hasnatzaidi in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 16:34
  5. Transferring data input from a Widget to a cpp file
    By Ahmad in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2007, 09:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.