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

    [quote author="advseo32" date="1374923562"]
    @this->item(row,col)->text().isEmpty(); @

    but the programme crashes , what the probleme her ??[/quote]If there is no data in the cell, then this->item() returns NULL.

    Your program will crash if you dereference a null pointer.

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

      [quote author="JKSH" date="1374928103"][quote author="advseo32" date="1374923562"]
      @this->item(row,col)->text().isEmpty(); @

      but the programme crashes , what the probleme her ??[/quote]If there is no data in the cell, then this->item() returns NULL.

      Your program will crash if you dereference a null pointer.

      [/quote]

      Yeah! that's true , because QTableWidgetItem was created only when the user fill in the cell

      do you have another way to check if the cell if not empty

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

        [quote author="advseo32" date="1374942257"]do you have another way to check if the cell if not empty [/quote]If there is no data in the cell, then this->item() returns NULL :)

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

          [quote author="JKSH" date="1374942566"][quote author="advseo32" date="1374942257"]do you have another way to check if the cell if not empty [/quote]If there is no data in the cell, then this->item() returns NULL :)[/quote]

          Probably , you have give the answer from the the begning , but i haven't pay attention to it

          yes it's works
          @if (this->item(LastRow,0) != 0)@

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

            now

            i want to change the behavior of vertical header in Qtableview

            this is what i mean:

            when user start fill table cells, i want this sign(*) present in vertical header instead of numbers

            when user moves beteween rows , i want also this sign(>) present in vertical header instead of numbers

            what function i need to implement

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

              Did you read the documentation of the various classes you are using ? For example "cellChanged":http://qt-project.org/doc/qt-4.8/qtablewidget.html#cellChanged ?

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

                yes i did, but what functions are responsible for doing this

                sign(*) present in vertical header instead of numbers

                sign(>) present in vertical header instead of numbers

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

                  [quote author="SGaist" date="1374952377"]Did you read the documentation of the various classes you are using ?
                  [quote author="advseo32" date="1374964095"]yes i did, but what functions are responsible for...[/quote][/quote]Please read more thoroughly.

                  Go to the "documentation":http://qt-project.org/doc/qt-5.1/qtwidgets/qtablewidget.html again and search for "header". Try different functions with good names to see what they do.

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

                    i have try many functions but no result

                    Her is my try
                    this function called when the user start editing cell "CellChanged Signal"
                    @
                    void Dialog::newfunction()
                    {
                    QModelIndex index ;
                    index = tableau->model()->index(0,0) ;
                    tableau->verticalHeader()->model()->setData(index,QPixmap("imgs/arrow.png"),Qt::DecorationRole);
                    }

                    @

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

                      Good on you for trying.
                      [quote author="advseo32" date="1375041952"]
                      Her is my try
                      this function called when the user start editing cell "CellChanged Signal"
                      @
                      void Dialog::newfunction()
                      {
                      QModelIndex index ;
                      index = tableau->model()->index(0,0) ;
                      tableau->verticalHeader()->model()->setData(index,QPixmap("imgs/arrow.png"),Qt::DecorationRole);
                      }

                      @
                      [/quote]There are two major issues:

                      If you haven't set any header data, tableau->verticalHeader()->model() returns NULL

                      You have 2 models here. tableau->model() is the table model, tableau->verticalHeader()->model() is the header model. You created an index for the table model, so you can't use that index to set data in the header model.

                      Hint: If you use setHeaderData(), you don't need an index

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

                        i have use setHeaderData but no thing shown

                        @tableau->verticalHeader()->model()->setHeaderData(0,Qt::Vertical,"***", Qt::DisplayRole) ;@

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

                          Since you are using QTableWidget, did you take a look at this part of the "QTableWidget documentation":http://qt-project.org/doc/qt-4.8/qtablewidget.html#verticalHeaderItem ?

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

                            [quote author="advseo32" date="1375120030"]i have use setHeaderData but no thing shown

                            @tableau->verticalHeader()->model()->setHeaderData(0,Qt::Vertical,"***", Qt::DisplayRole) ;@[/quote]You are calling setHeaderData() on the header's header. Read my point #2 again.

                            Call setHeaderData on the table's header.

                            Read SGaist's link too.

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

                              Thank's JKSH

                              but really i don't get the code work perfectly, as you guide me

                              her my code @void Dialog::newfunction(int row,int col)
                              {
                              tableau->model()->setHeaderData(row,Qt::Vertical,"**",Qt::DisplayRole);

                              }

                              @

                              i try the idea of SGaist's idea and it work , and hes is the code

                              @
                              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);
                              }

                              }
                              @

                              the only problem now is how i can catch when the user change the selection of a spesfic row as well as the editing of cell

                              sorry for the my many question,

                              i'm newbie, but trustme i have got many for you, i think the Model/view

                              pattren becomes more and more easy for me , after try with your examples and hints

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

                                [quote author="advseo32" date="1375141407"]
                                @
                                tableau->model()->setHeaderData(row,Qt::Vertical,"**",Qt::DisplayRole);
                                @
                                [/quote]You used Qt::DecorationRole before -- that was the correct role. I'm not sure why you changed to Qt::DisplayRole.

                                Please read up on the "ItemDataRoles":http://qt-project.org/doc/qt-5.1/qtcore/qt.html#ItemDataRole-enum.

                                [quote]i try the idea of SGaist's idea and it work , and hes is the code
                                [/quote]Congratulations! :) I'm glad you read his link and learnt how to use QTableWidget::setVerticalHeaderItem().

                                [quote]sorry for the my many question,

                                i'm newbie, but trustme i have got many for you, i think the Model/view

                                pattren becomes more and more easy for me , after try with your examples and hints [/quote]It's ok to ask lots of questions, because that's how we learn.

                                But the problem is, you're not doing your research properly before asking questions. SGaist and I already gave you the link you to the QTableWidget documentation many times. Yet, you still ask us to help you find a function that is already listed on that page -- it shows us that you haven't read the page.

                                You're supposed to do some work too -- not just ask us to find functions for you.

                                Please learn to read the documentation before asking questions. Qt has very good class names and function names -- it should be easy to find the function you want by just reading the names.

                                [quote]the only problem now is how i can catch when the user change the selection of a spesfic row as well as the editing of cell[/quote]You haven't shown us what you've tried.

                                Go to the QTableWidget documentation

                                Read the list of “Public Functions”, “Public Slots”, and “Signals”.

                                When you find a function/slot/signal that sounds good, click on it and read the description.

                                If the description sounds good, try it in your code.

                                After that, if you still need help, you can ask by showing us what you've 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
                                  #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

                                          • Login

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