Using a Qt library from a C program

sashoalm

Qt is great for building applications, but once or twice I have wanted to show a nice Qt dialog from an existing application that is written in pure C.

I want to describe how to create a Qt-based shared or static library that is usable by a Qt-unaware program. To narrow down the problem, let's assume that we call a function that shows some dialogs or widgets, and does not return until all dialogs have been closed. Otherwise it can't be done without some 'cooperation' from the hosting process - it has to at least run the message pump for Qt.

In this example I will use the Windows program rundll32.exe to load and execute a function that shows the Qt about dialog.

sashoalm

Let's say our DLL is called MyQtBasedDll.dll, and it has a single exported function - void MyEntryPoint().

We will run it using the command line rundll32.exe MyQtBasedDll.dll,MyEntryPoint. Note you need to give full paths for the exe and dll.

In order for rundll32.exe to load and use the program, MyEntryPoint() should have a C-calling convention. Additionally, we need a QApplication instance to do any GUI stuff, and rundll32.exe will obviously not create it for us.

To create the Qt library with Qt Creator, we can choose File->New File or Project->Libraries->C++ Library, and the project name should be MyQtBasedDll.

The code of the MyEntryPoint() function is pretty simple:

extern "C"
{
__declspec(dllexport) void MyEntryPoint()
{
    if (!QApplication::instance()) {
        QApplication a(__argc, __argv);
        QMessageBox::aboutQt(0);
    } else {
        QMessageBox::aboutQt(0);
    }
}
}

This function can be called multiple times by the same exe, and each time a new QApplication object is constructed, only for the duration of the function. Note that we check for an existing instance so our DLL will work even when called by a Qt application.

Now all you need to do is substitute QMessageBox::aboutQt(0); with your function that does useful stuff!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Link static Qt library to c program

From Dev

Separating Library Includes From Program Using Library

From Dev

Compile a C program in Linux using shared library

From Dev

C++ program using a mangled symbol from a C library causing an undefined symbol

From Dev

MySQL: Library not loaded from C program

From Dev

Insert data into mysql table from c++ program using OTL library

From Dev

Using aar Library with Qt

From Dev

Using aar Library with Qt

From Dev

Run a method from a C++ program with Qt without writing entire program in Qt

From Dev

Can I use a C++ template library from a C program?

From Dev

Build C++ library on OS X/Mac using Qt

From Dev

QT/QML c++ Program crash on access a QList from QML

From Dev

Using C++ shared library from C

From Dev

Using C++ shared library from C

From Dev

Cant run a c++ program using libvlc library (osx)

From Dev

How to run C program using pari library with gcc?

From Dev

How to compile a C program that includes a header from another library?

From Dev

Including only minimal Qt library with a program

From Dev

Using a dll in a qt application program

From Dev

Is this error from Qt or my program?

From Dev

Matrix library or program using Strings

From Dev

how to use main c++ program to build a sound related library (without using any third party library)

From Dev

Cmake and readline library in c program

From Dev

C++ program using a C library headers is recognizing "this" as a keyword. Extern "C" error?

From Dev

Calling a shared library from Haskell via FFI blocks, while it doesn't when linked from a C program

From Dev

Why is it I can use C++ code in a library and call from a C program. How does that work?

From Dev

Why is it I can use C++ code in a library and call from a C program. How does that work?

From Dev

symbol lookup error when using my Qt plugin from inside of shared library

From Dev

C++, Qt Putting data into Mysql database: through PHP script OR directly from the program

Related Related

  1. 1

    Link static Qt library to c program

  2. 2

    Separating Library Includes From Program Using Library

  3. 3

    Compile a C program in Linux using shared library

  4. 4

    C++ program using a mangled symbol from a C library causing an undefined symbol

  5. 5

    MySQL: Library not loaded from C program

  6. 6

    Insert data into mysql table from c++ program using OTL library

  7. 7

    Using aar Library with Qt

  8. 8

    Using aar Library with Qt

  9. 9

    Run a method from a C++ program with Qt without writing entire program in Qt

  10. 10

    Can I use a C++ template library from a C program?

  11. 11

    Build C++ library on OS X/Mac using Qt

  12. 12

    QT/QML c++ Program crash on access a QList from QML

  13. 13

    Using C++ shared library from C

  14. 14

    Using C++ shared library from C

  15. 15

    Cant run a c++ program using libvlc library (osx)

  16. 16

    How to run C program using pari library with gcc?

  17. 17

    How to compile a C program that includes a header from another library?

  18. 18

    Including only minimal Qt library with a program

  19. 19

    Using a dll in a qt application program

  20. 20

    Is this error from Qt or my program?

  21. 21

    Matrix library or program using Strings

  22. 22

    how to use main c++ program to build a sound related library (without using any third party library)

  23. 23

    Cmake and readline library in c program

  24. 24

    C++ program using a C library headers is recognizing "this" as a keyword. Extern "C" error?

  25. 25

    Calling a shared library from Haskell via FFI blocks, while it doesn't when linked from a C program

  26. 26

    Why is it I can use C++ code in a library and call from a C program. How does that work?

  27. 27

    Why is it I can use C++ code in a library and call from a C program. How does that work?

  28. 28

    symbol lookup error when using my Qt plugin from inside of shared library

  29. 29

    C++, Qt Putting data into Mysql database: through PHP script OR directly from the program

HotTag

Archive