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. [SOLVED] Insert row from Qtableview to another
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Insert row from Qtableview to another

Scheduled Pinned Locked Moved General and Desktop
68 Posts 3 Posters 35.4k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    advseo32
    wrote on last edited by
    #2

    i need some hints

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi,

      If you want some hints you'll have to give more information like: the code you are using to add the row and set the data, what result you are having etc...

      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
      • A Offline
        A Offline
        advseo32
        wrote on last edited by
        #4

        @ bool Mymodel::insertRows(int position, int rows, const QModelIndex &parent)
        {
        beginInsertRows(QModelIndex(), position, position+rows-1);
        // i want to get the event when the user click "Keydown"
        if (true)
        {
        // we add row with empty fields to allow user to fill them
        }

         endInsertRows();
         return true;
        

        }@

        her is an image that explain exacly what means!http://imageshack.us/photo/my-images/577/2ng9.png/(sell)!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          The link to your picture is broken. But it's not critical. If I've followed you correctly you are using a Sql database so just use a QSqlTableModel, they are designed for that and don't need to be subclassed (if you are not doing more than just modify the content of the database)

          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
          • A Offline
            A Offline
            advseo32
            wrote on last edited by
            #6

            Qsqltablemodel not working for me

            check this image, my english is not good, to expalin more

            http://mockupr.com/mu/mmfd6664

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              If I understood your picture correctly, you want some sort of database record editor. Then use a QTableWidget, a QSqlTableModel would require that you have a table in your database for this.

              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
              • A Offline
                A Offline
                advseo32
                wrote on last edited by
                #8

                yes , the user fill row one by one , example for application that uses this kind of thing is " Inventory management system" like inflow inventory

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Well then, use a QTableWidget for the editing part, unless you have a special table in your database for that.

                  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
                  • A Offline
                    A Offline
                    advseo32
                    wrote on last edited by
                    #10

                    but how row can be added automaticelly , " Firstly the table has one row
                    then the user fill that row, and when he clicks in Keydown, new row added

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      advseo32
                      wrote on last edited by
                      #11

                      i want to like the video,

                      https://www.youtube.com/watch?v=hkfmZwuc-YY

                      when the user entre data new row created

                      i can't post so i have modified this post

                      i can't control the addition of rows

                      her is my code

                      @#include "mainwindow.h"

                      MainWindow::MainWindow(QWidget *parent)
                      : QWidget(parent)
                      {

                      tableNouveau = new QTableWidget;
                      tableNouveau->setRowCount(1);
                      tableNouveau->setColumnCount(5);
                      QStringList list ;
                      list <<"N°"<<  "Désignation" << "Qte" << "Prix/U" << "Totale" ;
                      tableNouveau->setHorizontalHeaderLabels(list);
                      connect(tableNouveau,SIGNAL(cellClicked(int,int)),this,SLOT(test(int,int))) ;
                      QVBoxLayout *layoutPrincipale = new QVBoxLayout ;
                      layoutPrincipale->addWidget(tableNouveau);
                      setLayout(layoutPrincipale);
                      

                      }

                      void MainWindow::test(int row, int col)
                      {
                      // if(!tableNouveau->item(row,col)->text().isEmpty())
                      if(col==1)
                      {
                      tableNouveau->insertRow(1);
                      }

                      }

                      MainWindow::~MainWindow()
                      {

                      }
                      @

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #12

                        When the user enters data, the QTableWidget will emit some "signals":http://qt-project.org/doc/qt-5.1/qtwidgets/qtablewidget.html#signals. You should create your own slot to handle the signal(s) that you want.

                        Connect the signal to your slot. Inside your slot, call call "QTableWidget::insertRow()":http://qt-project.org/doc/qt-5.1/qtwidgets/qtablewidget.html#insertRow

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          advseo32
                          wrote on last edited by
                          #13

                          i can’t control the addition of rows

                          her is my code
                          @
                          #include "mainwindow.h"

                          MainWindow::MainWindow(QWidget *parent)
                              : QWidget(parent)
                          {
                           
                              tableNouveau = new QTableWidget;
                              tableNouveau->setRowCount(1);
                              tableNouveau->setColumnCount(5);
                              QStringList list ;
                              list <<"N°"<<  "Désignation" << "Qte" << "Prix/U" << "Totale" ;
                              tableNouveau->setHorizontalHeaderLabels(list);
                              connect(tableNouveau,SIGNAL(cellClicked(int,int)),this,SLOT(test(int,int))) ;
                              QVBoxLayout *layoutPrincipale = new QVBoxLayout ;
                              layoutPrincipale->addWidget(tableNouveau);
                              setLayout(layoutPrincipale);
                           
                           
                           
                          }
                           
                           
                           
                          void MainWindow::test(int row, int col)
                          {
                             // if(!tableNouveau->item(row,col)->text().isEmpty())
                                  if(col==1)
                                  {
                                      tableNouveau->insertRow(1);
                                  }
                           
                           
                          }
                           
                          MainWindow::~MainWindow()
                          {
                             
                          }
                          

                          @

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            What do you mean by "I can't control" ?

                            The code here will add a new row at row number 1 each time you click on a cell in the column 1.

                            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
                            • A Offline
                              A Offline
                              advseo32
                              wrote on last edited by
                              #15

                              i want to add it at the end of the table every time

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                Then change 1 by tableNouveau->rowCount()

                                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
                                • A Offline
                                  A Offline
                                  advseo32
                                  wrote on last edited by
                                  #17

                                  Wow, this works

                                  Now, what i want exactly is

                                  the user isn't able to add row until he fills all cells

                                  then he clicks "Key_Down" to add new row ( so her i will catch Qkeypressevent)

                                  Plz give somme hints, so i will try to implement this my selef and render the code for you to correct me

                                  thank's a lot SGaist

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    advseo32
                                    wrote on last edited by
                                    #18

                                    i have tried to inherif from Qtablewidget and override keypressevent function

                                    i have declared keypressevent function as signal

                                    it works with this condition
                                    @if ( event->key() == Qt::key_Down)@

                                    but when i add more conditions the app crached

                                    her is my code

                                    customtablewidget class @class MonTableauWidget : public QTableWidget
                                    {
                                    Q_OBJECT
                                    public:
                                    explicit MonTableauWidget(QWidget *parent = 0);

                                    signals:
                                    void keyPressEvent(QKeyEvent *event);
                                    public slots:

                                    };
                                    @

                                    the main of classe @#include "montableauwidget.h"

                                    MonTableauWidget::MonTableauWidget(QWidget *parent) :
                                    QTableWidget(parent)
                                    {

                                    }

                                    @

                                    and her is my main where i tried to connect siganl with slot @#include "dialog.h"

                                    Dialog::Dialog(QWidget *parent)
                                    : QWidget(parent)
                                    {
                                    layoutPrincipale = new QVBoxLayout(this);
                                    QPushButton *button = new QPushButton("Clear",this);
                                    tableau = new MonTableauWidget(this);
                                    tableau->setRowCount(1);
                                    tableau->setColumnCount(4) ;
                                    QKeyEvent event(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);

                                    QStringList list ;
                                    list << "Désignation" << "Qte" << "Prix/U" << "Total";
                                    tableau->setHorizontalHeaderLabels(list);
                                    layoutPrincipale->addWidget(tableau);
                                    layoutPrincipale->addWidget(button);
                                    setLayout(layoutPrincipale);
                                    connect(tableau,SIGNAL(cellChanged(int,int)),this,SLOT(addrow(int,int))) ;
                                    connect(tableau,SIGNAL(keyPressEvent(QKeyEvent*)),this,SLOT(newRecod(QKeyEvent*)));
                                    connect(button,SIGNAL(clicked()),tableau,SLOT(clear()));
                                    

                                    }

                                    Dialog::~Dialog()
                                    {

                                    }

                                    void Dialog::addrow(int row, int col)
                                    {
                                    if(col == 0)
                                    tableau->insertRow(tableau->rowCount());
                                    }
                                    // her is the problem
                                    void Dialog::newRecod(QKeyEvent *record)
                                    {
                                    QModelIndex index = tableau->currentIndex();
                                    int row = index.column();
                                    if(record->key() == Qt::Key_Down)
                                    if(tableau->item(row,0)->text().isEmpty())
                                    QMessageBox::information(this,"Empty Designation","Please Fill in the cell");
                                    else
                                    tableau->insertRow(tableau->rowCount());
                                    }

                                    @

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      keyPressEvent is not a signal, I would advise you to read its "documentation":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#keyPressEvent

                                      From your previous posts, and just to be sure, do you expect me to validate your work ?

                                      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
                                      • A Offline
                                        A Offline
                                        advseo32
                                        wrote on last edited by
                                        #20

                                        bq. From your previous posts, and just to be sure, do you expect me to validate your work ?

                                        sorry about that, i'm completely newbie , i have blocked to get a solution for my probelem to complete my first application

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          advseo32
                                          wrote on last edited by
                                          #21

                                          if i can't use keypressevent() as a signal

                                          how i can handle this event " when the uesr click Key_Down new row added in the bottom "

                                          1 Reply Last reply
                                          0

                                          • Login

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