Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build issues and create APK for the Android with Qt6 #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion androidnsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent)
// Note: needs to be quint64 as uintptr_t might be 32 or 64 bit depending on the system, while Java expects a jlong which is always 64 bit.
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QtAndroid::androidActivity().object());
#elif QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
nsdManager
= QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QNativeInterface::QAndroidApplication::context());
#else
nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this), QNativeInterface::QAndroidApplication::context());
nsdManager = QAndroidJniObject("qtzeroconf/QZeroConfNsdManager", "(JLandroid/content/Context;)V", reinterpret_cast<quint64>(this),
QNativeInterface::QAndroidApplication::context().object());
#endif
if (nsdManager.isValid()) {
jclass objectClass = env->GetObjectClass(nsdManager.object<jobject>());
Expand Down
46 changes: 39 additions & 7 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.4)
cmake_minimum_required(VERSION 3.14)
project(QtZeroConfExample)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
Expand All @@ -14,13 +14,45 @@ if(NOT TARGET QtZeroConf)
find_package(QtZeroConf CONFIG REQUIRED)
endif()

add_executable(QtZeroConfExample
window.h
window.cpp
main.cpp
)
# Check if it's Android and if Qt is >= 6.6.0
if(ANDROID AND QT_VERSION_MAJOR GREATER_EQUAL 6 AND QT_VERSION_MINOR GREATER_EQUAL 6)
qt_add_executable(${PROJECT_NAME}
window.h
window.cpp
main.cpp
)

if(NOT EXISTS ${ANDROID_OPENSSL_PATH})
message(WARNING "Cannot find OpenSSL for Android. Path: ${ANDROID_OPENSSL_PATH}")
message(FATAL_ERROR "Please set ANDROID_OPENSSL_PATH to the path of OpenSSL for Android.")
endif()

message(STATUS "Found OpenSSL for Android. Path: ${ANDROID_OPENSSL_PATH}")
set_property(TARGET ${PROJECT_NAME}
PROPERTY QT_ANDROID_TARGET_SDK_VERSION
34
)

set_property(TARGET ${PROJECT_NAME}
PROPERTY QT_ANDROID_EXTRA_LIBS
${ANDROID_OPENSSL_PATH}/libcrypto_3.so
${ANDROID_OPENSSL_PATH}/libssl_3.so
)

set_property(TARGET ${PROJECT_NAME}
APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/android
)

set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_BINARY_DIR}/src/android-build)
else ()
add_executable(${PROJECT_NAME}
window.h
window.cpp
main.cpp
)
endif()

target_link_libraries(QtZeroConfExample
target_link_libraries(${PROJECT_NAME} PRIVATE
QtZeroConf
Qt${QT_VERISON_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Widgets
)
21 changes: 16 additions & 5 deletions example/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,24 @@ void mainWindow::buildGUI()
connect(button, &QPushButton::clicked, this, &mainWindow::stopPublishClicked);

table.verticalHeader()->hide();
table.horizontalHeader()->hide();
// table.horizontalHeader()->hide();
table.setColumnCount(2);
layout->addWidget(&table);
//table.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

// set up the table
table.setHorizontalHeaderLabels(QStringList() << "Name"
<< "IP Address");
table.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table.horizontalHeader()->setStretchLastSection(true);
table.setEditTriggers(QAbstractItemView::NoEditTriggers);
table.setSelectionBehavior(QAbstractItemView::SelectRows);
table.setSelectionMode(QAbstractItemView::SingleSelection);
layout->addWidget(&table);

QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);

show();
}

Expand Down Expand Up @@ -160,9 +170,10 @@ void mainWindow::addService(QZeroConfService zcs)
cell = new QTableWidgetItem(zcs->ip().toString());
table.setItem(row, 1, cell);
table.resizeColumnsToContents();
#if !(defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
setFixedSize(table.horizontalHeader()->length() + 60, table.verticalHeader()->length() + 100);
#endif

#if !(defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
setFixedSize(table.horizontalHeader()->length() + 200, table.verticalHeader()->length() + 100);
#endif
}

void mainWindow::removeService(QZeroConfService zcs)
Expand Down