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. How can i add new row to the existing Qtablewidget?
QtWS25 Last Chance

How can i add new row to the existing Qtablewidget?

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetqmouseevent
6 Posts 3 Posters 20.7k Views
  • 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.
  • S Offline
    S Offline
    srikanth
    wrote on 25 Aug 2016, 10:52 last edited by A Former User
    #1

    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);
       }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 25 Aug 2016, 11:07 last edited by mrjj
      #2

      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

      S U 2 Replies Last reply 25 Aug 2016, 11:19
      1
      • M mrjj
        25 Aug 2016, 11:07

        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

        S Offline
        S Offline
        srikanth
        wrote on 25 Aug 2016, 11:19 last edited by
        #3

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

        M 1 Reply Last reply 25 Aug 2016, 11:27
        0
        • S srikanth
          25 Aug 2016, 11:19

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 25 Aug 2016, 11:27 last edited by
          #4

          @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

          S 1 Reply Last reply 25 Aug 2016, 13:11
          3
          • M mrjj
            25 Aug 2016, 11:27

            @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

            S Offline
            S Offline
            srikanth
            wrote on 25 Aug 2016, 13:11 last edited by
            #5

            @mrjj thanks a lot. solved

            1 Reply Last reply
            1
            • M mrjj
              25 Aug 2016, 11:07

              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

              U Offline
              U Offline
              Umer
              wrote on 6 Jan 2023, 03:53 last edited by
              #6

              @mrjj How to add and delete rows in table by clicking spinbox up and down arrow? Please help me through python code.

              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