[SOLVED] Insert row from Qtableview to another



  • Hi, every body

    i have a probleme with inserting a row from a table call tableProduct to another table call tableSalle

    when the user click entre key in the first table "tableProduct"

    i can't figured out, how this work

    i start using

    @my@SalleModel->insertRow(tableProduct->currentIndex().row()) ;@

    but not working



  • i need some hints


  • Lifetime Qt Champion

    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...



  • @ 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)!


  • Lifetime Qt Champion

    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)



  • Qsqltablemodel not working for me

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

    http://mockupr.com/mu/mmfd6664


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



  • 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



  • 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()
    {

    }
    @


  • Moderators

    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



  • 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()
    {
       
    }
    

    @


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    Then change 1 by tableNouveau->rowCount()



  • 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



  • 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());
    }

    @


  • Lifetime Qt Champion

    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 ?



  • 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



  • 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 "


  • Lifetime Qt Champion

    It is explained in the function documentation



  • i still can't know what i do ,

    subclass Qtablewidget and implement keypressevent
    or use shortcuts .


  • Lifetime Qt Champion

    You can also use an event filter to catch that event, this really is up to you to choose the solution that meets your need



  • i have implemented keypressEvent function

    and it works perfectly, but when i start to somme conditions the programme crashes

    her is my code @void MonTableauWidget::keyPressEvent(QKeyEvent *event)
    {
    int LastRow = this->rowCount();

    switch (event->key()) {
    case Qt::Key_Down :
    // the problem occured  when i added the line below if(!this->item(LastRow,0)->text().isEmpty())
        this->insertRow(this->rowCount());
        break;
    case Qt::Key_Up :
     this->removeRow(1);
        break;
    case Qt::Key_0 :
    
        QMessageBox::information(this,"Row coutn",QString::number( LastRow)) ;
        break;
    default:
        QTableWidget::keyPressEvent(event);
        break;
    }
    

    }
    @


  • Lifetime Qt Champion

    @removeRow(1); << What would happen if you don't have a row number 1 ?@



  • i have used the debugger to determine what's the issue
    and i think the issue is located her
    because when i delete this line every thing works perfectly
    @this->item(LastRow,0)->text().isEmpty() ;@

    this not refering to the customQtableWidget it's reffering to the
    QtableWidgetItem objet



  • [quote author="SGaist" date="1374875944"]@removeRow(1); << What would happen if you don't have a row number 1 ?@[/quote]

    nothing is happened , means no row have to removed , but i have correct this issue

    @this->removeRow(this->rowCount()-1); @
    and this should works , and finally it works

    thank's for the hint



  • her is my finall code @void MonTableauWidget::keyPressEvent(QKeyEvent *event)
    {
    int LastRow = this->rowCount()-1;

    // bool ok = this->item(LastRow,0)->text().isEmpty() ;

    switch (event->key()) {
    case Qt::Key_Down :
        if(LastRow)
            this->insertRow(this->rowCount());
        break;
    case Qt::Key_Up :
     this->removeRow(LastRow);
        break;
    default:
        QTableWidget::keyPressEvent(event);
        break;
    }
    

    @



  • i lost hope to get the solution from my self,

    the only problem now is how to test if Cell is Empty

    i use this

    @this->item(row,col)->text().isEmpty(); @

    but the programme crashes , what the probleme her ??


  • Moderators

    [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 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


  • Moderators

    [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 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)@



  • 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


  • Lifetime Qt Champion

    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 ?



  • 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


  • Moderators

    [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.



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

    @


  • Moderators

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.