QSql dependency removed ( using a QStandardItemModel instead )

This commit is contained in:
Uwe Rathmann 2024-02-26 11:39:16 +01:00
parent 60c7442fd0
commit e2486f914d
4 changed files with 34 additions and 34 deletions

View File

@ -54,11 +54,11 @@ macro(qsk_setup_Qt)
# C++, but QSkinny itself does not need the WebEngine at all. # C++, but QSkinny itself does not need the WebEngine at all.
if (QT_VERSION_MAJOR VERSION_LESS 6) 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} ) set( Qt5WebEngineQuick_FOUND ${Qt5WebEngine_FOUND} )
else() else()
find_package(Qt${QT_VERSION_MAJOR} QUIET find_package(Qt${QT_VERSION_MAJOR} QUIET
OPTIONAL_COMPONENTS WebEngineCore WebEngineQuick Sql) OPTIONAL_COMPONENTS WebEngineCore WebEngineQuick)
endif() endif()
if( NOT Qt${QT_VERSION_MAJOR}WebEngineQuick_FOUND) if( NOT Qt${QT_VERSION_MAJOR}WebEngineQuick_FOUND)

View File

@ -8,6 +8,7 @@ add_subdirectory(shadows)
add_subdirectory(shapes) add_subdirectory(shapes)
add_subdirectory(charts) add_subdirectory(charts)
add_subdirectory(plots) add_subdirectory(plots)
add_subdirectory(models)
if (BUILD_INPUTCONTEXT) if (BUILD_INPUTCONTEXT)
add_subdirectory(inputpanel) add_subdirectory(inputpanel)
@ -32,7 +33,3 @@ endif()
if(TARGET Qt::QuickWidgets) if(TARGET Qt::QuickWidgets)
add_subdirectory(grids) add_subdirectory(grids)
endif() endif()
if(TARGET Qt::Sql)
add_subdirectory(models)
endif()

View File

@ -4,4 +4,4 @@
############################################################################ ############################################################################
qsk_add_example(models Window.h Window.cpp main.cpp) qsk_add_example(models Window.h Window.cpp main.cpp)
target_link_libraries(models PRIVATE Qt::Sql) target_link_libraries(models)

View File

@ -9,39 +9,41 @@
#include <QskPushButton.h> #include <QskPushButton.h>
#include <QskTextInput.h> #include <QskTextInput.h>
#include <QskSpinBox.h> #include <QskSpinBox.h>
#include <QskModelObjectBinder.h> #include <QskModelObjectBinder.h>
#include <QSqlDatabase> #include <QStandardItemModel>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlRecord>
namespace namespace
{ {
class Model : public QSqlTableModel class Model : public QStandardItemModel
{ {
public: public:
Model( QObject *parent = nullptr ) Model( QObject* parent = nullptr )
: QSqlTableModel( parent, QSqlDatabase::addDatabase( "QSQLITE" ) ) : QStandardItemModel( 2, 2, parent )
{ {
auto db = database(); setValue( 0, 0, 1 );
setValue( 0, 1, "HELLO" );
db.setDatabaseName( ":memory:" ); setValue( 1, 0, 2 );
db.open(); setValue( 1, 1, "WORLD" );
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();
} }
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); auto next = new QskPushButton( ">", hBox);
connect(prev, &QskPushButton::clicked, connect(prev, &QskPushButton::clicked,
[ mapper ]() { mapper->setCurrentRow( 0 ); }); [ mapper ]() { mapper->setCurrentRow( 0 ); } );
connect( next, &QskPushButton::clicked, connect( next, &QskPushButton::clicked,
[ mapper ]() { mapper->setCurrentRow( 1 ); }); [ mapper ]() { mapper->setCurrentRow( 1 ); } );
vBox->addItem( hBox ); vBox->addItem( hBox );
} }
@ -83,11 +85,12 @@ Window::Window()
// update the current record with the data from the SpinBox and TextInput // update the current record with the data from the SpinBox and TextInput
auto save = new QskPushButton( "Save Data to Model", vBox ); auto save = new QskPushButton( "Save Data to Model", vBox );
connect( save, &QskPushButton::clicked, connect( save, &QskPushButton::clicked,
[=]() { mapper->submit(); qDebug() << model->record( mapper->currentRow() ); }); [ = ]() { mapper->submit(); model->dump(); } );
// trigger the binder and update the spinbox // 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, connect( set0, &QskPushButton::clicked,
[=]() { model->setValue( mapper->currentRow(), 0 ); } ); [ = ]() { model->setValue( mapper->currentRow(), 0, 42 ); } );
vBox->addSpacer( 0,100 );
vBox->addSpacer( 0, 100 );
} }