Results 1 to 5 of 5

Thread: how to make python Qwidget for c++ QtMainWindow?

  1. #1
    Join Date
    Feb 2025
    Posts
    4
    Thanks
    1

    Question how to make python Qwidget for c++ QtMainWindow?

    I have a c++ QMainWindow, and wants to develop more window designs qwidget with python for it.
    Perhaps somehow expose QMainwindow's add widget function via CPython?
    Could someone point to a example or tell me how to do so? Much appreciated!
    Last edited by eue; 19th February 2025 at 10:36.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,304
    Thanks
    313
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to make python Qwidget for c++ QtMainWindow?

    Perhaps somehow expose QMainWindow's add widget function via CPython?
    I think you mean QMainWindow::setCentralWidget(). Since that is not a C++ virtual method, you can't override it in a class derived from QMainWindow. If what you are interested in doing is to use a central widget created in Python, then I think you could probably write a method in your own MainWindow class, call it something like "setMainWidget" and expose it via CPython.

    I do not know what a QWidget created in Python "looks like" on the C++ side. If there is a way to convert it into a QWidget pointer, then you can pass that pointer into QMainWindow::setCentralWidget().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2025
    Posts
    4
    Thanks
    1

    Default Re: how to make python Qwidget for c++ QtMainWindow?

    exactly my man.

    I would like a c++ pointer to the created python QWidget to be add to my c++ mainwindow.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,304
    Thanks
    313
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to make python Qwidget for c++ QtMainWindow?

    If you are using PySide, then the PySide QWidget has a method winId() which returns a wId handle to the underlying window system window handle (like an HWnd in native Windows code).

    On the C++ side, the static method QWidget::find() takes a wId as argument and returns a QWidget pointer. So this could be the solution for you.
    Last edited by d_stranz; 20th February 2025 at 17:56.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Feb 2025
    Location
    Camden, OH
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to make python Qwidget for c++ QtMainWindow?

    You can embed a Python-based QWidget into a C++ QMainWindow by exposing the QMainWindow's addWidget function using Python bindings. This is typically done with PySide6 or PyQt6.

    Steps:
    1. Expose C++ functions to Python: Use Shiboken (for PySide6) or SIP (for PyQt6) to create bindings for QMainWindow’s addWidget function.
    2. Embed Python in C++: Use CPython API (Py_Initialize(), PyImport_ImportModule(), etc.) to run Python code inside C++.
    3. Create Python UI Components: Design QWidget in Python and expose it to C++.
    4. Load Python Widgets in C++: Use the exposed functions to add Python-based QWidget dynamically.

Similar Threads

  1. Python / PyQt5 MDI Window focus problem
    By apereira in forum Newbie
    Replies: 5
    Last Post: 22nd September 2015, 00:29
  2. Replies: 3
    Last Post: 14th February 2012, 16:47
  3. initial size of docked window
    By GrahamLabdon in forum Newbie
    Replies: 3
    Last Post: 28th February 2011, 16:33
  4. Docked main window
    By ahmed963 in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2008, 22:45
  5. Window OS make distclean && qmake && make one line
    By patrik08 in forum General Programming
    Replies: 4
    Last Post: 22nd March 2007, 11:43

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.