This commit is contained in:
Uwe Rathmann 2024-02-26 12:16:56 +01:00
parent e2486f914d
commit 7b4db3afc1
1 changed files with 85 additions and 53 deletions

View File

@ -21,19 +21,14 @@ namespace
Model( QObject* parent = nullptr )
: QStandardItemModel( 2, 2, parent )
{
setValue( 0, 0, 1 );
setValue( 0, 1, "HELLO" );
initValue( 0, 0, 1 );
initValue( 0, 1, "HELLO" );
setValue( 1, 0, 2 );
setValue( 1, 1, "WORLD" );
initValue( 1, 0, 2 );
initValue( 1, 1, "WORLD" );
}
void setValue( int row, int col, const QVariant& value )
{
setData( index( row, col ), value, Qt::EditRole );
}
void dump()
void dump() const
{
qDebug() << "Model";
for ( int row = 0; row < rowCount(); row++ )
@ -45,52 +40,89 @@ namespace
}
}
}
private:
void initValue( int row, int col, const QVariant& value )
{
setData( index( row, col ), value, Qt::EditRole );
}
};
class View : public QskLinearBox
{
public:
View( QQuickItem* parent = nullptr )
: QskLinearBox( Qt::Vertical, parent )
{
setPanel( true );
auto model = new Model( this );
auto textInput = new QskTextInput();
auto spinBox = new QskSpinBox( -100.0, +100.0, 1.0 );
m_binder = new QskModelObjectBinder( model, this );
m_binder->bindObject( spinBox, 0 );
m_binder->bindObject( textInput, 1 );
auto hBox = new QskLinearBox( Qt::Horizontal );
hBox->setSection( QskAspect::Header );
hBox->setSizePolicy( Qt::Vertical, QskSizePolicy::Fixed );
{
auto rowButton = new QskPushButton( "Toggle Row", hBox );
auto counterButton = new QskPushButton( "Invert Counter", hBox );
auto submitButton = new QskPushButton( "Submit Changes", hBox );
connect( rowButton, &QskPushButton::clicked,
this, &View::toogleRow );
connect( counterButton, &QskPushButton::clicked,
this, &View::invertCounter );
connect( submitButton, &QskPushButton::clicked,
this, &View::updateModel );
hBox->addStretch( 1 );
}
addItem( hBox );
addSpacer( 5 );
addItem( spinBox );
addItem( textInput );
addStretch( 1 );
}
private:
void toogleRow()
{
m_binder->setCurrentRow( m_binder->currentRow() == 0 ? 1 : 0 );
}
void updateModel()
{
m_binder->submit();
const auto model = dynamic_cast< const Model* >( m_binder->model() );
model->dump();
}
void invertCounter()
{
auto model = m_binder->model();
const auto index = model->index( m_binder->currentRow(), 0 );
auto value = model->data( index, Qt::EditRole );
model->setData( index, -value.toDouble(), Qt::EditRole );
}
QskModelObjectBinder* m_binder;
};
}
Window::Window()
{
auto model = new Model( this );
auto textInput = new QskTextInput();
auto spinBox = new QskSpinBox();
auto mapper = new QskModelObjectBinder( model, this );
mapper->bindObject( spinBox, 0 );
mapper->bindObject( textInput, 1 );
auto vBox = new QskLinearBox( Qt::Vertical );
vBox->addSpacer( 0, 100 );
vBox->addItem( spinBox );
vBox->addItem( textInput );
addItem( vBox );
auto hBox = new QskLinearBox( Qt::Horizontal );
{
auto prev = new QskPushButton( "<", hBox );
auto next = new QskPushButton( ">", hBox);
connect(prev, &QskPushButton::clicked,
[ mapper ]() { mapper->setCurrentRow( 0 ); } );
connect( next, &QskPushButton::clicked,
[ mapper ]() { mapper->setCurrentRow( 1 ); } );
vBox->addItem( hBox );
}
// 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(); model->dump(); } );
// trigger the binder and update the spinbox
auto set0 = new QskPushButton( "Set Model field to 42", vBox );
connect( set0, &QskPushButton::clicked,
[ = ]() { model->setValue( mapper->currentRow(), 0, 42 ); } );
vBox->addSpacer( 0, 100 );
addItem( new View() );
}