How to use C code in qt?
-
wrote on 25 Jul 2011, 18:02 last edited by
If the C-based code is a standalone program, it might be possible to use QProcess to run it, and then capture the output and show it in a widget. Not sure if that's a viable option for you or not, though.
-
wrote on 15 Aug 2011, 23:03 last edited by
shakthi
With some C you will need to use extern "C" and you might have to
convert some C types to Qt C++ . Smae goes with C++ std to Qt.For a regular C string, just use the main constructor:
@char name[] = "Stack Overflow";
qDebug() << QString qname(name);
@
or@std::string str("character/test/raw");
qDebug() << QString::fromStdString(str);
@
or@std::string str("character/test/raw");
QString qstr(str.c_str());
qDebug() << qstr;@Edit: please use @ tags around code sections; Andre
-
wrote on 18 Nov 2011, 18:39 last edited by
I have used C code withing Qt without any problem, to be exact I have used the languages C (with OpenSSL library), C++, Qt, QML, Javascript, HTML in a single application without too much effort. I don't think that you have to do something to mix C, C++ and Qt.
OK, now for example: the following code will use C and Qt.
@
#include <QApplication>
#include <QLabel>//C Header files
#include <stdlib.h>int main(int argc, char* argv[])
{
int 1;
char a[] = "64";
i = atoi(a);//C function to convert char to integer
QApplication app(argc, argv);
QLabel label(QString("Actual char: " + a + "Integer :" + QString::fromNumber(i) )); //char to QString
return app.exec();
}
@All you h
-
if you want to keep backend and frontend separately you either need to implement some data protocol (dbus maybe or pipes) to transfer data from backend to frontend. If you want simply show all output in single QTextEdit widget you can redirect stdout somewhere and show it in your widget, but first suggestion is more correct way (and much more flexible).
wrote on 29 Oct 2018, 10:58 last edited byHi@DenisKormalev I need to interface C driver code to QT GUI, Can you show me a snippet to achieve the linking process from C source code to QT GUI
Ex unsigned int a[100]={ 25, 17 , 10, 6, -5 , -6, -8 , 10, ......... };
I am required to send this array data to QT GUI through Slots, from C source file to QT Slots is that possible?
If possible please someone shows the code snippet in QT that will help me a lot
-
Hi@DenisKormalev I need to interface C driver code to QT GUI, Can you show me a snippet to achieve the linking process from C source code to QT GUI
Ex unsigned int a[100]={ 25, 17 , 10, 6, -5 , -6, -8 , 10, ......... };
I am required to send this array data to QT GUI through Slots, from C source file to QT Slots is that possible?
If possible please someone shows the code snippet in QT that will help me a lot
@vivekyuvan said in How to use C code in qt?:
I am required to send this array data to QT GUI through Slots, from C source file to QT Slots is that possible?
No it's not as your C driver code isn't based on Qt, right? You will need to call the interface functions of your C driver in your app.
-
wrote on 30 Oct 2018, 05:23 last edited by
Hi@jsulm
I like to implement frontend alone in qt and backend is in C. The backend code was written in C its best to keep the code in C because its a driver code. so now my concern is can I communicate my driver code with frontend QT GUI?// You will need to call the interface functions of your C driver in your app.//
some me some snipet to interface C code to qt with QDBUS IPC protocol
-
Hi@jsulm
I like to implement frontend alone in qt and backend is in C. The backend code was written in C its best to keep the code in C because its a driver code. so now my concern is can I communicate my driver code with frontend QT GUI?// You will need to call the interface functions of your C driver in your app.//
some me some snipet to interface C code to qt with QDBUS IPC protocol
@vivekyuvan You can use C code in a C++ project. I don't really understand what the problem is. Either add your C code directly to your project or add it as a shared library.
I can't give you any snippet of code to use this C code as I have no idea how it looks like. Take a look at its interface/API, read its documentation and find out how to use it. I can't/will not do it for you. -
wrote on 30 Oct 2018, 12:24 last edited by
Hi@jsulm Ill explain in detail, first of all, I cant add my C driver code as the shared library because I am Implement the lightweight GUI application. hope you can understand lightweight GUI application.
A lot of driver code participate in my project like UART Driver and I2C Driver and SPI Driver, all driver code was written in C code.
So I can't wrap my driver code into QT Source code as well as its difficult to add all driver code as the shared lib in QT Source code
Because I am Implementing lightweight GUI application. My frontend QT code is just a window to monitor all parameters
So I am forced to create my driver code as like as demon process in the background. Then I catch the required data in QT QProcess class in the frontend code Is that possible?
-
Hi@jsulm Ill explain in detail, first of all, I cant add my C driver code as the shared library because I am Implement the lightweight GUI application. hope you can understand lightweight GUI application.
A lot of driver code participate in my project like UART Driver and I2C Driver and SPI Driver, all driver code was written in C code.
So I can't wrap my driver code into QT Source code as well as its difficult to add all driver code as the shared lib in QT Source code
Because I am Implementing lightweight GUI application. My frontend QT code is just a window to monitor all parameters
So I am forced to create my driver code as like as demon process in the background. Then I catch the required data in QT QProcess class in the frontend code Is that possible?
@vivekyuvan How is your "driver code" communicating with the outside world? If you start it as a deamon you need a way to communicate with it. So, how does your driver communicate? Does it have to run as a daemon? I suggested two solutions: one was shared library (I still don't understand why you think it is not possible), or add the C code to your Qt app. Can't you simply add C code to your Qt app?
-
Hi@jsulm Ill explain in detail, first of all, I cant add my C driver code as the shared library because I am Implement the lightweight GUI application. hope you can understand lightweight GUI application.
A lot of driver code participate in my project like UART Driver and I2C Driver and SPI Driver, all driver code was written in C code.
So I can't wrap my driver code into QT Source code as well as its difficult to add all driver code as the shared lib in QT Source code
Because I am Implementing lightweight GUI application. My frontend QT code is just a window to monitor all parameters
So I am forced to create my driver code as like as demon process in the background. Then I catch the required data in QT QProcess class in the frontend code Is that possible?
I cant add my C driver code as the shared library because I am Implement the lightweight GUI application
Both options are not mutally exclusive.
You really only have three options:
- Add your C code directly in the Qt project (i.e. link it statically)
- Add your C code as a shared library (i.e. link it dynamically)
- Let your C code run as a standalone program (a daemon) and communicate with it by some interprocess communication method.
Note that for option 3 the daemon must be started by someone, in order to communicate with your GUI program.
Regards