// ============================================================
// vqt6/_bridge_widgets.cpp — QtWidgets C 桥接层实现
// ============================================================

#include "_bridge.h"
#include "_bridge_widgets.h"
#include "_bridge_gui.h"
#include <QApplication>
#include <QWidget>
#include <QDialog>
#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QPlainTextEdit>
#include <QCheckBox>
#include <QRadioButton>
#include <QComboBox>
#include <QListWidget>
#include <QProgressBar>
#include <QSlider>
#include <QSpinBox>
#include <QLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include <QStackedWidget>
#include <QTabWidget>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QStatusBar>
#include <QToolBar>
#include <QMessageBox>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QDesktopWidget>
#include <QScreen>
#include <QStringList>
#include <QFont>
#include <QColor>
#include <QPixmap>
#include <QIcon>
#include <QCursor>
#include <QKeySequence>

// ============================================================
// QApplication
// ============================================================
extern "C" VQT6_API void* vqt6_qapplication_create(int argc, char** argv) {
    return new QApplication(argc, argv);
}

extern "C" VQT6_API void* vqt6_qapplication_instance(void) {
    return QApplication::instance();
}

extern "C" VQT6_API int vqt6_qapplication_exec(void* app) {
    if (app == nullptr) return -1;
    return static_cast<QApplication*>(app)->exec();
}

extern "C" VQT6_API void vqt6_qapplication_quit(void* app) {
    if (app == nullptr) return;
    static_cast<QApplication*>(app)->quit();
}

extern "C" VQT6_API void vqt6_qapplication_exit(void* app, int exit_code) {
    if (app == nullptr) return;
    static_cast<QApplication*>(app)->exit(exit_code);
}

extern "C" VQT6_API void vqt6_qapplication_process_events(void* app) {
    if (app == nullptr) return;
    static_cast<QApplication*>(app)->processEvents();
}

extern "C" VQT6_API int vqt6_qapplication_desktop_width(void* app) {
    if (app == nullptr) return 0;
    QScreen* screen = static_cast<QApplication*>(app)->primaryScreen();
    return screen != nullptr ? screen->size().width() : 0;
}

extern "C" VQT6_API int vqt6_qapplication_desktop_height(void* app) {
    if (app == nullptr) return 0;
    QScreen* screen = static_cast<QApplication*>(app)->primaryScreen();
    return screen != nullptr ? screen->size().height() : 0;
}

extern "C" VQT6_API void vqt6_qapplication_set_quit_on_last_window_closed(void* app, int quit) {
    if (app == nullptr) return;
    static_cast<QApplication*>(app)->setQuitOnLastWindowClosed(quit != 0);
}

extern "C" VQT6_API int vqt6_qapplication_quit_on_last_window_closed(void* app) {
    if (app == nullptr) return 0;
    return QApplication::quitOnLastWindowClosed() ? 1 : 0;
}

// ============================================================
// QWidget
// ============================================================
extern "C" VQT6_API void* vqt6_qwidget_create(void) {
    return new QWidget();
}

extern "C" VQT6_API void* vqt6_qwidget_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QWidget(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qwidget_show(void* widget) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->show();
}

extern "C" VQT6_API void vqt6_qwidget_hide(void* widget) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->hide();
}

extern "C" VQT6_API int vqt6_qwidget_close(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->close() ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qwidget_is_visible(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->isVisible() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qwidget_set_visible(void* widget, int visible) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setVisible(visible != 0);
}

extern "C" VQT6_API void vqt6_qwidget_set_geometry(void* widget, int x, int y, int w, int h) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setGeometry(x, y, w, h);
}

extern "C" VQT6_API void vqt6_qwidget_geometry(void* widget, int* x, int* y, int* w, int* h) {
    if (widget == nullptr) return;
    QRect r = static_cast<QWidget*>(widget)->geometry();
    if (x != nullptr) *x = r.x();
    if (y != nullptr) *y = r.y();
    if (w != nullptr) *w = r.width();
    if (h != nullptr) *h = r.height();
}

extern "C" VQT6_API void vqt6_qwidget_resize(void* widget, int w, int h) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->resize(w, h);
}

extern "C" VQT6_API void vqt6_qwidget_size(void* widget, int* w, int* h) {
    if (widget == nullptr) return;
    QSize s = static_cast<QWidget*>(widget)->size();
    if (w != nullptr) *w = s.width();
    if (h != nullptr) *h = s.height();
}

extern "C" VQT6_API void vqt6_qwidget_move(void* widget, int x, int y) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->move(x, y);
}

extern "C" VQT6_API void vqt6_qwidget_pos(void* widget, int* x, int* y) {
    if (widget == nullptr) return;
    QPoint p = static_cast<QWidget*>(widget)->pos();
    if (x != nullptr) *x = p.x();
    if (y != nullptr) *y = p.y();
}

extern "C" VQT6_API int vqt6_qwidget_width(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->width();
}

extern "C" VQT6_API int vqt6_qwidget_height(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->height();
}

extern "C" VQT6_API int vqt6_qwidget_x(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->x();
}

extern "C" VQT6_API int vqt6_qwidget_y(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->y();
}

extern "C" VQT6_API void vqt6_qwidget_set_window_title(void* widget, void* title) {
    if (widget == nullptr || title == nullptr) return;
    static_cast<QWidget*>(widget)->setWindowTitle(*static_cast<QString*>(title));
}

extern "C" VQT6_API void* vqt6_qwidget_window_title(void* widget) {
    if (widget == nullptr) return nullptr;
    return new QString(static_cast<QWidget*>(widget)->windowTitle());
}

extern "C" VQT6_API void vqt6_qwidget_set_text(void* widget, void* text) {
    if (widget == nullptr || text == nullptr) return;
    static_cast<QWidget*>(widget)->setWindowTitle(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qwidget_text(void* widget) {
    if (widget == nullptr) return nullptr;
    return new QString(static_cast<QWidget*>(widget)->windowTitle());
}

extern "C" VQT6_API void vqt6_qwidget_set_enabled(void* widget, int enabled) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setEnabled(enabled != 0);
}

extern "C" VQT6_API int vqt6_qwidget_is_enabled(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->isEnabled() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qwidget_set_parent(void* widget, void* parent) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setParent(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void* vqt6_qwidget_parent_widget(void* widget) {
    if (widget == nullptr) return nullptr;
    return static_cast<QWidget*>(widget)->parentWidget();
}

extern "C" VQT6_API void vqt6_qwidget_set_focus(void* widget) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setFocus();
}

extern "C" VQT6_API void vqt6_qwidget_clear_focus(void* widget) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->clearFocus();
}

extern "C" VQT6_API int vqt6_qwidget_has_focus(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<QWidget*>(widget)->hasFocus() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qwidget_set_focus_policy(void* widget, int policy) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setFocusPolicy(static_cast<Qt::FocusPolicy>(policy));
}

extern "C" VQT6_API void vqt6_qwidget_update(void* widget) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->update();
}

extern "C" VQT6_API void vqt6_qwidget_repaint(void* widget) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->repaint();
}

extern "C" VQT6_API void vqt6_qwidget_set_update_enabled(void* widget, int enabled) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setUpdatesEnabled(enabled != 0);
}

extern "C" VQT6_API void vqt6_qwidget_set_window_flags(void* widget, int flags) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setWindowFlags(static_cast<Qt::WindowFlags>(flags));
}

extern "C" VQT6_API int vqt6_qwidget_window_flags(void* widget) {
    if (widget == nullptr) return 0;
    return static_cast<int>(static_cast<QWidget*>(widget)->windowFlags());
}

extern "C" VQT6_API void vqt6_qwidget_set_cursor(void* widget, void* cursor) {
    if (widget == nullptr || cursor == nullptr) return;
    static_cast<QWidget*>(widget)->setCursor(*static_cast<QCursor*>(cursor));
}

extern "C" VQT6_API void* vqt6_qwidget_cursor(void* widget) {
    if (widget == nullptr) return nullptr;
    return new QCursor(static_cast<QWidget*>(widget)->cursor());
}

extern "C" VQT6_API void vqt6_qwidget_unset_cursor(void* widget) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->unsetCursor();
}

extern "C" VQT6_API void vqt6_qwidget_set_tool_tip(void* widget, void* tip) {
    if (widget == nullptr || tip == nullptr) return;
    static_cast<QWidget*>(widget)->setToolTip(*static_cast<QString*>(tip));
}

extern "C" VQT6_API void* vqt6_qwidget_tool_tip(void* widget) {
    if (widget == nullptr) return nullptr;
    return new QString(static_cast<QWidget*>(widget)->toolTip());
}

extern "C" VQT6_API void vqt6_qwidget_set_status_tip(void* widget, void* tip) {
    if (widget == nullptr || tip == nullptr) return;
    static_cast<QWidget*>(widget)->setStatusTip(*static_cast<QString*>(tip));
}

extern "C" VQT6_API void* vqt6_qwidget_status_tip(void* widget) {
    if (widget == nullptr) return nullptr;
    return new QString(static_cast<QWidget*>(widget)->statusTip());
}

extern "C" VQT6_API void vqt6_qwidget_set_minimum_size(void* widget, int w, int h) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setMinimumSize(w, h);
}

extern "C" VQT6_API void vqt6_qwidget_set_maximum_size(void* widget, int w, int h) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setMaximumSize(w, h);
}

extern "C" VQT6_API void vqt6_qwidget_set_fixed_size(void* widget, int w, int h) {
    if (widget == nullptr) return;
    static_cast<QWidget*>(widget)->setFixedSize(w, h);
}

extern "C" VQT6_API void vqt6_qwidget_minimum_size(void* widget, int* w, int* h) {
    if (widget == nullptr) return;
    QSize s = static_cast<QWidget*>(widget)->minimumSize();
    if (w != nullptr) *w = s.width();
    if (h != nullptr) *h = s.height();
}

extern "C" VQT6_API void vqt6_qwidget_maximum_size(void* widget, int* w, int* h) {
    if (widget == nullptr) return;
    QSize s = static_cast<QWidget*>(widget)->maximumSize();
    if (w != nullptr) *w = s.width();
    if (h != nullptr) *h = s.height();
}

// ============================================================
// QDialog
// ============================================================
extern "C" VQT6_API void* vqt6_qdialog_create(void) {
    return new QDialog();
}

extern "C" VQT6_API void* vqt6_qdialog_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QDialog(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API int vqt6_qdialog_exec(void* dialog) {
    if (dialog == nullptr) return -1;
    return static_cast<QDialog*>(dialog)->exec();
}

extern "C" VQT6_API void vqt6_qdialog_done(void* dialog, int result) {
    if (dialog == nullptr) return;
    static_cast<QDialog*>(dialog)->done(result);
}

extern "C" VQT6_API void vqt6_qdialog_accept(void* dialog) {
    if (dialog == nullptr) return;
    static_cast<QDialog*>(dialog)->accept();
}

extern "C" VQT6_API void vqt6_qdialog_reject(void* dialog) {
    if (dialog == nullptr) return;
    static_cast<QDialog*>(dialog)->reject();
}

extern "C" VQT6_API void vqt6_qdialog_set_result(void* dialog, int result) {
    if (dialog == nullptr) return;
    static_cast<QDialog*>(dialog)->setResult(result);
}

extern "C" VQT6_API int vqt6_qdialog_result(void* dialog) {
    if (dialog == nullptr) return 0;
    return static_cast<QDialog*>(dialog)->result();
}

extern "C" VQT6_API void vqt6_qdialog_set_modal(void* dialog, int modal) {
    if (dialog == nullptr) return;
    static_cast<QDialog*>(dialog)->setModal(modal != 0);
}

extern "C" VQT6_API int vqt6_qdialog_is_modal(void* dialog) {
    if (dialog == nullptr) return 0;
    return static_cast<QDialog*>(dialog)->isModal() ? 1 : 0;
}

// ============================================================
// QMainWindow
// ============================================================
extern "C" VQT6_API void* vqt6_qmainwindow_create(void) {
    return new QMainWindow();
}

extern "C" VQT6_API void* vqt6_qmainwindow_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QMainWindow(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qmainwindow_set_central_widget(void* mw, void* widget) {
    if (mw == nullptr) return;
    static_cast<QMainWindow*>(mw)->setCentralWidget(static_cast<QWidget*>(widget));
}

extern "C" VQT6_API void* vqt6_qmainwindow_central_widget(void* mw) {
    if (mw == nullptr) return nullptr;
    return static_cast<QMainWindow*>(mw)->centralWidget();
}

extern "C" VQT6_API void* vqt6_qmainwindow_menu_bar(void* mw) {
    if (mw == nullptr) return nullptr;
    return static_cast<QMainWindow*>(mw)->menuBar();
}

extern "C" VQT6_API void vqt6_qmainwindow_set_menu_bar(void* mw, void* menubar) {
    if (mw == nullptr) return;
    static_cast<QMainWindow*>(mw)->setMenuBar(static_cast<QMenuBar*>(menubar));
}

extern "C" VQT6_API void* vqt6_qmainwindow_status_bar(void* mw) {
    if (mw == nullptr) return nullptr;
    return static_cast<QMainWindow*>(mw)->statusBar();
}

extern "C" VQT6_API void vqt6_qmainwindow_set_status_bar(void* mw, void* statusbar) {
    if (mw == nullptr) return;
    static_cast<QMainWindow*>(mw)->setStatusBar(static_cast<QStatusBar*>(statusbar));
}

extern "C" VQT6_API void vqt6_qmainwindow_add_tool_bar(void* mw, void* toolbar) {
    if (mw == nullptr) return;
    static_cast<QMainWindow*>(mw)->addToolBar(static_cast<QToolBar*>(toolbar));
}

extern "C" VQT6_API void vqt6_qmainwindow_add_tool_bar_break(void* mw) {
    if (mw == nullptr) return;
    static_cast<QMainWindow*>(mw)->addToolBarBreak();
}

// ============================================================
// QLabel
// ============================================================
extern "C" VQT6_API void* vqt6_qlabel_create(void) {
    return new QLabel();
}

extern "C" VQT6_API void* vqt6_qlabel_create_with_text(void* text) {
    if (text == nullptr) return nullptr;
    return new QLabel(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qlabel_create_with_text_parent(void* text, void* parent) {
    if (text == nullptr || parent == nullptr) return nullptr;
    return new QLabel(*static_cast<QString*>(text), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qlabel_set_text(void* label, void* text) {
    if (label == nullptr || text == nullptr) return;
    static_cast<QLabel*>(label)->setText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qlabel_text(void* label) {
    if (label == nullptr) return nullptr;
    return new QString(static_cast<QLabel*>(label)->text());
}

extern "C" VQT6_API void vqt6_qlabel_set_alignment(void* label, int alignment) {
    if (label == nullptr) return;
    static_cast<QLabel*>(label)->setAlignment(static_cast<Qt::Alignment>(alignment));
}

extern "C" VQT6_API int vqt6_qlabel_alignment(void* label) {
    if (label == nullptr) return 0;
    return static_cast<int>(static_cast<QLabel*>(label)->alignment());
}

extern "C" VQT6_API void vqt6_qlabel_set_pixmap(void* label, void* pixmap) {
    if (label == nullptr || pixmap == nullptr) return;
    static_cast<QLabel*>(label)->setPixmap(*static_cast<QPixmap*>(pixmap));
}

extern "C" VQT6_API void* vqt6_qlabel_pixmap(void* label) {
    if (label == nullptr) return nullptr;
    return new QPixmap(static_cast<QLabel*>(label)->pixmap());
}

extern "C" VQT6_API void vqt6_qlabel_set_indent(void* label, int indent) {
    if (label == nullptr) return;
    static_cast<QLabel*>(label)->setIndent(indent);
}

extern "C" VQT6_API void vqt6_qlabel_set_margin(void* label, int margin) {
    if (label == nullptr) return;
    static_cast<QLabel*>(label)->setMargin(margin);
}

extern "C" VQT6_API void vqt6_qlabel_set_word_wrap(void* label, int wrap) {
    if (label == nullptr) return;
    static_cast<QLabel*>(label)->setWordWrap(wrap != 0);
}

extern "C" VQT6_API int vqt6_qlabel_word_wrap(void* label) {
    if (label == nullptr) return 0;
    return static_cast<QLabel*>(label)->wordWrap() ? 1 : 0;
}

// ============================================================
// QPushButton
// ============================================================
extern "C" VQT6_API void* vqt6_qpushbutton_create(void) {
    return new QPushButton();
}

extern "C" VQT6_API void* vqt6_qpushbutton_create_with_text(void* text) {
    if (text == nullptr) return nullptr;
    return new QPushButton(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qpushbutton_create_with_text_parent(void* text, void* parent) {
    if (text == nullptr || parent == nullptr) return nullptr;
    return new QPushButton(*static_cast<QString*>(text), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qpushbutton_set_text(void* btn, void* text) {
    if (btn == nullptr || text == nullptr) return;
    static_cast<QPushButton*>(btn)->setText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qpushbutton_text(void* btn) {
    if (btn == nullptr) return nullptr;
    return new QString(static_cast<QPushButton*>(btn)->text());
}

extern "C" VQT6_API void vqt6_qpushbutton_set_icon(void* btn, void* icon) {
    if (btn == nullptr || icon == nullptr) return;
    static_cast<QPushButton*>(btn)->setIcon(*static_cast<QIcon*>(icon));
}

extern "C" VQT6_API void* vqt6_qpushbutton_icon(void* btn) {
    if (btn == nullptr) return nullptr;
    return new QIcon(static_cast<QPushButton*>(btn)->icon());
}

extern "C" VQT6_API void vqt6_qpushbutton_set_default(void* btn, int is_default) {
    if (btn == nullptr) return;
    static_cast<QPushButton*>(btn)->setDefault(is_default != 0);
}

extern "C" VQT6_API int vqt6_qpushbutton_is_default(void* btn) {
    if (btn == nullptr) return 0;
    return static_cast<QPushButton*>(btn)->isDefault() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qpushbutton_set_flat(void* btn, int flat) {
    if (btn == nullptr) return;
    static_cast<QPushButton*>(btn)->setFlat(flat != 0);
}

extern "C" VQT6_API int vqt6_qpushbutton_is_flat(void* btn) {
    if (btn == nullptr) return 0;
    return static_cast<QPushButton*>(btn)->isFlat() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qpushbutton_set_checkable(void* btn, int checkable) {
    if (btn == nullptr) return;
    static_cast<QPushButton*>(btn)->setCheckable(checkable != 0);
}

extern "C" VQT6_API int vqt6_qpushbutton_is_checkable(void* btn) {
    if (btn == nullptr) return 0;
    return static_cast<QPushButton*>(btn)->isCheckable() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qpushbutton_set_checked(void* btn, int checked) {
    if (btn == nullptr) return;
    static_cast<QPushButton*>(btn)->setChecked(checked != 0);
}

extern "C" VQT6_API int vqt6_qpushbutton_is_checked(void* btn) {
    if (btn == nullptr) return 0;
    return static_cast<QPushButton*>(btn)->isChecked() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qpushbutton_click(void* btn) {
    if (btn == nullptr) return;
    static_cast<QPushButton*>(btn)->click();
}

// ============================================================
// QLineEdit
// ============================================================
extern "C" VQT6_API void* vqt6_qlineedit_create(void) {
    return new QLineEdit();
}

extern "C" VQT6_API void* vqt6_qlineedit_create_with_text(void* text) {
    if (text == nullptr) return nullptr;
    return new QLineEdit(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qlineedit_create_with_text_parent(void* text, void* parent) {
    if (text == nullptr || parent == nullptr) return nullptr;
    return new QLineEdit(*static_cast<QString*>(text), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void* vqt6_qlineedit_text(void* le) {
    if (le == nullptr) return nullptr;
    return new QString(static_cast<QLineEdit*>(le)->text());
}

extern "C" VQT6_API void vqt6_qlineedit_set_text(void* le, void* text) {
    if (le == nullptr || text == nullptr) return;
    static_cast<QLineEdit*>(le)->setText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qlineedit_placeholder_text(void* le) {
    if (le == nullptr) return nullptr;
    return new QString(static_cast<QLineEdit*>(le)->placeholderText());
}

extern "C" VQT6_API void vqt6_qlineedit_set_placeholder_text(void* le, void* text) {
    if (le == nullptr || text == nullptr) return;
    static_cast<QLineEdit*>(le)->setPlaceholderText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qlineedit_set_max_length(void* le, int max) {
    if (le == nullptr) return;
    static_cast<QLineEdit*>(le)->setMaxLength(max);
}

extern "C" VQT6_API int vqt6_qlineedit_max_length(void* le) {
    if (le == nullptr) return 0;
    return static_cast<QLineEdit*>(le)->maxLength();
}

extern "C" VQT6_API void vqt6_qlineedit_set_read_only(void* le, int ro) {
    if (le == nullptr) return;
    static_cast<QLineEdit*>(le)->setReadOnly(ro != 0);
}

extern "C" VQT6_API int vqt6_qlineedit_is_read_only(void* le) {
    if (le == nullptr) return 0;
    return static_cast<QLineEdit*>(le)->isReadOnly() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qlineedit_set_echo_mode(void* le, int mode) {
    if (le == nullptr) return;
    static_cast<QLineEdit*>(le)->setEchoMode(static_cast<QLineEdit::EchoMode>(mode));
}

extern "C" VQT6_API int vqt6_qlineedit_echo_mode(void* le) {
    if (le == nullptr) return 0;
    return static_cast<int>(static_cast<QLineEdit*>(le)->echoMode());
}

extern "C" VQT6_API void vqt6_qlineedit_select_all(void* le) {
    if (le == nullptr) return;
    static_cast<QLineEdit*>(le)->selectAll();
}

extern "C" VQT6_API void vqt6_qlineedit_clear(void* le) {
    if (le == nullptr) return;
    static_cast<QLineEdit*>(le)->clear();
}

// ============================================================
// QTextEdit / QPlainTextEdit
// ============================================================
extern "C" VQT6_API void* vqt6_qtextedit_create(void) {
    return new QTextEdit();
}

extern "C" VQT6_API void* vqt6_qtextedit_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QTextEdit(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void* vqt6_qtextedit_to_plain_text(void* te) {
    if (te == nullptr) return nullptr;
    return new QString(static_cast<QTextEdit*>(te)->toPlainText());
}

extern "C" VQT6_API void vqt6_qtextedit_set_plain_text(void* te, void* text) {
    if (te == nullptr || text == nullptr) return;
    static_cast<QTextEdit*>(te)->setPlainText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qtextedit_set_html(void* te, void* html) {
    if (te == nullptr || html == nullptr) return;
    static_cast<QTextEdit*>(te)->setHtml(*static_cast<QString*>(html));
}

extern "C" VQT6_API void* vqt6_qtextedit_to_html(void* te) {
    if (te == nullptr) return nullptr;
    return new QString(static_cast<QTextEdit*>(te)->toHtml());
}

extern "C" VQT6_API void vqt6_qtextedit_append(void* te, void* text) {
    if (te == nullptr || text == nullptr) return;
    static_cast<QTextEdit*>(te)->append(*static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qtextedit_clear(void* te) {
    if (te == nullptr) return;
    static_cast<QTextEdit*>(te)->clear();
}

extern "C" VQT6_API void vqt6_qtextedit_set_read_only(void* te, int ro) {
    if (te == nullptr) return;
    static_cast<QTextEdit*>(te)->setReadOnly(ro != 0);
}

extern "C" VQT6_API int vqt6_qtextedit_is_read_only(void* te) {
    if (te == nullptr) return 0;
    return static_cast<QTextEdit*>(te)->isReadOnly() ? 1 : 0;
}

extern "C" VQT6_API void* vqt6_qplaintextedit_create(void) {
    return new QPlainTextEdit();
}

extern "C" VQT6_API void* vqt6_qplaintextedit_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QPlainTextEdit(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void* vqt6_qplaintextedit_to_plain_text(void* pte) {
    if (pte == nullptr) return nullptr;
    return new QString(static_cast<QPlainTextEdit*>(pte)->toPlainText());
}

extern "C" VQT6_API void vqt6_qplaintextedit_set_plain_text(void* pte, void* text) {
    if (pte == nullptr || text == nullptr) return;
    static_cast<QPlainTextEdit*>(pte)->setPlainText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qplaintextedit_append(void* pte, void* text) {
    if (pte == nullptr || text == nullptr) return;
    static_cast<QPlainTextEdit*>(pte)->appendPlainText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qplaintextedit_clear(void* pte) {
    if (pte == nullptr) return;
    static_cast<QPlainTextEdit*>(pte)->clear();
}

extern "C" VQT6_API void vqt6_qplaintextedit_set_read_only(void* pte, int ro) {
    if (pte == nullptr) return;
    static_cast<QPlainTextEdit*>(pte)->setReadOnly(ro != 0);
}

extern "C" VQT6_API int vqt6_qplaintextedit_line_count(void* pte) {
    if (pte == nullptr) return 0;
    return static_cast<QPlainTextEdit*>(pte)->blockCount();
}

// ============================================================
// QCheckBox / QRadioButton
// ============================================================
extern "C" VQT6_API void* vqt6_qcheckbox_create(void) {
    return new QCheckBox();
}

extern "C" VQT6_API void* vqt6_qcheckbox_create_with_text(void* text) {
    if (text == nullptr) return nullptr;
    return new QCheckBox(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qcheckbox_create_with_text_parent(void* text, void* parent) {
    if (text == nullptr || parent == nullptr) return nullptr;
    return new QCheckBox(*static_cast<QString*>(text), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qcheckbox_set_checked(void* cb, int checked) {
    if (cb == nullptr) return;
    static_cast<QCheckBox*>(cb)->setChecked(checked != 0);
}

extern "C" VQT6_API int vqt6_qcheckbox_is_checked(void* cb) {
    if (cb == nullptr) return 0;
    return static_cast<QCheckBox*>(cb)->isChecked() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qcheckbox_set_text(void* cb, void* text) {
    if (cb == nullptr || text == nullptr) return;
    static_cast<QCheckBox*>(cb)->setText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qcheckbox_text(void* cb) {
    if (cb == nullptr) return nullptr;
    return new QString(static_cast<QCheckBox*>(cb)->text());
}

extern "C" VQT6_API void vqt6_qcheckbox_set_tristate(void* cb, int tristate) {
    if (cb == nullptr) return;
    static_cast<QCheckBox*>(cb)->setTristate(tristate != 0);
}

extern "C" VQT6_API int vqt6_qcheckbox_is_tristate(void* cb) {
    if (cb == nullptr) return 0;
    return static_cast<QCheckBox*>(cb)->isTristate() ? 1 : 0;
}

extern "C" VQT6_API void* vqt6_qradiobutton_create(void) {
    return new QRadioButton();
}

extern "C" VQT6_API void* vqt6_qradiobutton_create_with_text(void* text) {
    if (text == nullptr) return nullptr;
    return new QRadioButton(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qradiobutton_create_with_text_parent(void* text, void* parent) {
    if (text == nullptr || parent == nullptr) return nullptr;
    return new QRadioButton(*static_cast<QString*>(text), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qradiobutton_set_checked(void* rb, int checked) {
    if (rb == nullptr) return;
    static_cast<QRadioButton*>(rb)->setChecked(checked != 0);
}

extern "C" VQT6_API int vqt6_qradiobutton_is_checked(void* rb) {
    if (rb == nullptr) return 0;
    return static_cast<QRadioButton*>(rb)->isChecked() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qradiobutton_set_text(void* rb, void* text) {
    if (rb == nullptr || text == nullptr) return;
    static_cast<QRadioButton*>(rb)->setText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qradiobutton_text(void* rb) {
    if (rb == nullptr) return nullptr;
    return new QString(static_cast<QRadioButton*>(rb)->text());
}

// ============================================================
// QComboBox
// ============================================================
extern "C" VQT6_API void* vqt6_qcombobox_create(void) {
    return new QComboBox();
}

extern "C" VQT6_API void* vqt6_qcombobox_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QComboBox(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qcombobox_add_item(void* cb, void* text) {
    if (cb == nullptr || text == nullptr) return;
    static_cast<QComboBox*>(cb)->addItem(*static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qcombobox_add_items(void* cb, void* items) {
    if (cb == nullptr || items == nullptr) return;
    static_cast<QComboBox*>(cb)->addItems(*static_cast<QStringList*>(items));
}

extern "C" VQT6_API void vqt6_qcombobox_insert_item(void* cb, int index, void* text) {
    if (cb == nullptr || text == nullptr) return;
    static_cast<QComboBox*>(cb)->insertItem(index, *static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qcombobox_remove_item(void* cb, int index) {
    if (cb == nullptr) return;
    static_cast<QComboBox*>(cb)->removeItem(index);
}

extern "C" VQT6_API void vqt6_qcombobox_clear(void* cb) {
    if (cb == nullptr) return;
    static_cast<QComboBox*>(cb)->clear();
}

extern "C" VQT6_API int vqt6_qcombobox_count(void* cb) {
    if (cb == nullptr) return 0;
    return static_cast<QComboBox*>(cb)->count();
}

extern "C" VQT6_API int vqt6_qcombobox_current_index(void* cb) {
    if (cb == nullptr) return -1;
    return static_cast<QComboBox*>(cb)->currentIndex();
}

extern "C" VQT6_API void vqt6_qcombobox_set_current_index(void* cb, int index) {
    if (cb == nullptr) return;
    static_cast<QComboBox*>(cb)->setCurrentIndex(index);
}

extern "C" VQT6_API void* vqt6_qcombobox_current_text(void* cb) {
    if (cb == nullptr) return nullptr;
    return new QString(static_cast<QComboBox*>(cb)->currentText());
}

extern "C" VQT6_API void* vqt6_qcombobox_item_text(void* cb, int index) {
    if (cb == nullptr) return nullptr;
    return new QString(static_cast<QComboBox*>(cb)->itemText(index));
}

extern "C" VQT6_API void vqt6_qcombobox_set_editable(void* cb, int editable) {
    if (cb == nullptr) return;
    static_cast<QComboBox*>(cb)->setEditable(editable != 0);
}

extern "C" VQT6_API int vqt6_qcombobox_is_editable(void* cb) {
    if (cb == nullptr) return 0;
    return static_cast<QComboBox*>(cb)->isEditable() ? 1 : 0;
}

// ============================================================
// QListWidget
// ============================================================
extern "C" VQT6_API void* vqt6_qlistwidget_create(void) {
    return new QListWidget();
}

extern "C" VQT6_API void* vqt6_qlistwidget_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QListWidget(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qlistwidget_add_item(void* lw, void* text) {
    if (lw == nullptr || text == nullptr) return;
    static_cast<QListWidget*>(lw)->addItem(*static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qlistwidget_add_items(void* lw, void* items) {
    if (lw == nullptr || items == nullptr) return;
    static_cast<QListWidget*>(lw)->addItems(*static_cast<QStringList*>(items));
}

extern "C" VQT6_API void vqt6_qlistwidget_insert_item(void* lw, int row, void* text) {
    if (lw == nullptr || text == nullptr) return;
    static_cast<QListWidget*>(lw)->insertItem(row, *static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qlistwidget_remove_item_widget(void* lw, int row) {
    if (lw == nullptr) return;
    static_cast<QListWidget*>(lw)->takeItem(row);
}

extern "C" VQT6_API void* vqt6_qlistwidget_take_item(void* lw, int row) {
    if (lw == nullptr) return nullptr;
    return static_cast<QListWidget*>(lw)->takeItem(row);
}

extern "C" VQT6_API void vqt6_qlistwidget_clear(void* lw) {
    if (lw == nullptr) return;
    static_cast<QListWidget*>(lw)->clear();
}

extern "C" VQT6_API int vqt6_qlistwidget_count(void* lw) {
    if (lw == nullptr) return 0;
    return static_cast<QListWidget*>(lw)->count();
}

extern "C" VQT6_API int vqt6_qlistwidget_current_row(void* lw) {
    if (lw == nullptr) return -1;
    return static_cast<QListWidget*>(lw)->currentRow();
}

extern "C" VQT6_API void vqt6_qlistwidget_set_current_row(void* lw, int row) {
    if (lw == nullptr) return;
    static_cast<QListWidget*>(lw)->setCurrentRow(row);
}

extern "C" VQT6_API void* vqt6_qlistwidget_current_item_text(void* lw) {
    if (lw == nullptr) return nullptr;
    QListWidgetItem* it = static_cast<QListWidget*>(lw)->currentItem();
    if (it == nullptr) return nullptr;
    return new QString(it->text());
}

extern "C" VQT6_API void* vqt6_qlistwidget_item_text(void* lw, int row) {
    if (lw == nullptr) return nullptr;
    QListWidgetItem* it = static_cast<QListWidget*>(lw)->item(row);
    if (it == nullptr) return nullptr;
    return new QString(it->text());
}

extern "C" VQT6_API void vqt6_qlistwidget_set_item_text(void* lw, int row, void* text) {
    if (lw == nullptr || text == nullptr) return;
    QListWidgetItem* it = static_cast<QListWidget*>(lw)->item(row);
    if (it != nullptr) it->setText(*static_cast<QString*>(text));
}

// ============================================================
// QProgressBar
// ============================================================
extern "C" VQT6_API void* vqt6_qprogressbar_create(void) {
    return new QProgressBar();
}

extern "C" VQT6_API void* vqt6_qprogressbar_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QProgressBar(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qprogressbar_set_value(void* pb, int value) {
    if (pb == nullptr) return;
    static_cast<QProgressBar*>(pb)->setValue(value);
}

extern "C" VQT6_API int vqt6_qprogressbar_value(void* pb) {
    if (pb == nullptr) return 0;
    return static_cast<QProgressBar*>(pb)->value();
}

extern "C" VQT6_API void vqt6_qprogressbar_set_minimum(void* pb, int min) {
    if (pb == nullptr) return;
    static_cast<QProgressBar*>(pb)->setMinimum(min);
}

extern "C" VQT6_API void vqt6_qprogressbar_set_maximum(void* pb, int max) {
    if (pb == nullptr) return;
    static_cast<QProgressBar*>(pb)->setMaximum(max);
}

extern "C" VQT6_API void vqt6_qprogressbar_set_range(void* pb, int min, int max) {
    if (pb == nullptr) return;
    static_cast<QProgressBar*>(pb)->setRange(min, max);
}

extern "C" VQT6_API int vqt6_qprogressbar_minimum(void* pb) {
    if (pb == nullptr) return 0;
    return static_cast<QProgressBar*>(pb)->minimum();
}

extern "C" VQT6_API int vqt6_qprogressbar_maximum(void* pb) {
    if (pb == nullptr) return 0;
    return static_cast<QProgressBar*>(pb)->maximum();
}

extern "C" VQT6_API void vqt6_qprogressbar_set_text_visible(void* pb, int visible) {
    if (pb == nullptr) return;
    static_cast<QProgressBar*>(pb)->setTextVisible(visible != 0);
}

extern "C" VQT6_API void vqt6_qprogressbar_set_format(void* pb, void* format) {
    if (pb == nullptr || format == nullptr) return;
    static_cast<QProgressBar*>(pb)->setFormat(*static_cast<QString*>(format));
}

extern "C" VQT6_API void vqt6_qprogressbar_reset(void* pb) {
    if (pb == nullptr) return;
    static_cast<QProgressBar*>(pb)->reset();
}

// ============================================================
// QSlider
// ============================================================
extern "C" VQT6_API void* vqt6_qslider_create(void) {
    return new QSlider();
}

extern "C" VQT6_API void* vqt6_qslider_create_with_orientation(int orientation) {
    return new QSlider(static_cast<Qt::Orientation>(orientation));
}

extern "C" VQT6_API void* vqt6_qslider_create_with_orientation_parent(int orientation, void* parent) {
    if (parent == nullptr) return nullptr;
    return new QSlider(static_cast<Qt::Orientation>(orientation), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qslider_set_value(void* s, int value) {
    if (s == nullptr) return;
    static_cast<QSlider*>(s)->setValue(value);
}

extern "C" VQT6_API int vqt6_qslider_value(void* s) {
    if (s == nullptr) return 0;
    return static_cast<QSlider*>(s)->value();
}

extern "C" VQT6_API void vqt6_qslider_set_minimum(void* s, int min) {
    if (s == nullptr) return;
    static_cast<QSlider*>(s)->setMinimum(min);
}

extern "C" VQT6_API void vqt6_qslider_set_maximum(void* s, int max) {
    if (s == nullptr) return;
    static_cast<QSlider*>(s)->setMaximum(max);
}

extern "C" VQT6_API void vqt6_qslider_set_range(void* s, int min, int max) {
    if (s == nullptr) return;
    static_cast<QSlider*>(s)->setRange(min, max);
}

extern "C" VQT6_API void vqt6_qslider_set_orientation(void* s, int orientation) {
    if (s == nullptr) return;
    static_cast<QSlider*>(s)->setOrientation(static_cast<Qt::Orientation>(orientation));
}

extern "C" VQT6_API int vqt6_qslider_orientation(void* s) {
    if (s == nullptr) return 0;
    return static_cast<int>(static_cast<QSlider*>(s)->orientation());
}

extern "C" VQT6_API void vqt6_qslider_set_single_step(void* s, int step) {
    if (s == nullptr) return;
    static_cast<QSlider*>(s)->setSingleStep(step);
}

extern "C" VQT6_API void vqt6_qslider_set_page_step(void* s, int step) {
    if (s == nullptr) return;
    static_cast<QSlider*>(s)->setPageStep(step);
}

// ============================================================
// QSpinBox
// ============================================================
extern "C" VQT6_API void* vqt6_qspinbox_create(void) {
    return new QSpinBox();
}

extern "C" VQT6_API void* vqt6_qspinbox_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QSpinBox(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API int vqt6_qspinbox_value(void* sb) {
    if (sb == nullptr) return 0;
    return static_cast<QSpinBox*>(sb)->value();
}

extern "C" VQT6_API void vqt6_qspinbox_set_value(void* sb, int value) {
    if (sb == nullptr) return;
    static_cast<QSpinBox*>(sb)->setValue(value);
}

extern "C" VQT6_API void vqt6_qspinbox_set_minimum(void* sb, int min) {
    if (sb == nullptr) return;
    static_cast<QSpinBox*>(sb)->setMinimum(min);
}

extern "C" VQT6_API void vqt6_qspinbox_set_maximum(void* sb, int max) {
    if (sb == nullptr) return;
    static_cast<QSpinBox*>(sb)->setMaximum(max);
}

extern "C" VQT6_API void vqt6_qspinbox_set_range(void* sb, int min, int max) {
    if (sb == nullptr) return;
    static_cast<QSpinBox*>(sb)->setRange(min, max);
}

extern "C" VQT6_API int vqt6_qspinbox_minimum(void* sb) {
    if (sb == nullptr) return 0;
    return static_cast<QSpinBox*>(sb)->minimum();
}

extern "C" VQT6_API int vqt6_qspinbox_maximum(void* sb) {
    if (sb == nullptr) return 0;
    return static_cast<QSpinBox*>(sb)->maximum();
}

extern "C" VQT6_API void vqt6_qspinbox_set_single_step(void* sb, int step) {
    if (sb == nullptr) return;
    static_cast<QSpinBox*>(sb)->setSingleStep(step);
}

extern "C" VQT6_API void vqt6_qspinbox_set_prefix(void* sb, void* prefix) {
    if (sb == nullptr || prefix == nullptr) return;
    static_cast<QSpinBox*>(sb)->setPrefix(*static_cast<QString*>(prefix));
}

extern "C" VQT6_API void vqt6_qspinbox_set_suffix(void* sb, void* suffix) {
    if (sb == nullptr || suffix == nullptr) return;
    static_cast<QSpinBox*>(sb)->setSuffix(*static_cast<QString*>(suffix));
}

extern "C" VQT6_API void* vqt6_qspinbox_prefix(void* sb) {
    if (sb == nullptr) return nullptr;
    return new QString(static_cast<QSpinBox*>(sb)->prefix());
}

extern "C" VQT6_API void* vqt6_qspinbox_suffix(void* sb) {
    if (sb == nullptr) return nullptr;
    return new QString(static_cast<QSpinBox*>(sb)->suffix());
}

// ============================================================
// Layouts
// ============================================================
extern "C" VQT6_API void vqt6_qlayout_add_widget(void* layout, void* widget) {
    if (layout == nullptr || widget == nullptr) return;
    static_cast<QLayout*>(layout)->addWidget(static_cast<QWidget*>(widget));
}

extern "C" VQT6_API void vqt6_qlayout_remove_widget(void* layout, void* widget) {
    if (layout == nullptr || widget == nullptr) return;
    static_cast<QLayout*>(layout)->removeWidget(static_cast<QWidget*>(widget));
}

extern "C" VQT6_API int vqt6_qlayout_count(void* layout) {
    if (layout == nullptr) return 0;
    return static_cast<QLayout*>(layout)->count();
}

extern "C" VQT6_API void* vqt6_qlayout_item_at(void* layout, int index) {
    if (layout == nullptr) return nullptr;
    QLayoutItem* item = static_cast<QLayout*>(layout)->itemAt(index);
    return item != nullptr ? item->widget() : nullptr;
}

extern "C" VQT6_API void vqt6_qlayout_set_contents_margins(void* layout, int left, int top, int right, int bottom) {
    if (layout == nullptr) return;
    static_cast<QLayout*>(layout)->setContentsMargins(left, top, right, bottom);
}

extern "C" VQT6_API void vqt6_qlayout_set_spacing(void* layout, int spacing) {
    if (layout == nullptr) return;
    static_cast<QLayout*>(layout)->setSpacing(spacing);
}

extern "C" VQT6_API int vqt6_qlayout_spacing(void* layout) {
    if (layout == nullptr) return 0;
    return static_cast<QLayout*>(layout)->spacing();
}

extern "C" VQT6_API void vqt6_qlayout_invalidate(void* layout) {
    if (layout == nullptr) return;
    static_cast<QLayout*>(layout)->invalidate();
}

extern "C" VQT6_API void* vqt6_qhboxlayout_create(void) {
    return new QHBoxLayout();
}

extern "C" VQT6_API void* vqt6_qhboxlayout_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QHBoxLayout(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qhboxlayout_add_layout(void* layout, void* child) {
    if (layout == nullptr || child == nullptr) return;
    static_cast<QHBoxLayout*>(layout)->addLayout(static_cast<QLayout*>(child));
}

extern "C" VQT6_API void vqt6_qhboxlayout_add_stretch(void* layout, int stretch) {
    if (layout == nullptr) return;
    static_cast<QHBoxLayout*>(layout)->addStretch(stretch);
}

extern "C" VQT6_API void vqt6_qhboxlayout_add_spacing(void* layout, int size) {
    if (layout == nullptr) return;
    static_cast<QHBoxLayout*>(layout)->addSpacing(size);
}

extern "C" VQT6_API void* vqt6_qvboxlayout_create(void) {
    return new QVBoxLayout();
}

extern "C" VQT6_API void* vqt6_qvboxlayout_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QVBoxLayout(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qvboxlayout_add_layout(void* layout, void* child) {
    if (layout == nullptr || child == nullptr) return;
    static_cast<QVBoxLayout*>(layout)->addLayout(static_cast<QLayout*>(child));
}

extern "C" VQT6_API void vqt6_qvboxlayout_add_stretch(void* layout, int stretch) {
    if (layout == nullptr) return;
    static_cast<QVBoxLayout*>(layout)->addStretch(stretch);
}

extern "C" VQT6_API void vqt6_qvboxlayout_add_spacing(void* layout, int size) {
    if (layout == nullptr) return;
    static_cast<QVBoxLayout*>(layout)->addSpacing(size);
}

extern "C" VQT6_API void* vqt6_qgridlayout_create(void) {
    return new QGridLayout();
}

extern "C" VQT6_API void* vqt6_qgridlayout_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QGridLayout(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qgridlayout_add_widget(void* gl, void* w, int row, int col) {
    if (gl == nullptr || w == nullptr) return;
    static_cast<QGridLayout*>(gl)->addWidget(static_cast<QWidget*>(w), row, col);
}

extern "C" VQT6_API void vqt6_qgridlayout_add_widget_with_span(void* gl, void* w, int row, int col, int row_span, int col_span) {
    if (gl == nullptr || w == nullptr) return;
    static_cast<QGridLayout*>(gl)->addWidget(static_cast<QWidget*>(w), row, col, row_span, col_span);
}

extern "C" VQT6_API void* vqt6_qformlayout_create(void) {
    return new QFormLayout();
}

extern "C" VQT6_API void* vqt6_qformlayout_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QFormLayout(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qformlayout_add_row(void* fl, void* label, void* field) {
    if (fl == nullptr || label == nullptr || field == nullptr) return;
    static_cast<QFormLayout*>(fl)->addRow(*static_cast<QString*>(label), static_cast<QWidget*>(field));
}

extern "C" VQT6_API void vqt6_qformlayout_add_row_widget_pair(void* fl, void* label, void* field) {
    if (fl == nullptr || label == nullptr || field == nullptr) return;
    static_cast<QFormLayout*>(fl)->addRow(static_cast<QWidget*>(label), static_cast<QWidget*>(field));
}

// ============================================================
// QStackedWidget / QTabWidget
// ============================================================
extern "C" VQT6_API void* vqt6_qstackedwidget_create(void) {
    return new QStackedWidget();
}

extern "C" VQT6_API void* vqt6_qstackedwidget_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QStackedWidget(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API int vqt6_qstackedwidget_add_widget(void* sw, void* w) {
    if (sw == nullptr || w == nullptr) return -1;
    return static_cast<QStackedWidget*>(sw)->addWidget(static_cast<QWidget*>(w));
}

extern "C" VQT6_API void vqt6_qstackedwidget_remove_widget(void* sw, void* w) {
    if (sw == nullptr || w == nullptr) return;
    static_cast<QStackedWidget*>(sw)->removeWidget(static_cast<QWidget*>(w));
}

extern "C" VQT6_API int vqt6_qstackedwidget_count(void* sw) {
    if (sw == nullptr) return 0;
    return static_cast<QStackedWidget*>(sw)->count();
}

extern "C" VQT6_API int vqt6_qstackedwidget_current_index(void* sw) {
    if (sw == nullptr) return -1;
    return static_cast<QStackedWidget*>(sw)->currentIndex();
}

extern "C" VQT6_API void vqt6_qstackedwidget_set_current_index(void* sw, int index) {
    if (sw == nullptr) return;
    static_cast<QStackedWidget*>(sw)->setCurrentIndex(index);
}

extern "C" VQT6_API void* vqt6_qstackedwidget_current_widget(void* sw) {
    if (sw == nullptr) return nullptr;
    return static_cast<QStackedWidget*>(sw)->currentWidget();
}

extern "C" VQT6_API void* vqt6_qstackedwidget_widget(void* sw, int index) {
    if (sw == nullptr) return nullptr;
    return static_cast<QStackedWidget*>(sw)->widget(index);
}

extern "C" VQT6_API void* vqt6_qtabwidget_create(void) {
    return new QTabWidget();
}

extern "C" VQT6_API void* vqt6_qtabwidget_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QTabWidget(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API int vqt6_qtabwidget_add_tab(void* tw, void* w, void* label) {
    if (tw == nullptr || w == nullptr || label == nullptr) return -1;
    return static_cast<QTabWidget*>(tw)->addTab(static_cast<QWidget*>(w), *static_cast<QString*>(label));
}

extern "C" VQT6_API int vqt6_qtabwidget_insert_tab(void* tw, int index, void* w, void* label) {
    if (tw == nullptr || w == nullptr || label == nullptr) return -1;
    return static_cast<QTabWidget*>(tw)->insertTab(index, static_cast<QWidget*>(w), *static_cast<QString*>(label));
}

extern "C" VQT6_API void vqt6_qtabwidget_remove_tab(void* tw, int index) {
    if (tw == nullptr) return;
    static_cast<QTabWidget*>(tw)->removeTab(index);
}

extern "C" VQT6_API int vqt6_qtabwidget_count(void* tw) {
    if (tw == nullptr) return 0;
    return static_cast<QTabWidget*>(tw)->count();
}

extern "C" VQT6_API int vqt6_qtabwidget_current_index(void* tw) {
    if (tw == nullptr) return -1;
    return static_cast<QTabWidget*>(tw)->currentIndex();
}

extern "C" VQT6_API void vqt6_qtabwidget_set_current_index(void* tw, int index) {
    if (tw == nullptr) return;
    static_cast<QTabWidget*>(tw)->setCurrentIndex(index);
}

extern "C" VQT6_API void* vqt6_qtabwidget_tab_text(void* tw, int index) {
    if (tw == nullptr) return nullptr;
    return new QString(static_cast<QTabWidget*>(tw)->tabText(index));
}

extern "C" VQT6_API void vqt6_qtabwidget_set_tab_text(void* tw, int index, void* text) {
    if (tw == nullptr || text == nullptr) return;
    static_cast<QTabWidget*>(tw)->setTabText(index, *static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qtabwidget_set_tabs_closable(void* tw, int closable) {
    if (tw == nullptr) return;
    static_cast<QTabWidget*>(tw)->setTabsClosable(closable != 0);
}

extern "C" VQT6_API int vqt6_qtabwidget_tabs_closable(void* tw) {
    if (tw == nullptr) return 0;
    return static_cast<QTabWidget*>(tw)->tabsClosable() ? 1 : 0;
}

// ============================================================
// QMenuBar / QMenu / QAction
// ============================================================
extern "C" VQT6_API void* vqt6_qmenubar_create(void) {
    return new QMenuBar();
}

extern "C" VQT6_API void* vqt6_qmenubar_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QMenuBar(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void* vqt6_qmenubar_add_menu(void* mb, void* title) {
    if (mb == nullptr || title == nullptr) return nullptr;
    return static_cast<QMenuBar*>(mb)->addMenu(*static_cast<QString*>(title));
}

extern "C" VQT6_API void vqt6_qmenubar_add_menu_with_menu(void* mb, void* menu) {
    if (mb == nullptr || menu == nullptr) return;
    static_cast<QMenuBar*>(mb)->addMenu(static_cast<QMenu*>(menu));
}

extern "C" VQT6_API void vqt6_qmenubar_add_action(void* mb, void* action) {
    if (mb == nullptr || action == nullptr) return;
    static_cast<QMenuBar*>(mb)->addAction(static_cast<QAction*>(action));
}

extern "C" VQT6_API void vqt6_qmenubar_clear(void* mb) {
    if (mb == nullptr) return;
    static_cast<QMenuBar*>(mb)->clear();
}

extern "C" VQT6_API void* vqt6_qmenu_create(void) {
    return new QMenu();
}

extern "C" VQT6_API void* vqt6_qmenu_create_with_title(void* title) {
    if (title == nullptr) return nullptr;
    return new QMenu(*static_cast<QString*>(title));
}

extern "C" VQT6_API void* vqt6_qmenu_create_with_title_parent(void* title, void* parent) {
    if (title == nullptr || parent == nullptr) return nullptr;
    return new QMenu(*static_cast<QString*>(title), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void* vqt6_qmenu_add_menu(void* menu, void* title) {
    if (menu == nullptr || title == nullptr) return nullptr;
    return static_cast<QMenu*>(menu)->addMenu(*static_cast<QString*>(title));
}

extern "C" VQT6_API void vqt6_qmenu_add_action(void* menu, void* action) {
    if (menu == nullptr || action == nullptr) return;
    static_cast<QMenu*>(menu)->addAction(static_cast<QAction*>(action));
}

extern "C" VQT6_API void vqt6_qmenu_add_separator(void* menu) {
    if (menu == nullptr) return;
    static_cast<QMenu*>(menu)->addSeparator();
}

extern "C" VQT6_API void vqt6_qmenu_clear(void* menu) {
    if (menu == nullptr) return;
    static_cast<QMenu*>(menu)->clear();
}

extern "C" VQT6_API void vqt6_qmenu_set_title(void* menu, void* title) {
    if (menu == nullptr || title == nullptr) return;
    static_cast<QMenu*>(menu)->setTitle(*static_cast<QString*>(title));
}

extern "C" VQT6_API void* vqt6_qmenu_title(void* menu) {
    if (menu == nullptr) return nullptr;
    return new QString(static_cast<QMenu*>(menu)->title());
}

extern "C" VQT6_API void* vqt6_qmenu_add_action_with_text(void* menu, void* text) {
    if (menu == nullptr || text == nullptr) return nullptr;
    return static_cast<QMenu*>(menu)->addAction(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qaction_create(void) {
    return new QAction();
}

extern "C" VQT6_API void* vqt6_qaction_create_with_text(void* text) {
    if (text == nullptr) return nullptr;
    return new QAction(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qaction_create_with_text_parent(void* text, void* parent) {
    if (text == nullptr || parent == nullptr) return nullptr;
    return new QAction(*static_cast<QString*>(text), static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qaction_set_text(void* action, void* text) {
    if (action == nullptr || text == nullptr) return;
    static_cast<QAction*>(action)->setText(*static_cast<QString*>(text));
}

extern "C" VQT6_API void* vqt6_qaction_text(void* action) {
    if (action == nullptr) return nullptr;
    return new QString(static_cast<QAction*>(action)->text());
}

extern "C" VQT6_API void vqt6_qaction_set_icon(void* action, void* icon) {
    if (action == nullptr || icon == nullptr) return;
    static_cast<QAction*>(action)->setIcon(*static_cast<QIcon*>(icon));
}

extern "C" VQT6_API void* vqt6_qaction_icon(void* action) {
    if (action == nullptr) return nullptr;
    return new QIcon(static_cast<QAction*>(action)->icon());
}

extern "C" VQT6_API void vqt6_qaction_set_shortcut(void* action, void* key_sequence) {
    if (action == nullptr || key_sequence == nullptr) return;
    static_cast<QAction*>(action)->setShortcut(QKeySequence(*static_cast<QString*>(key_sequence)));
}

extern "C" VQT6_API void* vqt6_qaction_shortcut(void* action) {
    if (action == nullptr) return nullptr;
    return new QString(static_cast<QAction*>(action)->shortcut().toString());
}

extern "C" VQT6_API void vqt6_qaction_set_checkable(void* action, int checkable) {
    if (action == nullptr) return;
    static_cast<QAction*>(action)->setCheckable(checkable != 0);
}

extern "C" VQT6_API int vqt6_qaction_is_checkable(void* action) {
    if (action == nullptr) return 0;
    return static_cast<QAction*>(action)->isCheckable() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qaction_set_checked(void* action, int checked) {
    if (action == nullptr) return;
    static_cast<QAction*>(action)->setChecked(checked != 0);
}

extern "C" VQT6_API int vqt6_qaction_is_checked(void* action) {
    if (action == nullptr) return 0;
    return static_cast<QAction*>(action)->isChecked() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qaction_set_enabled(void* action, int enabled) {
    if (action == nullptr) return;
    static_cast<QAction*>(action)->setEnabled(enabled != 0);
}

extern "C" VQT6_API int vqt6_qaction_is_enabled(void* action) {
    if (action == nullptr) return 0;
    return static_cast<QAction*>(action)->isEnabled() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qaction_trigger(void* action) {
    if (action == nullptr) return;
    static_cast<QAction*>(action)->trigger();
}

// ============================================================
// QStatusBar / QToolBar
// ============================================================
extern "C" VQT6_API void* vqt6_qstatusbar_create(void) {
    return new QStatusBar();
}

extern "C" VQT6_API void* vqt6_qstatusbar_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QStatusBar(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void vqt6_qstatusbar_show_message(void* sb, void* message, int timeout_ms) {
    if (sb == nullptr || message == nullptr) return;
    static_cast<QStatusBar*>(sb)->showMessage(*static_cast<QString*>(message), timeout_ms);
}

extern "C" VQT6_API void vqt6_qstatusbar_clear_message(void* sb) {
    if (sb == nullptr) return;
    static_cast<QStatusBar*>(sb)->clearMessage();
}

extern "C" VQT6_API void* vqt6_qstatusbar_current_message(void* sb) {
    if (sb == nullptr) return nullptr;
    return new QString(static_cast<QStatusBar*>(sb)->currentMessage());
}

extern "C" VQT6_API void vqt6_qstatusbar_add_widget(void* sb, void* w, int stretch) {
    if (sb == nullptr || w == nullptr) return;
    static_cast<QStatusBar*>(sb)->addWidget(static_cast<QWidget*>(w), stretch);
}

extern "C" VQT6_API void vqt6_qstatusbar_add_permanent_widget(void* sb, void* w, int stretch) {
    if (sb == nullptr || w == nullptr) return;
    static_cast<QStatusBar*>(sb)->addPermanentWidget(static_cast<QWidget*>(w), stretch);
}

extern "C" VQT6_API void* vqt6_qtoolbar_create(void) {
    return new QToolBar();
}

extern "C" VQT6_API void* vqt6_qtoolbar_create_with_parent(void* parent) {
    if (parent == nullptr) return nullptr;
    return new QToolBar(static_cast<QWidget*>(parent));
}

extern "C" VQT6_API void* vqt6_qtoolbar_add_widget(void* tb, void* w) {
    if (tb == nullptr || w == nullptr) return nullptr;
    return static_cast<QToolBar*>(tb)->addWidget(static_cast<QWidget*>(w));
}

extern "C" VQT6_API void vqt6_qtoolbar_add_action(void* tb, void* action) {
    if (tb == nullptr || action == nullptr) return;
    static_cast<QToolBar*>(tb)->addAction(static_cast<QAction*>(action));
}

extern "C" VQT6_API void vqt6_qtoolbar_add_separator(void* tb) {
    if (tb == nullptr) return;
    static_cast<QToolBar*>(tb)->addSeparator();
}

extern "C" VQT6_API void vqt6_qtoolbar_set_icon_size(void* tb, int w, int h) {
    if (tb == nullptr) return;
    static_cast<QToolBar*>(tb)->setIconSize(QSize(w, h));
}

extern "C" VQT6_API void vqt6_qtoolbar_set_orientation(void* tb, int orientation) {
    if (tb == nullptr) return;
    static_cast<QToolBar*>(tb)->setOrientation(static_cast<Qt::Orientation>(orientation));
}

extern "C" VQT6_API int vqt6_qtoolbar_orientation(void* tb) {
    if (tb == nullptr) return 0;
    return static_cast<int>(static_cast<QToolBar*>(tb)->orientation());
}

// ============================================================
// QMessageBox
// ============================================================
extern "C" VQT6_API int vqt6_qmessagebox_information(void* parent, void* title, void* text, int buttons) {
    if (title == nullptr || text == nullptr) return 0;
    QMessageBox mb(static_cast<QMessageBox::Icon>(0x0400), *static_cast<QString*>(title), *static_cast<QString*>(text),
                   static_cast<QMessageBox::StandardButtons>(buttons), static_cast<QWidget*>(parent));
    return mb.exec();
}

extern "C" VQT6_API int vqt6_qmessagebox_warning(void* parent, void* title, void* text, int buttons) {
    if (title == nullptr || text == nullptr) return 0;
    QMessageBox mb(static_cast<QMessageBox::Icon>(0x0200), *static_cast<QString*>(title), *static_cast<QString*>(text),
                   static_cast<QMessageBox::StandardButtons>(buttons), static_cast<QWidget*>(parent));
    return mb.exec();
}

extern "C" VQT6_API int vqt6_qmessagebox_critical(void* parent, void* title, void* text, int buttons) {
    if (title == nullptr || text == nullptr) return 0;
    QMessageBox mb(static_cast<QMessageBox::Icon>(0x0100), *static_cast<QString*>(title), *static_cast<QString*>(text),
                   static_cast<QMessageBox::StandardButtons>(buttons), static_cast<QWidget*>(parent));
    return mb.exec();
}

extern "C" VQT6_API int vqt6_qmessagebox_question(void* parent, void* title, void* text, int buttons) {
    if (title == nullptr || text == nullptr) return 0;
    QMessageBox mb(static_cast<QMessageBox::Icon>(0x0200), *static_cast<QString*>(title), *static_cast<QString*>(text),
                   static_cast<QMessageBox::StandardButtons>(buttons), static_cast<QWidget*>(parent));
    return mb.exec();
}

extern "C" VQT6_API void vqt6_qmessagebox_about(void* parent, void* title, void* text) {
    if (title == nullptr || text == nullptr) return;
    QMessageBox::about(static_cast<QWidget*>(parent), *static_cast<QString*>(title), *static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qmessagebox_about_qt(void* parent) {
    QMessageBox::aboutQt(static_cast<QWidget*>(parent));
}

// ============================================================
// QFileDialog
// ============================================================
extern "C" VQT6_API void* vqt6_qfiledialog_get_open_file_name(void* parent, void* caption, void* dir, void* filter) {
    QString* result = new QString(QFileDialog::getOpenFileName(
        static_cast<QWidget*>(parent),
        caption != nullptr ? *static_cast<QString*>(caption) : QString(),
        dir != nullptr ? *static_cast<QString*>(dir) : QString(),
        filter != nullptr ? *static_cast<QString*>(filter) : QString()
    ));
    return result;
}

extern "C" VQT6_API void* vqt6_qfiledialog_get_save_file_name(void* parent, void* caption, void* dir, void* filter) {
    QString* result = new QString(QFileDialog::getSaveFileName(
        static_cast<QWidget*>(parent),
        caption != nullptr ? *static_cast<QString*>(caption) : QString(),
        dir != nullptr ? *static_cast<QString*>(dir) : QString(),
        filter != nullptr ? *static_cast<QString*>(filter) : QString()
    ));
    return result;
}

extern "C" VQT6_API void* vqt6_qfiledialog_get_existing_directory(void* parent, void* caption, void* dir) {
    QString* result = new QString(QFileDialog::getExistingDirectory(
        static_cast<QWidget*>(parent),
        caption != nullptr ? *static_cast<QString*>(caption) : QString(),
        dir != nullptr ? *static_cast<QString*>(dir) : QString()
    ));
    return result;
}

extern "C" VQT6_API void* vqt6_qfiledialog_get_open_file_names(void* parent, void* caption, void* dir, void* filter) {
    return new QStringList(QFileDialog::getOpenFileNames(
        static_cast<QWidget*>(parent),
        caption != nullptr ? *static_cast<QString*>(caption) : QString(),
        dir != nullptr ? *static_cast<QString*>(dir) : QString(),
        filter != nullptr ? *static_cast<QString*>(filter) : QString()
    ));
}
