LUA: Add icon path to the widget from GUI bindings

Change-Id: I4f9ac52639e4edb1b65a0637e009a6bbba6998dd
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Lukasz Papierkowski
2024-08-22 15:11:32 +02:00
committed by lie
parent 869d334eba
commit f3b67568cd
4 changed files with 30 additions and 2 deletions

View File

@@ -3,6 +3,9 @@
#include "layoutbuilder.h"
#include <utils/icon.h>
#include <utils/filepath.h>
#include <QDebug>
#include <QFormLayout>
#include <QGridLayout>
@@ -873,6 +876,17 @@ void PushButton::setText(const QString &text)
access(this)->setText(text);
}
void PushButton::setIconPath(const Utils::FilePath &iconPath)
{
if (!iconPath.exists()) {
access(this)->setIcon({});
return;
}
Utils::Icon icon{iconPath};
access(this)->setIcon(icon.icon());
}
void PushButton::onClicked(const std::function<void ()> &func, QObject *guard)
{
QObject::connect(access(this), &QAbstractButton::clicked, guard, func);

View File

@@ -38,6 +38,11 @@ class QVBoxLayout;
class QWidget;
QT_END_NAMESPACE
namespace Utils
{
class FilePath;
} // Utils
namespace Layouting {
//////////////////////////////////////////////
@@ -285,6 +290,7 @@ public:
PushButton(std::initializer_list<I> ps);
void setText(const QString &);
void setIconPath(const Utils::FilePath &);
void onClicked(const std::function<void()> &, QObject *guard);
};

View File

@@ -6,6 +6,7 @@
#include "inheritance.h"
#include <utils/aspects.h>
#include <utils/filepath.h>
#include <utils/layoutbuilder.h>
#include <QMetaEnum>
@@ -99,10 +100,16 @@ HAS_MEM_FUNC(setSize, hasSetSize);
HAS_MEM_FUNC(setWindowFlags, hasSetWindowFlags);
HAS_MEM_FUNC(setWidgetAttribute, hasSetWidgetAttribute);
HAS_MEM_FUNC(setAutoFillBackground, hasSetAutoFillBackground);
HAS_MEM_FUNC(setIconPath, hasSetIconPath);
template<class T>
void setProperties(std::unique_ptr<T> &item, const sol::table &children, QObject *guard)
{
void setProperties(std::unique_ptr<T> &item, const sol::table &children, QObject *guard) {
if constexpr (hasSetIconPath<T, void (T::*)(const Utils::FilePath &)>::value) {
const auto iconPath = children.get<sol::optional<Utils::FilePath>>("iconPath");
if (iconPath)
item->setIconPath(*iconPath);
}
if constexpr (hasSetWindowFlags<T, void (T::*)(Qt::WindowFlags)>::value) {
sol::optional<sol::table> windowFlags = children.get<sol::optional<sol::table>>(
"windowFlags");

View File

@@ -30,6 +30,7 @@ gui.baseWidgetOptions = {}
---@field title? string The title of the widget, if applicable.
---@field onTextChanged? function The function to be called when the text of the widget changes, if applicable.
---@field onClicked? function The function to be called when the widget is clicked, if applicable.
---@field iconPath? FilePath The path to the icon of the widget, if applicable. Empty or not existent paths set null icon. (see [QIcon](https://doc.qt.io/qt-5/qicon.html#QIcon))
---@field text? string The text of the widget, if applicable.
---@field value? integer The value of the widget, if applicable.
---@field [1]? Layout The layout of the widget, if applicable.