From ea875a5009c391cfcfb86b9a47fbf2568a86ffd4 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 12 Apr 2020 09:33:45 +0800 Subject: improve qt.moc --- tests/projects/qt/widgetapp_private_slot2/main.cpp | 11 +++++++ .../qt/widgetapp_private_slot2/mainwindow.cpp | 35 ++++++++++++++++++++++ .../qt/widgetapp_private_slot2/mainwindow.h | 32 ++++++++++++++++++++ .../qt/widgetapp_private_slot2/mainwindow.ui | 24 +++++++++++++++ .../projects/qt/widgetapp_private_slot2/xmake.lua | 6 ++++ xmake/rules/qt/moc/xmake.lua | 23 +++++++++----- 6 files changed, 124 insertions(+), 7 deletions(-) create mode 100755 tests/projects/qt/widgetapp_private_slot2/main.cpp create mode 100755 tests/projects/qt/widgetapp_private_slot2/mainwindow.cpp create mode 100755 tests/projects/qt/widgetapp_private_slot2/mainwindow.h create mode 100755 tests/projects/qt/widgetapp_private_slot2/mainwindow.ui create mode 100644 tests/projects/qt/widgetapp_private_slot2/xmake.lua diff --git a/tests/projects/qt/widgetapp_private_slot2/main.cpp b/tests/projects/qt/widgetapp_private_slot2/main.cpp new file mode 100755 index 000000000..aab39bb89 --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot2/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/tests/projects/qt/widgetapp_private_slot2/mainwindow.cpp b/tests/projects/qt/widgetapp_private_slot2/mainwindow.cpp new file mode 100755 index 000000000..aa6aee75c --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot2/mainwindow.cpp @@ -0,0 +1,35 @@ +#include "mainwindow.h" +#include + +class MainWindowPrivate:public QObject +{ + Q_OBJECT +public: + MainWindow *q_ptr; + Q_DECLARE_PUBLIC(MainWindow) +public: + MainWindowPrivate(){} + void mainWindow_slot(){qDebug()<<"mainWindow_slot";} +private: + Q_PRIVATE_SLOT(q_ptr, void test()) +}; + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent) +{ + d_ptr = new MainWindowPrivate; + d_ptr->q_ptr = this; +} + +MainWindow::~MainWindow() +{ + +} + +void MainWindow::test() +{ + +} + +#include "moc_mainwindow.cpp" +#include "mainwindow.moc" diff --git a/tests/projects/qt/widgetapp_private_slot2/mainwindow.h b/tests/projects/qt/widgetapp_private_slot2/mainwindow.h new file mode 100755 index 000000000..5350a979e --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot2/mainwindow.h @@ -0,0 +1,32 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class MainWindowPrivate; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +public Q_SLOTS: + void test(); +private: + MainWindowPrivate *d_ptr; + Q_DECLARE_PRIVATE(MainWindow) + Q_DISABLE_COPY(MainWindow) + + Q_PRIVATE_SLOT(d_func(), void mainWindow_slot()) +}; +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif +#endif // MAINWINDOW_H diff --git a/tests/projects/qt/widgetapp_private_slot2/mainwindow.ui b/tests/projects/qt/widgetapp_private_slot2/mainwindow.ui new file mode 100755 index 000000000..7de574d7c --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot2/mainwindow.ui @@ -0,0 +1,24 @@ + + MainWindow + + + + 0 + 0 + 400 + 300 + + + + MainWindow + + + + + + + + + + + diff --git a/tests/projects/qt/widgetapp_private_slot2/xmake.lua b/tests/projects/qt/widgetapp_private_slot2/xmake.lua new file mode 100644 index 000000000..fdc582c30 --- /dev/null +++ b/tests/projects/qt/widgetapp_private_slot2/xmake.lua @@ -0,0 +1,6 @@ +target("test") + add_rules("qt.widgetapp") + add_files("main.cpp") + add_files("mainwindow.cpp", {rules = "qt.moc"}) + add_files("*.h") + add_files("*.ui") diff --git a/xmake/rules/qt/moc/xmake.lua b/xmake/rules/qt/moc/xmake.lua index 8685643c0..d0ed37720 100644 --- a/xmake/rules/qt/moc/xmake.lua +++ b/xmake/rules/qt/moc/xmake.lua @@ -39,7 +39,7 @@ rule("qt.moc") end) -- before build file (we need compile it first if exists Q_PRIVATE_SLOT) - before_build_file(function (target, headerfile_moc, opt) + before_build_file(function (target, sourcefile, opt) -- imports import("moc") @@ -50,7 +50,16 @@ rule("qt.moc") import("core.project.depend") -- get c++ source file for moc - local sourcefile_moc = path.join(target:autogendir(), "rules", "qt", "moc", "moc_" .. path.basename(headerfile_moc) .. ".cpp") + -- + -- add_files("mainwindow.h") -> moc_MainWindow.cpp + -- add_files("mainwindow.cpp", {rules = "qt.moc"}) -> mainwindow.moc, @see https://github.com/xmake-io/xmake/issues/750 + -- + local basename = path.basename(sourcefile) + local filename_moc = "moc_" .. basename .. ".cpp" + if sourcefile:endswith(".cpp") then + filename_moc = basename .. ".moc" + end + local sourcefile_moc = path.join(target:autogendir(), "rules", "qt", "moc", filename_moc) -- get object file local objectfile = target:objectfile(sourcefile_moc) @@ -77,17 +86,17 @@ rule("qt.moc") -- trace progress info cprintf("${color.build.progress}" .. theme.get("text.build.progress_format") .. ":${clear} ", opt.progress) if option.get("verbose") then - cprint("${dim color.build.object}compiling.qt.moc %s", headerfile_moc) + cprint("${dim color.build.object}compiling.qt.moc %s", sourcefile) else - cprint("${color.build.object}compiling.qt.moc %s", headerfile_moc) + cprint("${color.build.object}compiling.qt.moc %s", sourcefile) end -- generate c++ source file for moc - moc.generate(target, headerfile_moc, sourcefile_moc) + moc.generate(target, sourcefile, sourcefile_moc) -- we need compile this moc_xxx.cpp file if exists Q_PRIVATE_SLOT, @see https://github.com/xmake-io/xmake/issues/750 dependinfo.files = {} - local mocdata = io.readfile(headerfile_moc) + local mocdata = io.readfile(sourcefile) if mocdata and mocdata:find("Q_PRIVATE_SLOT") then -- add includedirs of sourcefile_moc target:add("includedirs", path.directory(sourcefile_moc)) @@ -112,6 +121,6 @@ rule("qt.moc") -- update files and values to the dependent file dependinfo.values = depvalues - table.insert(dependinfo.files, headerfile_moc) + table.insert(dependinfo.files, sourcefile) depend.save(dependinfo, dependfile) end) -- cgit v1.3.1