Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

WinApi and QT - how use



  • Hello,

    I would like to write "hello world" using QT and WinApi: create simple Window. My code:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        WNDCLASSEXW wc;
        wc.cbSize = sizeof( WNDCLASSEXW );
        wc.style = 0;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance =  (HINSTANCE)::GetModuleHandle(NULL);
        wc.hIcon = 0;
        wc.hCursor = 0;
        wc.hbrBackground =0;
        wc.lpszMenuName = NULL;
        wc.lpszClassName = (LPCWSTR)"myClass";
        wc.hIconSm = 0;
        qInfo()<<RegisterClassExW(&wc);
        HWND window = CreateWindowExW(WS_EX_CLIENTEDGE, (LPCWSTR)"myClass", (LPCWSTR)"title", WS_EX_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,300,300,NULL, NULL, (HINSTANCE)::GetModuleHandle(NULL), NULL);
        qInfo()<<window;
        ShowWindow(window ,SW_SHOW);
        UpdateWindow( window );
    }
    

    I get numbers ( qInfo() ):
    49696
    0x0

    So RegisterClassExW is successed and CreateWindowExW isn't is.

    Maybe (HINSTANCE)::GetModuleHandle(NULL) is bad?



  • @TomNow99
    What in the world is the point of this? The whole point about developing in Qt is so that you not use a platform-specific api like WinApi, you use Qt methods instead. I can only imagine you have not understood this. The MainWindow instance you are in is already a main window, all you have to do is show() it. If you really want to do your way, give up on Qt, just use e.g. Visual Studio and a Windows SDK.



  • I know that I can use QDialog to create Window instead of WinApi. But I think this example is very easy, so I think there is another way...