Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot submit value of a spinbox [SOLVED]
Forum Update on Monday, May 27th 2025

Cannot submit value of a spinbox [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
18 Posts 2 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr. Kibu
    wrote on 18 May 2015, 20:08 last edited by Mr. Kibu
    #1

    Hello!

    If I call "mapper->submit" the data of a mapped spinbox is not written into the model, but the string of an lineedit does.

    In the class "mainwindow" I connect to the database (sqlite) and create a QSqlRelationalTableModel and show the data in a QTableView. Than I have a funktion where I open the data of one row in a dialog. In the funktion where I open the dialog, I pass the model to the dialog:

    void MainWindow::editBeleg()
    {
         //####get the value of the key-field
        QModelIndexList indexList = ui->tabBelege->selectionModel()->selectedIndexes();
        int row;
        foreach (QModelIndex index, indexList) {
            row = index.row();
        }
    
       int _selBelegKey = ui->tabBelege->model()->data(ui->tabBelege->model()->index(row,0)).toInt();
    
        frmBeleg newBeleg(_selBelegKey,modBelege, this);// <<<<<<<<modBelege is the model to pass!!!!
        newBeleg.exec();
    

    In the dialog I adopt the variables like this:

    frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *BelegMod, QWidget *parent) :
        QDialog(parent),
        ui(new Ui::frmBeleg)
    {
    
        ui->setupUi(this);
    

    In the dialog I can edit the data. If I edit the data of the spinbox by hand and submit the data to the model, it works fine and the data is in the database.
    If I set the value of a spinbox in a event (for example: ui->spinbox->setValue(4)), the data is NOT submited to the model. If I do that with a lineedit, everything works.

    How can I set the data of a spinbox from a event???

    Thanks for help!!

    Franz

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 May 2015, 20:19 last edited by
      #2

      Hi,

      Are you calling setValue before you setup the mapper ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mr. Kibu
        wrote on 18 May 2015, 20:22 last edited by
        #3

        Yes, submit is called in a submit-function.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 18 May 2015, 20:35 last edited by
          #4

          It's not what I'm asking; when are you calling ui->spinBox->setValue(4) ? Before or after setting up the mapper ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mr. Kibu
            wrote on 18 May 2015, 20:43 last edited by
            #5

            Ok, I set the mapper in the constructor, set the spinbox in a event-function and submit the data in a submit-function. So I think that I set the mapper befor.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 18 May 2015, 20:45 last edited by
              #6

              What event function ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mr. Kibu
                wrote on 18 May 2015, 21:04 last edited by
                #7
                void frmBeleg::on_txtAnm1_editingFinished()
                {
                    ui->txtBelegNr->setValue(4);//<<<< this is the spinbox that not work
                    ui->txtAnm2->setText("tessst");//<<<this is a combobox; after submit is the value in the database
                }```
                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 18 May 2015, 21:09 last edited by
                  #8

                  Can you show how you setup the mapper ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mr. Kibu
                    wrote on 18 May 2015, 21:14 last edited by
                    #9

                    Here is the constructor of the dialog (frmBeleg):

                    frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *BelegMod, QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::frmBeleg)
                    {
                        ui->setupUi(this);
                    
                        mapper=new QDataWidgetMapper(this);
                        mapper->setModel(BelegMod);
                        //mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
                        mapper->setItemDelegate(new QSqlRelationalDelegate(mapper));
                    
                        mapper->addMapping(ui->txtBelegNr,2);//<<<<<    the spinbox
                        mapper->addMapping(ui->txtAnm1,14);//<<<<<<<   the combo
                    
                        if (BelegID != -1) {
                                for (int row = 0; row < BelegMod->rowCount(); ++row) {
                                    QSqlRecord record = BelegMod->record(row);
                                    if (record.value(0).toInt() == BelegID) {//Spalte des Key-Feldes
                                        mapper->setCurrentIndex(row);
                                        break;
                                    }
                                }
                            }
                            else {
                                mapper->toFirst();
                                }
                    
                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mr. Kibu
                      wrote on 19 May 2015, 18:25 last edited by
                      #10

                      Addition:

                      It is no problem to use the passed model from the mainwindow in the constuctor of the dialog (frmBeleg).
                      When I use the model (BelegMod) in a function, than I have no error at buildung the application but the programm crashes when I use the function in the programm.

                      For example I tried:

                      void frmBeleg::on_btnSave_clicked()
                      {
                          mapper->submit();
                      
                          if (BelegMod->submitAll())
                              BelegMod->database().commit();
                          else
                              BelegMod->database().rollback();
                      
                          frmBeleg::close();
                      }
                      

                      Or:

                      void frmBeleg::on_btnNextBelegNr_clicked()
                      {
                          QSqlQueryModel modMaxBelNr;
                          modMaxBelNr.setQuery("SELECT max(BelegNr) FROM Belege");
                          int nextBelegNr = modMaxBelNr.data(modMaxBelNr.index(0, 0)).toInt();
                          BelegMod->setData(BelegMod->index(0,2), nextBelegNr+1);
                      }
                      

                      How can I use the passed model in a function?

                      Thank's

                      Franz

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 19 May 2015, 19:58 last edited by
                        #11

                        Are you initializing BelegMod properly in the constructor ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Mr. Kibu
                          wrote on 19 May 2015, 20:11 last edited by
                          #12

                          I hope that I have understand your question:

                          I am initializing the model "modBelege" in the function "on_actionBelege_triggered" like this:

                          void MainWindow::on_actionBelege_triggered()
                          {
                              ui->stackedWidget->setCurrentIndex(0);
                          
                              modBelege = new QSqlRelationalTableModel(this, cn::db());
                              modBelege->setEditStrategy(QSqlRelationalTableModel::OnFieldChange);
                              modBelege->setTable("belege");
                          
                              //modBelege->setRelation(19, QSqlRelation("Zahlarten","ZahlartKey","Zahlart"));
                              modBelege->setRelation(8, QSqlRelation("kontakte","AdrKey","Name"));
                              modBelege->select();
                              ......
                          

                          Then I pass the model (modBeleg) to the dialog:

                          void MainWindow::editBeleg()
                          {
                              //####search the row of the tableview "tabBelege"
                              QModelIndexList indexList = ui->tabBelege->selectionModel()->selectedIndexes();
                              int row;
                              foreach (QModelIndex index, indexList) {
                                  row = index.row();
                              }
                          
                              //####get Key-value in column 0
                          
                              int _selBelegKey = ui->tabBelege->model()->data(ui->tabBelege->model()->index(row,0)).toInt();
                          
                              //#### open dialog:
                              frmBeleg newBeleg(_selBelegKey,modBelege, this);
                              newBeleg.exec();
                          }
                          

                          Hope it helps!

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 19 May 2015, 20:22 last edited by
                            #13

                            In the frmBeleg constructor, do you assign modBelege to BelegMod ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mr. Kibu
                              wrote on 19 May 2015, 20:25 last edited by
                              #14

                              I have only this code in frmBeleg:

                              frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *BelegMod, QWidget *parent) :
                                  QDialog(parent),
                                  ui(new Ui::frmBeleg)
                              {
                                  ui->setupUi(this);
                                  ...
                              
                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 19 May 2015, 20:30 last edited by
                                #15

                                From your code it seems that you don't initialize your class member that is also named BeleMod

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  Mr. Kibu
                                  wrote on 19 May 2015, 20:34 last edited by
                                  #16

                                  And how can I do this? And is it better to rename BelegMod?

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 19 May 2015, 20:54 last edited by SGaist
                                    #17
                                    frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *BelegMod, QWidget *parent) :
                                        QDialog(parent),
                                        ui(new Ui::frmBeleg),
                                        BelegMod(BelegMod)
                                    {
                                    

                                    But yes,

                                    frmBeleg::frmBeleg(int BelegID, QSqlRelationalTableModel *model, QWidget *parent) :
                                        QDialog(parent),
                                        ui(new Ui::frmBeleg),
                                        BelegMod(model)
                                    {
                                    

                                    is clearer.

                                    One thing I recommend: use a clear naming scheme e.g. camel case with starting letter lowercased for variables and camel case for classes. It will make your code easier to read (following Qt's pattern is good idea)

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Mr. Kibu
                                      wrote on 20 May 2015, 19:03 last edited by
                                      #18

                                      Hi SGaist!

                                      It works. Thank you for your help!

                                      märssi

                                      Franz

                                      1 Reply Last reply
                                      0

                                      1/18

                                      18 May 2015, 20:08

                                      • Login

                                      • Login or register to search.
                                      1 out of 18
                                      • First post
                                        1/18
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved