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 34.5k 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
    #46

    her is my code

    header file @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <montableauwidget.h>
    #include <QtWidgets>
    class Dialog : public QWidget
    {
    Q_OBJECT

    public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
    public slots:
    void newfunction(int row,int col);
    private:
    MonTableauWidget *tableau;
    QVBoxLayout *layoutPrincipale ;

    };

    #endif // DIALOG_H
    @

    main.cpp

    @#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) ;
    tableau->setCurrentIndex(tableau->model()->index(0,0,QModelIndex()));

    //tableau->setCurrentIndex();
    

    // QKeyEvent event(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);

    QStringList list ;
    list << "Désignation" << "Qte" << "Prix/U" << "Total";
    tableau->setHorizontalHeaderLabels(list);
    tableau->setCurrentCell(0,0);
    layoutPrincipale->addWidget(tableau);
    layoutPrincipale->addWidget(button);
    setLayout(layoutPrincipale);
    
     connect(tableau,SIGNAL(cellActivated(int,int)),this,SLOT(newfunction(int,int)));
    

    }

    Dialog::~Dialog()
    {

    }

    void Dialog::newfunction(int row,int col)
    {

    QTableWidgetItem * item = new QTableWidgetItem() ;
    item->setIcon( *(new QIcon("imgs/editIcon.png")));
    tableau->setVerticalHeaderItem(row,item);
    for (int rowToDelete=0; rowToDelete < tableau->rowCount(); ++rowToDelete)
    {
        if(rowToDelete != row)
            tableau->takeVerticalHeaderItem(rowToDelete);
    }
    

    }

    @

    so, i'm in searching of signal or idea to help to detect when the user start typing or editing a row

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

      Did you search through QTableWidget's "documentation":http://qt-project.org/doc/qt-4.8/qtablewidget.html ?

      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
        #48

        thank's

        i have got the icon appear in verticalHeader

        her is the signal that i have used
        @ connect(tableau->selectionModel(),SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
        this,SLOT(newfunction(QModelIndex,QModelIndex))) ;
        @
        and this is the function

        @
        void Dialog::newfunction(QModelIndex current, QModelIndex prev)
        {
        int row = current.row() ;
        QTableWidgetItem * item = new QTableWidgetItem() ;
        item->setIcon( *(new QIcon("imgs/arrow.png")));
        tableau->setVerticalHeaderItem(row,item);
        for (int rowToDelete=0; rowToDelete < tableau->rowCount(); ++rowToDelete)
        {
        if(rowToDelete != row)
        tableau->takeVerticalHeaderItem(rowToDelete);
        }

        }
        @

        Plz i need your review about my code ( is bad , or good )

        and i need your hints to detect when the user start typing in a cell

        not when the cell is changed , so i will place another sign with "arrow.png" icon togother ):

        thank's

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

          Again, are you asking us to do consulting for 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
            #50

            [quote author="SGaist" date="1375217386"]Again, are you asking us to do consulting for your work ?[/quote]

            sorry, but not this what i mean

            i have consulte the documentation and tried many signal and slots
            but i don't get what i want

            i have find @cellChanged(int row, int column)@ but this is not what i want

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

              i have used this Signals but really no this what i want

              @
              cellChanged()
              cellEntered()
              currentCellChanged()
              currentItemChanged()
              itemActivated()
              itemChanged()
              itemEntered()
              @

              i want a signal that allows me to catch this behavior

              when the user enter in cell no thing happened her , but when he start typing her is my goal , "catch the start of editing or typing"

              plz d'ont unserstand me in wrong way, i have read the documentation many and many time but i don't get any thing

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

                This function is not ready made so you'll have to dig a bit deeper, look for QStyledItemDelegate and QEvent::EnterEditFocus

                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
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #53

                  Ah, I see now. Sorry, I misunderstood what you wanted the table to do.

                  Qt's model/view classes produce signals when you:

                  • Select a cell
                  • Finish typing into a cell

                  However, they don't provide signals when you start typing into a cell. So, you have to implement your own low-level event handlers to detect typing.

                  You can subclass the QTableWidget and reimplement "QWidget::keyPressEvent()":http://qt-project.org/doc/qt-5.1/qtwidgets/qwidget.html#keyPressEvent

                  @
                  class MyTable : public QTableWidget {
                  // ...
                  };

                  void MyTable::keyPressEvent(QKeyEvent* event) {

                  // 1. Check if the event is caused by typing
                  if (...) {
                      qDebug("The user typed something!");
                  }
                  
                  // 2. Pass the event back to the QTableWidget,
                  // to store the typed text
                  QTableWidget::keyPressEvent(event);
                  

                  }
                  @

                  See the "QKeyEvent":http://qt-project.org/doc/qt-5.1/qtgui/qkeyevent.html documentation and choose a way to do #1.

                  [quote author="SGaist" date="1375272324"]look for QStyledItemDelegate and QEvent::EnterEditFocus[/quote]Unfortunately, QEvent::EnterEditFocus is not available in normal Qt builds -- the QT_KEYPAD_NAVIGATION macro is not enabled.

                  [quote author="advseo32" date="1375270136"]plz d'ont unserstand me in wrong way, i have read the documentation many and many time but i don't get any thing [/quote]I understand that it can be tiring to read, especially if English is not your first language. Your perseverance is very good.

                  Please remember: When you ask questions (on qt-project.org, or on any other forum), it is very important to describe what you already tried.

                  Earlier, you said "and i need your hints to detect when the user start typing in a cell" but you didn't say what you tried. This was bad, because it looked like you haven't done any work yourself.

                  Later on, you explained "i have used this Signals but really no this what i want" and you listed the signals you tried. This was much better, because it showed us that you had done work yourself and tried many things.

                  Please continue to be detailed in your future questions.

                  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
                    #54

                    i'm already subclassed the Qtablewidget and i have implemented

                    keyPressEvent(QKeyEvent* event)

                    this is the code @void MonTableauWidget::keyPressEvent(QKeyEvent *event)
                    {

                    int LastRow = (this->rowCount() == 0) ? 0 : this->rowCount()-1 ;
                    int currentRows = this->currentRow() ;
                    switch (event->key()) {
                    case Qt::Key_Down :
                        if( this->item(LastRow,0) != 0)
                          LastRow == currentRows ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ;
                        else
                          QTableWidget::keyPressEvent(event);
                        break;
                    case Qt::Key_Up :
                        if(LastRow > 0 & this->item(LastRow,0) == 0)
                         LastRow == currentRows ? this->removeRow(LastRow) : QTableWidget::keyPressEvent(event) ;
                        else
                          QTableWidget::keyPressEvent(event);
                        break;
                    

                    /*
                    i will try to do this
                    case Qt::(key != Keys(down,up,..etc) :
                    setVerticalheader(row,icon);
                    */
                    default:
                    QTableWidget::keyPressEvent(event);
                    break;
                    }

                    }@

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

                      I tried this for Key_p, but i need this action for other case != Key_Dow,Key_Up

                      this is a part of above code

                      @void MonTableauWidget::keyPressEvent(QKeyEvent *event)
                      {

                      int LastRow = (this->rowCount() == 0) ? 0 : this->rowCount()-1 ;
                      int currentRows = this->currentRow() ;
                      //int row = this->selectionModel()->currentIndex().row();
                      QTableWidgetItem * item = new QTableWidgetItem() ;
                      switch (event->key()) {
                      case Qt::Key_Down :
                          if( this->item(LastRow,0) != 0)
                            LastRow == currentRows ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ;
                          else
                            QTableWidget::keyPressEvent(event);
                          break;
                      case Qt::Key_Up :
                          if(LastRow > 0 & this->item(LastRow,0) == 0)
                           LastRow == currentRows ? this->removeRow(LastRow) : QTableWidget::keyPressEvent(event) ;
                          else
                            QTableWidget::keyPressEvent(event);
                          break;
                      
                      case Qt::Key_P :
                      
                           item->setIcon( *(new QIcon("imgs/Edit-icon.png")));
                           this->setVerticalHeaderItem(currentRows,item);
                           QTableWidget::keyPressEvent(event);
                          break;
                      
                      default:
                          QTableWidget::keyPressEvent(event);
                          break;
                      }
                      

                      ;@

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

                        Now it's working

                        i need your advises about my implementation, (it's bad ? , it's good ?)
                        @void MonTableauWidget::keyPressEvent(QKeyEvent *event)
                        {

                        int LastRow = (this->rowCount() == 0) ? 0 : this->rowCount()-1 ;
                        int currentRows = this->currentRow() ;
                        QTableWidgetItem * item = new QTableWidgetItem() ;
                        switch (event->key()) {
                        case Qt::Key_Down :
                            if( this->item(LastRow,0) != 0)
                              LastRow == currentRows ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ;
                            else
                              QTableWidget::keyPressEvent(event);
                            break;
                        case Qt::Key_Up :
                            if(LastRow > 0 && this->item(LastRow,0) == 0)
                             LastRow == currentRows ? this->removeRow(LastRow) : QTableWidget::keyPressEvent(event) ;
                            else
                              QTableWidget::keyPressEvent(event);
                            break;
                        default:
                            item->setIcon( *(new QIcon("imgs/Edit-icon.png")));
                            this->setVerticalHeaderItem(currentRows,item);
                            QTableWidget::keyPressEvent(event);
                            break;
                        }
                        

                        }
                        @

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

                          [quote author="JKSH" date="1375283945"] Unfortunately, QEvent::EnterEditFocus is not available in normal Qt builds — the QT_KEYPAD_NAVIGATION macro is not enabled.[/quote]

                          Right ! I forgot about that one

                          Quick code review (not talking about whether it works)
                          Your variables name doesn't seem to follow a coding style, use camel case everywhere will make it more readable (there are different styles but that's another story let's use this one). Also even if this is code have a good grammar/orthography:

                          @int currentRows = this->currentRow(); @

                          currentRows implies that there are several rows implicated which is not the case following your statement.

                          @int lastRow = qMax(0, this->rowCount() -1);@

                          You should also refactor your tests, using one liner like that doesn't make the code readable, especially if in the end the second choice is the same as the else code.

                          @
                          if (this->item(LastRow,0) != 0) &&
                          lastRow == currentRows)
                          this->insertRow(this->rowCount());
                          else
                          QTableWidget::keyPressEvent(event);
                          @

                          If you have a test that is done repeatedly you can also use a variable with a meaningful name

                          @bool isCurrentRowLast = (lastRow == currentRow);@

                          Don't forget that the code you are writing now might be the same code that you'll have to read in a year. Being able to go through it without having to do deep analyzes to understand what is does will prove valuable also for your co-workers.

                          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
                            #58

                            Her is my code , after your advices

                            @void MonTableauWidget::keyPressEvent(QKeyEvent *event)
                            {

                            int LastRow =qMax(0,this->rowCount()-1);
                            int CurrentRow = this->currentRow() ;
                            bool isCurrentRowLast = (LastRow == CurrentRow);
                            bool isCurrentCellEmpty = this->item(LastRow,1) == 0;
                            QTableWidgetItem * item = new QTableWidgetItem() ;
                            switch (event->key()) {
                            case Qt::Key_Down :
                                if( !isCurrentCellEmpty)
                                  (isCurrentRowLast) ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ;
                                else
                                  QTableWidget::keyPressEvent(event);
                                break;
                            case Qt::Key_Up :
                                if(LastRow > 0 && isCurrentCellEmpty)
                                 isCurrentRowLast ? this->removeRow(LastRow) : QTableWidget::keyPressEvent(event) ;
                                else
                                  QTableWidget::keyPressEvent(event);
                                break;
                            default:
                                item->setIcon( *(new QIcon("imgs/Edit-icon.png")));
                                this->setVerticalHeaderItem(CurrentRow,item);
                                QTableWidget::keyPressEvent(event);
                                break;
                            }
                            

                            }@

                            1 Reply Last reply
                            0
                            • JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #59
                              1. This is a memory leak:
                                @
                                item->setIcon( *(new QIcon("imgs/Edit-icon.png")));
                                @
                                setIcon() will make a copy of your icon, but the original stays in memory forever.

                              2. Your variable names have mixed style -- some start with upper-case (e.g. CurrentRow), but some start with lower-case (e.g. isCurrentRowLast). Choose one style and don't mix them.

                              In Qt's style, all function names and variable names start with lower-case, and all class names start with upper-case.

                              1. Your checking is complicated:
                                @
                                if( !isCurrentCellEmpty)
                                (isCurrentRowLast) ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ;
                                else
                                QTableWidget::keyPressEvent(event);
                                @
                                Just write if (!isCurrentCellEmpty && isCurrentRowLast). Your other case has the same issue.

                              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
                                #60

                                Ok

                                this my final version (Thank's for your advices )

                                @void MonTableauWidget::keyPressEvent(QKeyEvent *event)
                                {

                                int LastRow =qMax(0,this->rowCount()-1);
                                int CurrentRow = this->currentRow() ;
                                bool IsCurrentRowLast = (LastRow == CurrentRow);
                                bool IsCurrentCellEmpty = this->item(LastRow,1) == 0;
                                QTableWidgetItem * item = new QTableWidgetItem() ;
                                QIcon *ShowEditSignIcon = new QIcon("imgs/Edit-icon.png");
                                switch (event->key()) {
                                case Qt::Key_Down :
                                    if(IsCurrentRowLast && !IsCurrentCellEmpty)
                                      this->insertRow(this->rowCount()) ;
                                    else
                                      QTableWidget::keyPressEvent(event);
                                    break;
                                case Qt::Key_Up :
                                    if(LastRow > 0 && IsCurrentCellEmpty && IsCurrentRowLast)
                                     this->removeRow(LastRow) ;
                                    else
                                      QTableWidget::keyPressEvent(event);
                                    break;
                                default:
                                    item->setIcon(*ShowEditSignIcon);
                                    delete ShowEditSignIcon;
                                    this->setVerticalHeaderItem(CurrentRow,item);
                                    QTableWidget::keyPressEvent(event);
                                    break;
                                }
                                

                                }
                                @

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

                                  That looks a lot tidier, well done :)

                                  You don't have to create a QIcon on the heap with new QIcon(). Just create it on the stack (no pointer) with QIcon ShowEditSignIcon("imgs/Edit-icon.png"); -- then you don't have to delete it yourself.

                                  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
                                    #62

                                    Thank's JKSH and SGaist, now i can be said, i have figured out more than 30% of Model/view architecture in Qt

                                    if i have another question, i will post her , i will create another thread ?

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

                                      You're welcome. Good luck with Qt programming. :)

                                      You should start new threads for new topics. Remember to provide details of what you already tried.

                                      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
                                        #64

                                        i start use the same strategy as we did with Qtablewidget but

                                        there is no setVerticalHeaderItem(row,item) function like Qtablewidget(it seems that QtableWidget is very easy than Qtableview

                                        my question her :

                                        should i sublcass QHeaderView class and implement neccessary function
                                        after setting up vertical header @setVerticalHeader(QHeaderView * header)@

                                        or i can use another solution with out subclassing QheaderView ???

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

                                          i have used this but no thing happened

                                          i use Qtableview with Qsqlquerymodel

                                          her is my code
                                          @ui->tableView->verticalHeader()->model()->
                                          setData(ui->tableView->verticalHeader()->model()->index(1,1),"**",Qt::DisplayRole) ;
                                          @

                                          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