How can i add new row to the existing Qtablewidget?



  • Problem in getting mouseEvent on QTableWidget, this code is to create a window with tabelwidget and mouseclickevent, when i click right button of mouse the i got two actin event options named add and delete, i want to add new rows with 3 columns when i click "add" event function,and delete the last row when i click on "delete" event function,

    (sorry for my english), any help is appreciated.

    #include "notepad.h"
       #include <QMessageBox>
       #include <QTableView>
       #include <QMouseEvent>
    
       Notepad::Notepad() 
       {
    
            test() ;
    
           add_action = new QAction(tr("Add cell"), this);
           add_action->setIcon(QIcon("add.jpg"));
           Delete_action = new QAction(tr("Delete cell"), this);
           Delete_action->setIcon(QIcon("delete.jpg"));
    
           connect(Delete_action, SIGNAL(triggered()), this, SLOT(Delete()));
           connect(add_action, SIGNAL(triggered()), this, SLOT(add()));
    
           centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
           centralWidget()->setAttribute(Qt::WA_MouseTracking,true);
    
           setMouseTracking(true);
    
       }
       void Notepad::test() 
       {       
           QTableWidget* table = new QTableWidget();
           QTableWidgetItem* tableItem = new QTableWidgetItem();
    
           table->setRowCount(1);
           table->setColumnCount(3);
           table->setItem(0,0,new QTableWidgetItem());
           table->setItem(0,1,new QTableWidgetItem());
           table->setItem(0,2,new QTableWidgetItem());
    
           table->setMouseTracking(true);
           table->viewport()->setMouseTracking(true);
           table->installEventFilter(this);
           table->viewport()->installEventFilter(this);
    
           table->setSelectionBehavior(QAbstractItemView::SelectRows);
           table->setSelectionMode(QAbstractItemView::SingleSelection);
    
           tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);
           table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
           setCentralWidget(table);
    
    
       }
    
       void Notepad::mouseReleaseEvent (QMouseEvent * event )
       {   
    
    
           QMessageBox* msgBox;
           if(event->button() == Qt::RightButton)
             {
       QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
              QMenu *menu = new QMenu(this);
              menu->addAction(add_action);
              menu->addAction(Delete_action);
              menu->exec(mouseEvent->globalPos());
    
           } 
       }
       void Notepad::add() 
       {
           QTableWidget* table = new QTableWidget();
           test();
    
           table->setColumnCount(3);*/
    
    
           int newRow = table->rowCount();
           int newcol = table->columnCount();
           qDebug() << newRow;  
           for (int row ; row < newRow+1 ; ++row)
           {
               QWidget *parent;
               QStyleOptionViewItem option;
               for (int column = 0; column < 3; ++column)
               {
    
               table->insertRow( table->rowCount());
               table->insertColumn( newcol );
               }
           }
    
           setCentralWidget(table);
           centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
           setMouseTracking(true);
       }
       void Notepad::Delete() 
       {
           QTableWidget* table = new QTableWidget();
    
           add();
    
           int row=table->rowCount();
    
    
       if (int i= row){
           table->removeRow(i);
    
       }
           setCentralWidget(table);
           centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
           setMouseTracking(true);
       }
    

  • Qt Champions 2016

    Hi
    First of all, you should keep the same tablewidget so
    QTableWidget* table = new QTableWidget();
    should not be done in a function.
    so

    QTableWidget* table; --- > to live in mainwindow.h , as a class member

    and
    table = new QTableWidget(); -- > in mainwin constructor.

    Then use "table" in all functions.

    (easier way, simply use Designer and just place it. it can then be used with ui->tablewidget)

    so short story is that:
    void Notepad::Delete()
    {
    QTableWidget* table = new QTableWidget(); << looks wrong as u make new one

    so i think moving the table to a class member will help you alot.

    Remember when you add a new "row" , to set
    table->setRowCount(1); << should be increased



  • @mrjj how can i add rows and delete rows , please help me


  • Qt Champions 2016

    @srikanth

    You add with

       QTableWidgetItem* tableItem = new QTableWidgetItem();
       http://doc.qt.io/qt-5/qtablewidget.html#insertRow
    
       table->setItem(0,row,new QTableWidgetItem());
    

    and delete with
    http://doc.qt.io/qt-5/qtablewidget.html#removeRow

    Please see
    http://doc.qt.io/qt-5/qtablewidget.html



  • @mrjj thanks a lot. solved


Log in to reply
 

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