IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Qt Discussion :

QDataWidgetMapper, QStyledItemDelegate et createEditor()


Sujet :

Qt

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 14
    Points : 9
    Points
    9
    Par défaut QDataWidgetMapper, QStyledItemDelegate et createEditor()
    Bonjour à tous,

    Après quelques recherches infructueuses sur internet et pas plus de succès sur #qt et #qt-fr sur freenode, je me permets de vous exposer mon problème en espérant que vous pourrez m'aider

    Je développe une petite application qui utilise un QSqlTableModel modifié (ajout de deux colonnes supplémentaires, rien d'incroyable). J'ai également codé un QStyledItemDelegate perso qui me permet d'afficher les deux colonnes supplémentaires dans une QTableView.

    Pour éditer chacune des row du modèle, j'aimerais plutôt utiliser un widget perso qui utilise un QDataWidgetMapper pour mapper les différentes sections du modèle vers des widgets. Dans un premier temps, j'avais directement mappé les section vers des widgets permettant l'édition (des QLineEdit, un widget perso pour les deux colonnes ajoutées au modèle,...).

    J'avais également besoin d'une possibilité d'afficher simplement les données sans permettre l'édition et je m'étais dit qu'il me suffirait d'utiliser setEnabled pour désactiver les widget. Mais après y avoir repensé cette solution de me satisfait pas et je préfèrerais obtenir avec QDataWidgetMapper un comportement identique à celui qu'on peut avoir dans une QTableView (c'est-à-dire remplacer le QLabel utilisé pour afficher les données par un QWidget permettant de les éditer.

    J'ai donc pensé qu'il me suffirait de remplacer les widget d'édition par des QLabel (ça, ça fonctionne) pour l'affichage et à changer le delegate pour permettre l'édition.

    1) Malheureusement, je ne comprends pas comment je dois faire pour appeler QStyledItemDelegate::createEditor() suite à un évenement (je voudrais créer les editor pour toutes les sections en une fois, suite au clic sur un bouton "Édition"). Comment faire ?

    2) Est-ce seulement possible ou vais-je devoir faire dériver le widget, utilisant QDataWidgetMapper, de QAbstractItemView (et donc ne plus utiliser le QDataWidgetMapper) ?

    Merci d'avance pour vos réponses !


    @micalement,

    ixM`

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 14
    Points : 9
    Points
    9
    Par défaut
    Alors, j'ai tenté de ré-implémenter QAbstractItemView et je me suis pour l'instant contenté de ré-implémenter les méthodes virtuelles pures.

    J'utilise setIndexWidget() pour tenter de faire apparaître le contenu de certaines colonnes dans les widgets appropriés. Je dis tenter parce que ça ne fonctionne pas. Rien n'apparaît dans les champs.

    Est-ce que setIndexWidget() est approprié ?

    Qu'est-ce que je devrais ré-implémenter d'autres comme méthodes ?

    Merci d'avance

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    #ifndef SINGLEBOOKVIEW2_H
    #define SINGLEBOOKVIEW2_H
     
    #include <QAbstractItemView>
     
    #include "booksmodel.h"
    #include "interactivelistwidget.h"
     
    #include <QGridLayout>
    #include <QFormLayout>
    #include <QDataWidgetMapper>
    #include <QSpinBox>
    #include <QPlainTextEdit>
    #include <QDoubleSpinBox>
    #include <QGroupBox>
     
    class SingleBookView2 : public QAbstractItemView
    {
        Q_OBJECT
    public:
        explicit SingleBookView2(QAbstractItemModel* model, QModelIndex index, QWidget *parent = 0);
     
        virtual QRect visualRect(const QModelIndex &index) const;
     
        virtual void scrollTo(const QModelIndex &index, ScrollHint hint);
     
        virtual QModelIndex indexAt(const QPoint &point) const;
     
    protected:
        virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
     
        virtual int horizontalOffset() const;
     
        virtual int verticalOffset() const;
     
        virtual bool isIndexHidden(const QModelIndex &index) const;
     
        virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command);
     
        virtual QRegion visualRegionForSelection(const QItemSelection &selection) const;
     
    signals:
     
    public slots:
        void startEditingNewBook();
     
        void changeBook(QModelIndex& index);
     
        void submit();
     
        void revert();
     
        void modifyField(unsigned int section, bool isModified = true);
     
        void toNext();
     
        void toPrevious();
     
        void setEditionEnabled(bool enable);
     
    private:
        void createLayout();
     
        QAbstractItemModel* model;
     
        QModelIndex index;
     
        bool isEditing;
     
        QMap<unsigned int, QLabel*> labels;
        QMap<unsigned int, QString> labelTexts;
        QMap<unsigned int, QWidget*> editionWidgets;
        QMap<unsigned int, QLabel*> viewWidgets;
     
        QVBoxLayout* mainLayout;
     
        QGroupBox* informations_groupBox;
     
        QHBoxLayout* informations_hBoxLayout;
     
        QFormLayout* informations_left_formLayout;
     
        InteractiveListWidget* authors_widget;
     
        QLabel* authors_view_label;
     
        QFormLayout* informations_right_formLayout;
     
        QLineEdit* books_title_lineEdit;
     
        QLabel* books_title_view_label;
     
        QLineEdit* books_editor_lineEdit;
     
        QLabel* books_editor_view_label;
     
        QSpinBox* books_year_spinBox;
     
        QLabel* books_year_view_label;
     
        QPlainTextEdit* books_description_plainTextEdit;
     
        QLabel* books_description_view_label;
     
        QGroupBox* characteristics_groupBox;
     
        QHBoxLayout* characteristics_hBoxLayout;
     
        QFormLayout* characteristics_left_formLayout;
     
        QFormLayout* characteristics_right_formLayout;
     
        QSpinBox* books_nbr_pages_spinBox;
     
        QLabel* books_nbr_pages_view_label;
     
        QDoubleSpinBox* books_weight_doubleSpinBox;
     
        QLabel* books_weight_view_label;
     
        QDoubleSpinBox* books_width_doubleSpinBox;
     
        QLabel* books_width_view_label;
     
        QDoubleSpinBox* books_height_doubleSpinBox;
     
        QLabel* books_height_view_label;
     
        QDoubleSpinBox* books_price_doubleSpinBox;
     
        QLabel* books_price_view_label;
     
        QPlainTextEdit* books_comment_plainTextEdit;
     
        QLabel* books_comment_view_label;
     
        InteractiveListWidget* keywords_widget;
     
        QLabel* keywords_view_label;
     
        QMap<unsigned int, bool> fieldsModified;
    };
     
    #endif // SINGLEBOOKVIEW2_H
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    #include "singlebookview2.h"
    #include "booksmodeldelegate.h"
     
    #include <QDebug>
     
    #include <QMessageBox>
     
    #include <QSortFilterProxyModel>
     
    SingleBookView2::SingleBookView2(QAbstractItemModel* model, QModelIndex index, QWidget *parent) :
        QAbstractItemView(parent), model(model), index(index)
    {
        isEditing = false;
     
        setModel(model);
     
        createLayout();
     
        setCurrentIndex(index);
     
        //BooksModelDelegate* bmd = new BooksModelDelegate();
     
        //setItemDelegate(bmd);
    }
     
    void SingleBookView2::createLayout() {
        const BooksModel* booksModel = NULL;
        if(qobject_cast<const BooksModel*>(model) != NULL)
            booksModel = qobject_cast<const BooksModel*>(model);
        else if(qobject_cast<const QSortFilterProxyModel*>(model) != NULL) {
            const QSortFilterProxyModel* pmodel = qobject_cast<const QSortFilterProxyModel*>(model);
     
            if(qobject_cast<BooksModel*>(pmodel->sourceModel()) != NULL)
                booksModel = qobject_cast<BooksModel*>(pmodel->sourceModel());
        }
     
        if(booksModel == NULL) {
            qDebug() << "ERROR: invalid model";
     
            return;
        }
     
        int row = index.row();
     
        /* BEGIN COMPLETERS: AUTHORS */
        QMap<QString, QCompleter*> authors_fields_completer;
     
        QSqlTableModel* authors_last_name_model = new QSqlTableModel();
        authors_last_name_model->setTable("authors");
        authors_last_name_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        authors_last_name_model->select();
     
        authors_fields_completer["authors_last_name"] = new QCompleter();
        authors_fields_completer["authors_last_name"]->setModel(authors_last_name_model);
        authors_fields_completer["authors_last_name"]->setCompletionColumn(authors_last_name_model->fieldIndex("authors_last_name"));
        authors_fields_completer["authors_last_name"]->setCompletionMode(QCompleter::InlineCompletion);
     
        QSqlTableModel* authors_first_name_model = new QSqlTableModel();
        authors_first_name_model->setTable("authors");
        authors_first_name_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        authors_first_name_model->select();
     
        authors_fields_completer["authors_first_name"] = new QCompleter();
        authors_fields_completer["authors_first_name"]->setModel(authors_first_name_model);
        authors_fields_completer["authors_first_name"]->setCompletionColumn(authors_first_name_model->fieldIndex("authors_first_name"));
        authors_fields_completer["authors_first_name"]->setCompletionMode(QCompleter::InlineCompletion);
        /* END COMPLETERS: AUTHORS */
     
        /* BEGIN COMPLETERS: KEYWORDS */
        QMap<QString, QCompleter*> keywords_fields_completer;
     
        QSqlTableModel* keywords_text_model = new QSqlTableModel();
        keywords_text_model->setTable("keywords");
        keywords_text_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        keywords_text_model->select();
     
        keywords_fields_completer["keywords_text"] = new QCompleter();
        keywords_fields_completer["keywords_text"]->setModel(keywords_text_model);
        keywords_fields_completer["keywords_text"]->setCompletionColumn(1);
        keywords_fields_completer["keywords_text"]->setCompletionMode(QCompleter::InlineCompletion);
        /* END COMPLETERS: KEYWORDS */
     
        QLabel* label = NULL;
        unsigned int fieldIndex;
        QString labelText;
        QWidget* tmpEditionWidget = NULL;
        QLabel* tmpViewWidget = NULL;
     
        mainLayout = new QVBoxLayout;
     
        informations_groupBox = new QGroupBox(tr("Main informations"));
     
        informations_hBoxLayout = new QHBoxLayout;
     
        informations_left_formLayout = new QFormLayout;
     
        /* BEGIN: BOOKS_TITLE */
        fieldIndex = booksModel->fieldIndex("books_title");
     
        books_title_lineEdit = new QLineEdit();
        books_title_lineEdit->setPlaceholderText(tr("Type title"));
        books_title_lineEdit->setMinimumWidth(200);
        tmpEditionWidget = books_title_lineEdit;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_title_view_label = new QLabel;
        tmpViewWidget = books_title_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Book title");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        informations_left_formLayout->addRow(label, tmpEditionWidget);
        //informations_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_TITLE */
     
     
        /* BEGIN: BOOKS_EDITOR */
        fieldIndex = booksModel->fieldIndex("books_editor");
     
        books_editor_lineEdit = new QLineEdit();
        books_editor_lineEdit->setPlaceholderText(tr("Type editor name"));
        books_editor_lineEdit->setMinimumWidth(200);
        tmpEditionWidget = books_editor_lineEdit;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_editor_view_label = new QLabel;
        tmpViewWidget = books_editor_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Book Editor");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        informations_left_formLayout->addRow(label, tmpEditionWidget);
        //informations_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_EDITOR */
     
     
        /* BEGIN: BOOKS_YEAR */
        fieldIndex = booksModel->fieldIndex("books_year");
     
        books_year_spinBox = new QSpinBox();
        books_year_spinBox->setRange(0, 2200);
        books_year_spinBox->setValue(1980);
        tmpEditionWidget = books_year_spinBox;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_year_view_label = new QLabel;
        tmpViewWidget = books_year_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Edition year");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        informations_left_formLayout->addRow(label, tmpEditionWidget);
        //informations_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_YEAR */
     
     
        /* BEGIN: BOOKS_DESCRIPTION */
        fieldIndex = booksModel->fieldIndex("books_description");
     
        books_description_plainTextEdit = new QPlainTextEdit();
        tmpEditionWidget = books_description_plainTextEdit;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_description_view_label = new QLabel;
        tmpViewWidget = books_description_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Description");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        informations_left_formLayout->addRow(label, tmpEditionWidget);
        //informations_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_DESCRIPTION */
     
        informations_hBoxLayout->addLayout(informations_left_formLayout);
     
        informations_right_formLayout = new QFormLayout;
     
     
        /* BEGIN: BOOKS_AUTHORS */
        fieldIndex = booksModel->fieldIndex("books_authors");
     
        authors_widget = new InteractiveListWidget(
                    booksModel->getAuthorsFields(),
                    authors_fields_completer,
                    booksModel->getAuthorsFieldsPattern(),
                    1
                    );
        tmpEditionWidget = authors_widget;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        authors_view_label = new QLabel;
        tmpViewWidget = authors_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Authors");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        informations_right_formLayout->addRow(label, tmpEditionWidget);
        //informations_right_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_AUTHORS */
     
        informations_hBoxLayout->addLayout(informations_right_formLayout);
     
        informations_groupBox->setLayout(informations_hBoxLayout);
     
        mainLayout->addWidget(informations_groupBox);
     
        characteristics_groupBox = new QGroupBox(tr("Book characteristics"));
     
        characteristics_hBoxLayout = new QHBoxLayout;
     
        characteristics_left_formLayout = new QFormLayout;
     
     
        /* BEGIN: BOOKS_NBR_PAGES */
        fieldIndex = booksModel->fieldIndex("books_nbr_pages");
     
        books_nbr_pages_spinBox = new QSpinBox();
        books_nbr_pages_spinBox->setRange(0, 2000);
        books_nbr_pages_spinBox->setValue(100);
        books_nbr_pages_spinBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
        tmpEditionWidget = books_nbr_pages_spinBox;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_nbr_pages_view_label = new QLabel;
        tmpViewWidget = books_nbr_pages_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Page Number");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        characteristics_left_formLayout->addRow(label, tmpEditionWidget);
        //characteristics_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_NBR_PAGES */
     
     
        /* BEGIN: BOOKS_WEIGHT */
        fieldIndex = booksModel->fieldIndex("books_weight");
     
        books_weight_doubleSpinBox = new QDoubleSpinBox();
        books_weight_doubleSpinBox->setSuffix(" g");
        books_weight_doubleSpinBox->setRange(0, 10000);
        books_weight_doubleSpinBox->setValue(1000);
        books_weight_doubleSpinBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
        tmpEditionWidget = books_weight_doubleSpinBox;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_weight_view_label = new QLabel;
        tmpViewWidget = books_weight_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Weight");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        characteristics_left_formLayout->addRow(label, tmpEditionWidget);
        //characteristics_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_WEIGHT */
     
     
        /* BEGIN: BOOKS_WIDTH */
        fieldIndex = booksModel->fieldIndex("books_width");
     
        books_width_doubleSpinBox = new QDoubleSpinBox();
        books_width_doubleSpinBox->setSuffix(" cm");
        books_width_doubleSpinBox->setRange(0, 10000);
        books_width_doubleSpinBox->setValue(1000);
        books_width_doubleSpinBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
        tmpEditionWidget = books_width_doubleSpinBox;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_width_view_label = new QLabel;
        tmpViewWidget = books_width_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Width");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        characteristics_left_formLayout->addRow(label, tmpEditionWidget);
        //characteristics_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_WIDTH */
     
     
        /* BEGIN: BOOKS_HEIGHT */
        fieldIndex = booksModel->fieldIndex("books_height");
     
        books_height_doubleSpinBox = new QDoubleSpinBox();
        books_height_doubleSpinBox->setSuffix(" cm");
        books_height_doubleSpinBox->setRange(0, 10000);
        books_height_doubleSpinBox->setValue(1000);
        books_height_doubleSpinBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
        tmpEditionWidget = books_height_doubleSpinBox;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        books_height_view_label = new QLabel;
        tmpViewWidget = books_height_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Height");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        characteristics_left_formLayout->addRow(label, tmpEditionWidget);
        //characteristics_left_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_HEIGHT */
     
        characteristics_hBoxLayout->addLayout(characteristics_left_formLayout);
     
        characteristics_right_formLayout = new QFormLayout;
     
     
        /* BEGIN: BOOKS_KEYWORDS */
        fieldIndex = booksModel->fieldIndex("books_keywords");
     
        keywords_widget = new InteractiveListWidget(
                    booksModel->getKeywordsFields(),
                    keywords_fields_completer,
                    booksModel->getKeywordsFieldsPattern(),
                    1
                    );
        tmpEditionWidget = keywords_widget;
        editionWidgets[fieldIndex] = tmpEditionWidget;
     
        keywords_view_label = new QLabel;
        tmpViewWidget = keywords_view_label;
        viewWidgets[fieldIndex] = tmpViewWidget;
     
        labelText = tr("Keywords");
        labelTexts[fieldIndex] = labelText;
     
        label = new QLabel(labelText);
        labels[fieldIndex] = label;
     
        fieldsModified[fieldIndex] = false;
     
        characteristics_right_formLayout->addRow(label, tmpEditionWidget);
        //characteristics_right_formLayout->addRow(label, tmpViewWidget);
     
        setIndexWidget(booksModel->index(row, fieldIndex), tmpEditionWidget);
        //setIndexWidget(booksModel->index(row, fieldIndex), tmpViewWidget);
        /* END: BOOKS_KEYWORDS */
     
        characteristics_hBoxLayout->addLayout(characteristics_right_formLayout);
     
        characteristics_groupBox->setLayout(characteristics_hBoxLayout);
     
        mainLayout->addWidget(characteristics_groupBox);
     
        setLayout(mainLayout);
    }
     
    void SingleBookView2::startEditingNewBook() {
        if(fieldsModified.values().contains(true)) {
            QMessageBox::StandardButton returnButton =
                    QMessageBox::question(this, tr("Modified data unsaved"),
                                          tr("There are some unsaved changes that "
                                             "will be lost if you add a new book. "
                                             "Are you sure?"),
                                          QMessageBox::Yes | QMessageBox::No,
                                          QMessageBox::Yes
                                          );
     
            if(returnButton == QMessageBox::No)
                return;
        }
     
        //model->setFilter("");
     
        int newRow = model->rowCount();
     
        if(!model->insertRow(newRow))
            qDebug() << "Could not insert row";
     
        //dwm->toLast();
     
        for(QMap<unsigned int, bool>::const_iterator it = fieldsModified.constBegin(),
            end = fieldsModified.constEnd(); it != end; ++it)
        {
            modifyField(it.key(), false);
        }
    }
     
    void SingleBookView2::changeBook(QModelIndex& index) {
        if(fieldsModified.values().contains(true)) {
            QMessageBox::StandardButton returnButton =
                    QMessageBox::question(this, tr("Modified data unsaved"),
                                          tr("There are some unsaved changes that "
                                             "will be lost if you change book. "
                                             "Are you sure?"),
                                          QMessageBox::Yes | QMessageBox::No,
                                          QMessageBox::Yes
                                          );
     
            if(returnButton == QMessageBox::No)
                return;
        }
     
        //model->setFilter("id_book = "+ QString::number(id_book));
     
        //dwm->setCurrentModelIndex(index);
     
        for(QMap<unsigned int, bool>::const_iterator it = fieldsModified.constBegin(),
            end = fieldsModified.constEnd(); it != end; ++it)
        {
            modifyField(it.key(), false);
        }
    }
     
    void SingleBookView2::submit() {
        //dwm->submit();
     
        //model->setFilter("id_book = "+ QString::number(id_book));
     
        for(QMap<unsigned int, bool>::const_iterator it = fieldsModified.constBegin(),
            end = fieldsModified.constEnd(); it != end; ++it)
        {
            modifyField(it.key(), false);
        }
    }
     
    void SingleBookView2::revert() {
        if(fieldsModified.values().contains(true)) {
            QMessageBox::StandardButton returnButton =
                    QMessageBox::question(this, tr("Modified data unsaved"),
                                          tr("There are some unsaved changes that "
                                             "will be lost if you cancel. Are you "
                                             "sure?"),
                                          QMessageBox::Yes | QMessageBox::No,
                                          QMessageBox::Yes
                                          );
     
            if(returnButton == QMessageBox::No)
                return;
        }
     
        //dwm->revert();
     
        for(QMap<unsigned int, bool>::const_iterator it = fieldsModified.constBegin(),
            end = fieldsModified.constEnd(); it != end; ++it)
        {
            modifyField(it.key(), false);
        }
    }
     
    void SingleBookView2::modifyField(unsigned int section, bool isModified) {
        if(isModified)
            labels.value(section)->setText("<i>"+ labelTexts.value(section) +"</i>");
        else
            labels.value(section)->setText(labelTexts.value(section));
     
        fieldsModified[section] = isModified;
    }
     
    void SingleBookView2::toNext() {
        if(fieldsModified.values().contains(true)) {
            QMessageBox::StandardButton returnButton =
                    QMessageBox::question(this, tr("Modified data unsaved"),
                                          tr("There are some unsaved changes that "
                                             "will be lost if you change book. "
                                             "Are you sure?"),
                                          QMessageBox::Yes | QMessageBox::No,
                                          QMessageBox::Yes
                                          );
     
            if(returnButton == QMessageBox::No)
                return;
        }
     
        //dwm->toNext();
     
        for(QMap<unsigned int, bool>::const_iterator it = fieldsModified.constBegin(),
            end = fieldsModified.constEnd(); it != end; ++it)
        {
            modifyField(it.key(), false);
        }
    }
     
    void SingleBookView2::toPrevious() {
        if(fieldsModified.values().contains(true)) {
            QMessageBox::StandardButton returnButton =
                    QMessageBox::question(this, tr("Modified data unsaved"),
                                          tr("There are some unsaved changes that "
                                             "will be lost if you change book. "
                                             "Are you sure?"),
                                          QMessageBox::Yes | QMessageBox::No,
                                          QMessageBox::Yes
                                          );
     
            if(returnButton == QMessageBox::No)
                return;
        }
     
        //dwm->toPrevious();
     
        for(QMap<unsigned int, bool>::const_iterator it = fieldsModified.constBegin(),
            end = fieldsModified.constEnd(); it != end; ++it)
        {
            modifyField(it.key(), false);
        }
    }
     
    void SingleBookView2::setEditionEnabled(bool enable) {
     
    }
     
    QRect SingleBookView2::visualRect(const QModelIndex &index) const {
        if(viewWidgets.contains(index.column())) {
            return viewWidgets.value(index.column())->geometry();
        }
        return QRect();
    }
     
    void SingleBookView2::scrollTo(const QModelIndex &index, ScrollHint hint) {
        return;
    }
     
    QModelIndex SingleBookView2::indexAt(const QPoint &point) const {
        for(QMap<unsigned int, QLabel*>::const_iterator it = viewWidgets.constBegin(),
            end = viewWidgets.constEnd(); it != end; ++it)
        {
            if(it.value()->geometry().contains(point)) {
                QModelIndex curIndex = model->index(index.row(), it.key());
                return curIndex;
            }
        }
     
        return QModelIndex();
    }
     
    QModelIndex SingleBookView2::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) {
        QModelIndex resIndex;
        QModelIndex curIndex = currentIndex();
     
        switch(cursorAction) {
        case QAbstractItemView::MoveUp:
        case QAbstractItemView::MoveLeft:
        case QAbstractItemView::MovePageUp:
        case QAbstractItemView::MovePrevious:
            if(curIndex.column() == 0)
                resIndex = curIndex;
     
            resIndex = model->index(curIndex.row(), curIndex.column() - 1);
            break;
     
        case QAbstractItemView::MoveDown:
        case QAbstractItemView::MoveRight:
        case QAbstractItemView::MovePageDown:
        case QAbstractItemView::MoveNext:
            if(curIndex.column() == model->columnCount() - 1)
                resIndex = curIndex;
     
            resIndex = model->index(curIndex.row(), curIndex.column() + 1);
            break;
     
        case QAbstractItemView::MoveHome:
            resIndex = model->index(curIndex.row(), 0);
            break;
     
        case QAbstractItemView::MoveEnd:
            resIndex = model->index(curIndex.row(), model->columnCount() - 1);
            break;
        }
     
        return resIndex;
    }
     
    int SingleBookView2::horizontalOffset() const {
        return 0;
    }
     
    int SingleBookView2::verticalOffset() const {
        return 0;
    }
     
    bool SingleBookView2::isIndexHidden(const QModelIndex &index) const {
        return false;
    }
     
    void SingleBookView2::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) {
        return;
    }
     
    QRegion SingleBookView2::visualRegionForSelection(const QItemSelection &selection) const {
        return QRect();
    }

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 14
    Points : 9
    Points
    9
    Par défaut
    Finalement sur les conseils de PasNox de #qt-fr, j'ai opté pour un QStackedWidget perso qui s'occupe de changer le widget affiché et le mapping (du QDataWidgetMapper) en fonction du mode choisi (édition ou vue).

    Ce n'est pas satisfaisant de mon point de vue parce qu'il doit certainement y avoir moyen d'obtenir avec QDataWidgetMapper le même comportement qu'avec n'importe quelle autre vue (donc "affichage des données du modèle et après un certain event, affichage des éditeurs appropriés pour l'édition de ces données avec l'utilisation d'un delegate approprié").

    Mais je ne trouve pas comment faire et personne ne semble pouvoir m'aider donc je vais me contenter de l'autre solution dans un premier temps et je suis ouvert à toute suggestion et toute piste me permettant de réaliser ça comme je le voudrais


    Merci à PasNox

    Ciao

Discussions similaires

  1. Réponses: 1
    Dernier message: 10/03/2015, 08h02
  2. Réponses: 13
    Dernier message: 21/12/2014, 22h42
  3. Associer un QDataWidgetMapper à un QComboBox
    Par pyngux dans le forum Qt
    Réponses: 4
    Dernier message: 01/05/2014, 11h36
  4. Réponses: 2
    Dernier message: 07/04/2014, 17h37
  5. MyQItemDelegate::createEditor n'est jamais appelé
    Par traiangueul dans le forum Qt
    Réponses: 8
    Dernier message: 16/12/2010, 14h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo