0

Problem in getting mouseEvent on QTableWidget, this code is to create a window with tabelwidget and mouseclickevent, when i click right button of mouse then i got two action 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);
    }
Kirill Chernikov
  • 1,387
  • 8
  • 21
Srikanth Togara
  • 49
  • 2
  • 11
  • 2
    `QWidget`s have especial `contextMenuEvent` for such operations. But what is a problem? – ilotXXI Aug 25 '16 at 10:40
  • i want to add a new row with 3 columns every time when i click add event, for that am struggling a lot from morning but i didn't get any solution please help me to find solution. – Srikanth Togara Aug 25 '16 at 10:55
  • If you want to increase the number of rows by one just use `table->setRowCount(table->rowCount() + 1)`. Is that not what you're looking for? – G.M. Aug 25 '16 at 12:08

1 Answers1

0

You have 2 questions in one: how to create a context menu and how to add a row.

How to create a context menu.

Step 1: create a menu once. In constructor, for example.

// _menu is a class member.
_menu = new QMenu(this);
_menu->addAction(add_action);
_menu->addAction(Delete_action);

Step 2: show it in corresponding event:

void Notepad::contextMenuEvent(QContextMenuEvent *event)
{
    _menu->exec(event->globalPos());
}

How to add a row.

At first, you need a table as a class member. Why do you create a new instance every time? They are different objects! You create a new table widget on each add call!

At second, you need to increase rows count and to fill a new row:

void Notepad::add()
{
    const int newIndex = _table->rowCount();
    _table->setRowCount(newIndex + 1);
    // Fill data in row with index newIndex.
    ...
}
ilotXXI
  • 1,075
  • 7
  • 9