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. spinbox input add rows ??

spinbox input add rows ??

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 6 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.
  • N Offline
    N Offline
    n-2204
    wrote on 29 Apr 2021, 11:55 last edited by
    #1

    how to add rows based on spinbox input ?
    i have qtableviiew so when user enter number in spinbox number. of rows should be added how to do this ?
    as i can add row using 2 ways on button click

    1.
    add row{
     model->insertRow(model->rowCount(QModelIndex()));
    }
    2.
    QList<QStandardItem*> newRow;
    QStandardItem* item1 = new QStandardItem(QString(""));
    newRow.append(item1);
    model->appendRow(newRow);
    

    how to do ??
    thanks in advance

    J A 2 Replies Last reply 29 Apr 2021, 12:01
    0
    • N n-2204
      29 Apr 2021, 11:55

      how to add rows based on spinbox input ?
      i have qtableviiew so when user enter number in spinbox number. of rows should be added how to do this ?
      as i can add row using 2 ways on button click

      1.
      add row{
       model->insertRow(model->rowCount(QModelIndex()));
      }
      2.
      QList<QStandardItem*> newRow;
      QStandardItem* item1 = new QStandardItem(QString(""));
      newRow.append(item1);
      model->appendRow(newRow);
      

      how to do ??
      thanks in advance

      J Offline
      J Offline
      JonB
      wrote on 29 Apr 2021, 12:01 last edited by
      #2

      @n-2204
      Either way, get the number value from the spin box and do a loop that many times to add multiple rows? There is also a insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) to insert multiple rows at a time.

      1 Reply Last reply
      4
      • N n-2204
        29 Apr 2021, 11:55

        how to add rows based on spinbox input ?
        i have qtableviiew so when user enter number in spinbox number. of rows should be added how to do this ?
        as i can add row using 2 ways on button click

        1.
        add row{
         model->insertRow(model->rowCount(QModelIndex()));
        }
        2.
        QList<QStandardItem*> newRow;
        QStandardItem* item1 = new QStandardItem(QString(""));
        newRow.append(item1);
        model->appendRow(newRow);
        

        how to do ??
        thanks in advance

        A Offline
        A Offline
        artwaw
        wrote on 29 Apr 2021, 12:03 last edited by
        #3

        @n-2204
        https://doc.qt.io/qt-5/signalsandslots.html

        Connect to the valueChanged(int) signal https://doc.qt.io/Qt-5/qspinbox.html#valueChanged

        It is up to you to figure out if value has increased or decreased but you can figure it out using rowCount()

        For more information please re-read.

        Kind Regards,
        Artur

        N 1 Reply Last reply 29 Apr 2021, 12:24
        3
        • A artwaw
          29 Apr 2021, 12:03

          @n-2204
          https://doc.qt.io/qt-5/signalsandslots.html

          Connect to the valueChanged(int) signal https://doc.qt.io/Qt-5/qspinbox.html#valueChanged

          It is up to you to figure out if value has increased or decreased but you can figure it out using rowCount()

          N Offline
          N Offline
          n-2204
          wrote on 29 Apr 2021, 12:24 last edited by
          #4

          @artwaw
          this is not working why??

          QObject::connect(ui.spinBox, SIGNAL(valueChanged(int)), ui.tableView, SLOT(setTableRows()));
          void tool::setTableRows()
          {
          int rows = ui.spinBox->value();
          model->setRowCount(rows);
          }
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 29 Apr 2021, 12:39 last edited by
            #5

            @n-2204 said in spinbox input add rows ??:

            this is not working why??

            Because you did something wrong.
            I would guess ui.tableView is not the correct receiver.

            Use the new signal/slot syntax to catch such errors during compile time.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • N Offline
              N Offline
              n-2204
              wrote on 29 Apr 2021, 14:16 last edited by n-2204
              #6

              tried after change ui.tableview to model but not working

                  QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); 
              void tool::setTableRows()
              {
                   int rows = ui.spinBox->value();
              model->setRowCount(rows);
              }
              

              or tried using insertrows also not working

                 QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); 
               void tool::setTableRows()
              {
              int noofrows = ui.spinBox->value();
              for (int i = 0;i < noofrows;++i)
              {
                  model->insertRows(i, model->rowCount(QModelIndex()));
              }
              }
              
              J 1 Reply Last reply 29 Apr 2021, 14:43
              0
              • N n-2204
                29 Apr 2021, 14:16

                tried after change ui.tableview to model but not working

                    QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); 
                void tool::setTableRows()
                {
                     int rows = ui.spinBox->value();
                model->setRowCount(rows);
                }
                

                or tried using insertrows also not working

                   QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); 
                 void tool::setTableRows()
                {
                int noofrows = ui.spinBox->value();
                for (int i = 0;i < noofrows;++i)
                {
                    model->insertRows(i, model->rowCount(QModelIndex()));
                }
                }
                
                J Offline
                J Offline
                JonB
                wrote on 29 Apr 2021, 14:43 last edited by JonB
                #7

                @n-2204
                First, put in debug/print statements just to see if slot called, instead of relying on seeing row count increase.

                Secondly, you have

                 QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); 
                

                but

                 void tool::setTableRows()
                

                Is model of class tool? Because if not you are not connecting the slot.

                If you followed @Christian-Ehrlicher's suggestion of changing to new signal/slot syntax and never used SIGNAL/SLOT() macros you would find out, and your coding would be better....

                And finally I doubt you really intend

                model->insertRows(i, model->rowCount(QModelIndex()));
                

                Why rowCount() as the number of rows to add?? I would have thought given your loop you would want 1 there. You are confusing insertRows() to add multiple rows without a loop versus a loop to add one row at a time....

                N 1 Reply Last reply 29 Apr 2021, 15:37
                2
                • J JonB
                  29 Apr 2021, 14:43

                  @n-2204
                  First, put in debug/print statements just to see if slot called, instead of relying on seeing row count increase.

                  Secondly, you have

                   QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); 
                  

                  but

                   void tool::setTableRows()
                  

                  Is model of class tool? Because if not you are not connecting the slot.

                  If you followed @Christian-Ehrlicher's suggestion of changing to new signal/slot syntax and never used SIGNAL/SLOT() macros you would find out, and your coding would be better....

                  And finally I doubt you really intend

                  model->insertRows(i, model->rowCount(QModelIndex()));
                  

                  Why rowCount() as the number of rows to add?? I would have thought given your loop you would want 1 there. You are confusing insertRows() to add multiple rows without a loop versus a loop to add one row at a time....

                  N Offline
                  N Offline
                  n-2204
                  wrote on 29 Apr 2021, 15:37 last edited by
                  #8

                  @JonB yes tool is my class

                  model = new QStandardItemModel(10, 13, this)
                  ui.tableView->setModel(model);
                  
                  J 1 Reply Last reply 29 Apr 2021, 16:32
                  0
                  • N n-2204
                    29 Apr 2021, 15:37

                    @JonB yes tool is my class

                    model = new QStandardItemModel(10, 13, this)
                    ui.tableView->setModel(model);
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 29 Apr 2021, 16:32 last edited by JonB
                    #9

                    @n-2204
                    You seem to have:

                        QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); 
                    void tool::setTableRows()
                    

                    You have just said that model is of type QStandardItemModel.

                    But setTableRows() is a method of tool.

                    And earlier you had:

                    QObject::connect(ui.spinBox, SIGNAL(valueChanged(int)), ui.tableView, SLOT(setTableRows()));
                    

                    which had the same issue,

                    For the third time, if you followed https://wiki.qt.io/New_Signal_Slot_Syntax you would get compile-time help preventing you doing what you are trying to do.

                    1 Reply Last reply
                    1
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 29 Apr 2021, 16:49 last edited by VRonin
                      #10

                      I suspect the 3rd argument might be this. You can at least put the connect inside a Q_ASSUME() so you get a runtime crash in debug mode if the connect fails

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      2
                      • N Offline
                        N Offline
                        n-2204
                        wrote on 29 Apr 2021, 17:20 last edited by
                        #11

                        yes, thanks using this works
                        but what happening is i added 10 rows and when i change value in spinbox to 1 then no. of rows in table also changing to 1

                        so i think using loop this prblm will be solved

                        int noofrows = ui.spinBox->value();
                           for (int i = 0;i < noofrows;i++)
                        {
                           model->insertRows(i,noofrows);
                        }
                        

                        if use this loop when i enter 1 in spinbox it adds two rows ?

                        J 1 Reply Last reply 29 Apr 2021, 17:26
                        0
                        • N n-2204
                          29 Apr 2021, 17:20

                          yes, thanks using this works
                          but what happening is i added 10 rows and when i change value in spinbox to 1 then no. of rows in table also changing to 1

                          so i think using loop this prblm will be solved

                          int noofrows = ui.spinBox->value();
                             for (int i = 0;i < noofrows;i++)
                          {
                             model->insertRows(i,noofrows);
                          }
                          

                          if use this loop when i enter 1 in spinbox it adds two rows ?

                          J Offline
                          J Offline
                          JonB
                          wrote on 29 Apr 2021, 17:26 last edited by JonB
                          #12

                          @n-2204
                          I already told you about that earlier.

                          And finally I doubt you really intend

                          model->insertRows(i, model->rowCount(QModelIndex()));

                          Why rowCount() as the number of rows to add?? I would have thought given your loop you would want 1 there. You are confusing insertRows() to add multiple rows without a loop versus a loop to add one row at a time....

                          EITHER a for loop with insertRow(), OR no for loop and insertRows(). You really should be able to understand this.

                          Put some qDebug() statements in to see for yourself what is going on. That is what debugging is about.

                          1 Reply Last reply
                          1
                          • N Offline
                            N Offline
                            n-2204
                            wrote on 30 Apr 2021, 04:27 last edited by
                            #13

                            ok understood
                            thank you all

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              n-2204
                              wrote on 30 Apr 2021, 10:56 last edited by
                              #14
                               tooll::tool(QWidget* parent)
                              : QMainWindow(parent)
                                {
                                ui.setupUi(this);
                               model = new QStandardItemModel(10, 13, this)
                               ui.tableView->setModel(model);
                               connect(ui.spinBox, SIGNAL(valueChanged(int)), model, 
                               SLOT(setTableRows())); 
                              }
                              void tool::setTable2Rows()
                                 { 
                               int noofrows2 = ui.spinBox_2->value();
                               for (int i = 0;i < noofrows2;++i)
                                {
                                    model2->insertRow(i);
                                  //qDebug() << i;
                              }
                              }
                              

                              when i am using new signal/slot why i am getting error
                              (Error (active) E0304 no instance of overloaded function)

                               connect(ui.spinBox, &QSpinBox::valueChanged, this,&tool::setTableRows);
                              connect( sender, &Sender::valueChanged,    receiver, &Receiver::updateValue);
                              
                              J V 2 Replies Last reply 30 Apr 2021, 11:00
                              0
                              • N n-2204
                                30 Apr 2021, 10:56
                                 tooll::tool(QWidget* parent)
                                : QMainWindow(parent)
                                  {
                                  ui.setupUi(this);
                                 model = new QStandardItemModel(10, 13, this)
                                 ui.tableView->setModel(model);
                                 connect(ui.spinBox, SIGNAL(valueChanged(int)), model, 
                                 SLOT(setTableRows())); 
                                }
                                void tool::setTable2Rows()
                                   { 
                                 int noofrows2 = ui.spinBox_2->value();
                                 for (int i = 0;i < noofrows2;++i)
                                  {
                                      model2->insertRow(i);
                                    //qDebug() << i;
                                }
                                }
                                

                                when i am using new signal/slot why i am getting error
                                (Error (active) E0304 no instance of overloaded function)

                                 connect(ui.spinBox, &QSpinBox::valueChanged, this,&tool::setTableRows);
                                connect( sender, &Sender::valueChanged,    receiver, &Receiver::updateValue);
                                
                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 30 Apr 2021, 11:00 last edited by
                                #15

                                @n-2204 Please post whole error message. Does your tooll class have setTableRows slot? I'm asking because I only see setTable2Rows()

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                N 1 Reply Last reply 30 Apr 2021, 11:29
                                0
                                • J jsulm
                                  30 Apr 2021, 11:00

                                  @n-2204 Please post whole error message. Does your tooll class have setTableRows slot? I'm asking because I only see setTable2Rows()

                                  N Offline
                                  N Offline
                                  n-2204
                                  wrote on 30 Apr 2021, 11:29 last edited by
                                  #16

                                  yes slot setTablerow() is also there

                                   void tool::setTableRows()
                                  { 
                                  int noofrows = ui.spinBox->value();
                                  for (int i = 0;i < noofrows;++i)
                                  {
                                    model->insertRow(i);
                                  //qDebug() << i;
                                  } 
                                  

                                  c++ no instance of overloaded function matches the argument list argument types are: (QSpinBox *, <unknown-type>, tooll , void (tool::)())
                                  Severity Code Description Project File Line Suppression State Detail Description
                                  Error (active) E0304 no instance of overloaded function "tool::connect" matches the argument list tool C:\User\source\repos\tool\tooll.cpp 265 argument types are: (QSpinBox *, <unknown-type>, tool , void (tool::)())

                                  1 Reply Last reply
                                  0
                                  • N n-2204
                                    30 Apr 2021, 10:56
                                     tooll::tool(QWidget* parent)
                                    : QMainWindow(parent)
                                      {
                                      ui.setupUi(this);
                                     model = new QStandardItemModel(10, 13, this)
                                     ui.tableView->setModel(model);
                                     connect(ui.spinBox, SIGNAL(valueChanged(int)), model, 
                                     SLOT(setTableRows())); 
                                    }
                                    void tool::setTable2Rows()
                                       { 
                                     int noofrows2 = ui.spinBox_2->value();
                                     for (int i = 0;i < noofrows2;++i)
                                      {
                                          model2->insertRow(i);
                                        //qDebug() << i;
                                    }
                                    }
                                    

                                    when i am using new signal/slot why i am getting error
                                    (Error (active) E0304 no instance of overloaded function)

                                     connect(ui.spinBox, &QSpinBox::valueChanged, this,&tool::setTableRows);
                                    connect( sender, &Sender::valueChanged,    receiver, &Receiver::updateValue);
                                    
                                    V Offline
                                    V Offline
                                    VRonin
                                    wrote on 30 Apr 2021, 16:06 last edited by
                                    #17

                                    @n-2204 said in spinbox input add rows ??:

                                    tooll::tool(QWidget* parent)

                                    ????

                                    What's the name of the class? tooll or tool? looks like a big mess here

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    1 Reply Last reply
                                    0
                                    • C Offline
                                      C Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 30 Apr 2021, 16:50 last edited by
                                      #18

                                      @n-2204 said in spinbox input add rows ??:

                                      c++ no instance of overloaded function matches the argument list argument types are: (QSpinBox *, <unknown-type>, tooll , void (tool::)())

                                      How about taking a single look into the documentation? The problem is exactly explained there.

                                      Simply writing something without understanding what you're doing doesn't help you at all.

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      1 Reply Last reply
                                      1
                                      • N Offline
                                        N Offline
                                        n-2204
                                        wrote on 30 Apr 2021, 17:26 last edited by n-2204
                                        #19

                                        thanks for sharing ref doc, its done

                                         connect(ui.spinBox, QOverload<int>::of(&QSpinBox::valueChanged), 
                                                 this, &tool::setTableRows);
                                        
                                        1 Reply Last reply
                                        0
                                        • N Offline
                                          N Offline
                                          n-2204
                                          wrote on 3 May 2021, 05:17 last edited by
                                          #20

                                          what changes need to do to end the row at end of row, now when i enter value in spinbox row is adding in top but i need to add the row at end how to do ?

                                           void tool::setTableRows()
                                          {
                                          int noofrows = ui.spinBox->value();
                                          for (int i = 0;i < noofrows;++i)
                                          {
                                             model->insertRow(i);
                                          }
                                           }
                                          connect(ui.spinBox, QOverload<int>::of(&QSpinBox::valueChanged), 
                                                   this, &tool::setTableRows);
                                          
                                          J 1 Reply Last reply 3 May 2021, 06:34
                                          0

                                          9/29

                                          29 Apr 2021, 16:32

                                          topic:navigator.unread, 20
                                          • Login

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