systemdialogs example added

This commit is contained in:
Uwe Rathmann 2025-01-07 16:02:59 +01:00
parent b97889d2d8
commit a44162fabb
4 changed files with 220 additions and 0 deletions

View File

@ -68,6 +68,9 @@ macro(qsk_setup_Qt)
find_package(Qt${QT_VERSION_MAJOR} QUIET find_package(Qt${QT_VERSION_MAJOR} QUIET
OPTIONAL_COMPONENTS QuickShapesPrivate) OPTIONAL_COMPONENTS QuickShapesPrivate)
find_package(Qt${QT_VERSION_MAJOR} QUIET
OPTIONAL_COMPONENTS QuickDialogs2Utils QuickDialogs2 )
endif() endif()
if( NOT Qt${QT_VERSION_MAJOR}WebEngineQuick_FOUND) if( NOT Qt${QT_VERSION_MAJOR}WebEngineQuick_FOUND)
@ -77,6 +80,10 @@ macro(qsk_setup_Qt)
if (NOT Qt${QT_VERSION_MAJOR}QuickShapesPrivate_FOUND) if (NOT Qt${QT_VERSION_MAJOR}QuickShapesPrivate_FOUND)
message(STATUS "No Qt/Quick Shapes support: skipping some unimportant examples") message(STATUS "No Qt/Quick Shapes support: skipping some unimportant examples")
endif() endif()
if( NOT Qt${QT_VERSION_MAJOR}QuickDialogs2_FOUND)
message(STATUS "No Qt/Quick Dialogs2 support: skipping some unimportant examples")
endif()
endif() endif()
endmacro() endmacro()

View File

@ -32,3 +32,7 @@ endif()
if(TARGET Qt::QuickWidgets) if(TARGET Qt::QuickWidgets)
add_subdirectory(grids) add_subdirectory(grids)
endif() endif()
if(TARGET Qt::QuickDialogs2)
add_subdirectory(systemdialogs)
endif()

View File

@ -0,0 +1,14 @@
############################################################################
# QSkinny - Copyright (C) The authors
# SPDX-License-Identifier: BSD-3-Clause
############################################################################
set(SOURCES main.cpp)
set(target systemdialogs)
qsk_add_example(${target} ${SOURCES})
target_link_libraries(${target} PRIVATE
Qt::QuickDialogs2UtilsPrivate
Qt::QuickDialogs2Private
)

View File

@ -0,0 +1,195 @@
/******************************************************************************
* QSkinny - Copyright (C) The authors
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#include <SkinnyShortcut.h>
#include <QskObjectCounter.h>
#include <QskWindow.h>
#include <QskPushButton.h>
#include <QskCheckBox.h>
#include <QskLinearBox.h>
#include <QskMainView.h>
#include <QGuiApplication>
#include <private/qquickcolordialog_p.h>
#include <private/qquickfiledialog_p.h>
#include <private/qquickfolderdialog_p.h>
#include <private/qquickfontdialog_p.h>
#include <private/qquickmessagedialog_p.h>
#include <QtQml>
static QQuickAbstractDialog* createQml( const char* className )
{
static QQmlEngine engine( nullptr );
QByteArray qmlCode = "import QtQuick.Dialogs\n";
qmlCode += className;
qmlCode += " {}";
auto component = new QQmlComponent( &engine );
component->setData( qmlCode.constData(), QUrl() );
if ( component->status() != QQmlComponent::Ready )
{
qWarning() << component->errorString();
delete component;
return nullptr;
}
auto dialog = qobject_cast< QQuickAbstractDialog* >( component->create() );
QObject::connect( dialog, &QObject::destroyed,
component, &QObject::deleteLater );
return dialog;
}
namespace
{
class ButtonBox : public QskLinearBox
{
Q_OBJECT
public:
enum DialogType
{
ColorDialog,
FileDialog,
FolderDialog,
FontDialog,
MessageDialog,
};
Q_ENUM( DialogType )
ButtonBox( QQuickItem* parent = nullptr )
: QskLinearBox( Qt::Vertical, parent )
{
setDefaultAlignment( Qt::AlignCenter );
setMargins( 10 );
setSpacing( 20 );
const auto metaEnum = QMetaEnum::fromType<DialogType>();
for ( int i = ColorDialog; i <= MessageDialog; i++ )
{
auto button = new QskPushButton( metaEnum.key( i ), this );
button->setPreferredWidth( 200 );
button->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
connect( button, &QskPushButton::clicked,
this, [ this, i ]() { openDialog( i ); } );
}
setExtraSpacingAt( Qt::BottomEdge );
}
private:
void openDialog( int dialogType )
{
/*
Qt/Quick Dialogs implements a thin wrapper to make
QPlatformTheme::createPlatformDialogHelper accessible from QML.
There is not much value for the QSkinny use case and we could
use QPlatformTheme in our QskDialog classes without the wrapper.
However Qt/Quick Dialogs also offers a fallback implementation
that is used when a dialog type is not supported by the platform.
These classes are implemented in QML and we need QmlEngine/QmlComponent
to use them. Once we have our own fallback implementation we can
drop this QML dependency.
*/
delete m_dialog;
m_dialog = nullptr;
if ( qGuiApp->testAttribute( Qt::AA_DontUseNativeDialogs ) )
{
const auto metaEnum = QMetaEnum::fromType<DialogType>();
m_dialog = createQml( metaEnum.key( dialogType ) );
if ( m_dialog )
m_dialog->setParentWindow( window() );
}
else
{
switch( dialogType )
{
case ColorDialog:
m_dialog = new QQuickColorDialog();
break;
case FileDialog:
m_dialog = new QQuickFileDialog();
break;
case FolderDialog:
m_dialog = new QQuickFolderDialog();
break;
case FontDialog:
m_dialog = new QQuickFontDialog();
break;
case MessageDialog:
m_dialog = new QQuickMessageDialog();
break;
}
}
if ( m_dialog )
{
if ( auto messageDialog = qobject_cast< QQuickMessageDialog* >( m_dialog ) )
messageDialog->setText( "The quick brown fox jumps over the lazy dog" );
//m_dialog->setModality( Qt::WindowModal );
m_dialog->open();
}
}
QQuickAbstractDialog* m_dialog = nullptr;
};
class MainView : public QskMainView
{
public:
MainView( QQuickItem* parent = nullptr )
: QskMainView( parent )
{
auto header = new QskLinearBox();
header->setExtraSpacingAt( Qt::LeftEdge );
header->setSizePolicy( Qt::Vertical, QskSizePolicy::Fixed );
auto button = new QskCheckBox( "Try Native Dialogs", header );
button->setChecked( true );
connect( button, &QskCheckBox::toggled,
[]( bool on ) { qGuiApp->setAttribute( Qt::AA_DontUseNativeDialogs, !on ); } );
setHeader( header );
setBody( new ButtonBox() );
}
};
}
int main( int argc, char* argv[] )
{
#ifdef ITEM_STATISTICS
QskObjectCounter counter( true );
#endif
QGuiApplication app( argc, argv );
SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts );
QskWindow window;
window.addItem( new MainView() );
window.resize( 800, 600 );
window.show();
return app.exec();
}
#include "main.moc"