From e2486f914d38664f6de9af5f17ce73f4da41f0d9 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 26 Feb 2024 11:39:16 +0100 Subject: [PATCH] QSql dependency removed ( using a QStandardItemModel instead ) --- cmake/QskFindMacros.cmake | 4 +-- playground/CMakeLists.txt | 5 +-- playground/models/CMakeLists.txt | 2 +- playground/models/Window.cpp | 57 +++++++++++++++++--------------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/cmake/QskFindMacros.cmake b/cmake/QskFindMacros.cmake index e2b01e7f..f2791254 100644 --- a/cmake/QskFindMacros.cmake +++ b/cmake/QskFindMacros.cmake @@ -54,11 +54,11 @@ macro(qsk_setup_Qt) # C++, but QSkinny itself does not need the WebEngine at all. if (QT_VERSION_MAJOR VERSION_LESS 6) - find_package(Qt${QT_VERSION_MAJOR} QUIET OPTIONAL_COMPONENTS WebEngine Sql) + find_package(Qt${QT_VERSION_MAJOR} QUIET OPTIONAL_COMPONENTS WebEngine) set( Qt5WebEngineQuick_FOUND ${Qt5WebEngine_FOUND} ) else() find_package(Qt${QT_VERSION_MAJOR} QUIET - OPTIONAL_COMPONENTS WebEngineCore WebEngineQuick Sql) + OPTIONAL_COMPONENTS WebEngineCore WebEngineQuick) endif() if( NOT Qt${QT_VERSION_MAJOR}WebEngineQuick_FOUND) diff --git a/playground/CMakeLists.txt b/playground/CMakeLists.txt index 7ba9e0b5..d3f2b0c7 100644 --- a/playground/CMakeLists.txt +++ b/playground/CMakeLists.txt @@ -8,6 +8,7 @@ add_subdirectory(shadows) add_subdirectory(shapes) add_subdirectory(charts) add_subdirectory(plots) +add_subdirectory(models) if (BUILD_INPUTCONTEXT) add_subdirectory(inputpanel) @@ -32,7 +33,3 @@ endif() if(TARGET Qt::QuickWidgets) add_subdirectory(grids) endif() - -if(TARGET Qt::Sql) - add_subdirectory(models) -endif() diff --git a/playground/models/CMakeLists.txt b/playground/models/CMakeLists.txt index c8b277e0..da9a430e 100644 --- a/playground/models/CMakeLists.txt +++ b/playground/models/CMakeLists.txt @@ -4,4 +4,4 @@ ############################################################################ qsk_add_example(models Window.h Window.cpp main.cpp) -target_link_libraries(models PRIVATE Qt::Sql) +target_link_libraries(models) diff --git a/playground/models/Window.cpp b/playground/models/Window.cpp index 352dc538..69e6f623 100644 --- a/playground/models/Window.cpp +++ b/playground/models/Window.cpp @@ -9,39 +9,41 @@ #include #include #include - #include -#include -#include -#include -#include +#include namespace { - class Model : public QSqlTableModel + class Model : public QStandardItemModel { public: - Model( QObject *parent = nullptr ) - : QSqlTableModel( parent, QSqlDatabase::addDatabase( "QSQLITE" ) ) + Model( QObject* parent = nullptr ) + : QStandardItemModel( 2, 2, parent ) { - auto db = database(); + setValue( 0, 0, 1 ); + setValue( 0, 1, "HELLO" ); - db.setDatabaseName( ":memory:" ); - db.open(); - - QSqlQuery query( db ); - query.exec( "create table test(id integer,value text);" ); - query.exec( "insert into test (id,value) values (1,'HELLO');" ); - query.exec( "insert into test (id,value) values (2,'WORLD');" ); - - setTable( "test" ); - select(); + setValue( 1, 0, 2 ); + setValue( 1, 1, "WORLD" ); } - void setValue( int row, const QVariant &value ) + void setValue( int row, int col, const QVariant& value ) { - setData( index( row, 0 ), value ); + setData( index( row, col ), value, Qt::EditRole ); + } + + void dump() + { + qDebug() << "Model"; + for ( int row = 0; row < rowCount(); row++ ) + { + for ( int col = 0; col < columnCount(); col++ ) + { + qDebug() << '\t' << row << col + << data( index( row, col ), Qt::EditRole ); + } + } } }; } @@ -72,10 +74,10 @@ Window::Window() auto next = new QskPushButton( ">", hBox); connect(prev, &QskPushButton::clicked, - [ mapper ]() { mapper->setCurrentRow( 0 ); }); + [ mapper ]() { mapper->setCurrentRow( 0 ); } ); connect( next, &QskPushButton::clicked, - [ mapper ]() { mapper->setCurrentRow( 1 ); }); + [ mapper ]() { mapper->setCurrentRow( 1 ); } ); vBox->addItem( hBox ); } @@ -83,11 +85,12 @@ Window::Window() // update the current record with the data from the SpinBox and TextInput auto save = new QskPushButton( "Save Data to Model", vBox ); connect( save, &QskPushButton::clicked, - [=]() { mapper->submit(); qDebug() << model->record( mapper->currentRow() ); }); + [ = ]() { mapper->submit(); model->dump(); } ); // trigger the binder and update the spinbox - auto set0 = new QskPushButton( "Set Model field to 0", vBox ); + auto set0 = new QskPushButton( "Set Model field to 42", vBox ); connect( set0, &QskPushButton::clicked, - [=]() { model->setValue( mapper->currentRow(), 0 ); } ); - vBox->addSpacer( 0,100 ); + [ = ]() { model->setValue( mapper->currentRow(), 0, 42 ); } ); + + vBox->addSpacer( 0, 100 ); }