Results 1 to 5 of 5

Thread: Open a pop up menu when right click on any cell of the table

  1. #1
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1

    Default Open a pop up menu when right click on any cell of the table

    Hi everyone...

    I want to know that how can i open a new pop up menu when i right click on the table items.In the pop up menu some actions like add, delete,update etc should be given which will create a new row or delete the selected row.I am a newbie in QT so i does not have any idea of QT so if anybody can give me full details(with code if possible) then i will bw really grateful towards him/her.

    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Open a pop up menu when right click on any cell of the table

    A popup menu in Qt is called a context menu.

    Implement the context menu events and signals.

  3. #3
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1

    Default Re: Open a pop up menu when right click on any cell of the table

    Quote Originally Posted by tbscope View Post
    A popup menu in Qt is called a context menu.

    Implement the context menu events and signals.

    Thank you for your reply but that i already know...Now what i want to know is how to implement that to my table so that when any cell is right clicked it should show me the options of add, delete etc

  4. #4
    Join Date
    Aug 2006
    Posts
    77
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Open a pop up menu when right click on any cell of the table

    QTableWidgetItem * itemAt ( const QPoint & point ) const


    Qt Code:
    1. void MyTableWidget::contextMenuEvent( QContextMenuEvent * e )
    2. {
    3. QAction *pAddAction = new QAction("Add",this);
    4. connect(pAddAction,SIGNAL(triggered()),this,(slotAdd()));
    5. QAction *pRemoveAction = new QAction("Remove",this);
    6. connect(pRemoveAction ,SIGNAL(triggered()),this,(slotRemove()));
    7. QAction *pUpdateAction = new QAction("Update",this);
    8. connect(pUpdateAction ,SIGNAL(triggered()),this,(slotUpdate()));
    9. //
    10. QMenu *pContextMenu = new QMenu( this);
    11. pContextMenu->addAction(pAddAction);
    12. pContextMenu->addAction(pRemoveAction );
    13. pContextMenu->addAction(pUpdateAction );
    14. //
    15. pContextMenu->exec( e->globalPos() );
    16.  
    17. delete pContextMenu;
    18. pContextMenu = NULL;
    19. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to kemp for this useful post:

    katta_ashish (9th September 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1

    Default Re: Open a pop up menu when right click on any cell of the table

    Thank you very much for your valuable reply.....My problem is solved now with some of your help and some of mine coding.I am pasting the code also so that it can help others also...

    First create a signal and slot in main program like this

    connect(ui->tableWidget,SIGNAL(customContextMenuRequested(con st QPoint &)),this,SLOT(ProvideContextMenu(const QPoint &)));

    and then just create the slots functions for them like this....

    void MainWindow::ProvideContextMenu(const QPoint &pos)
    {

    item = ui->tableWidget->itemAt(pos);
    // QPoint globalPos = ui->tableWidget->mapToGlobal(pos);
    QAction *pAddAction = new QAction("Add",ui->tableWidget);
    connect(pAddAction,SIGNAL(triggered()),this,SLOT(n ewRow()));
    QAction *pRemoveAction = new QAction("Remove",ui->tableWidget);
    connect(pRemoveAction ,SIGNAL(triggered()),this,SLOT(deleteRow()));
    QAction *pUpdateAction = new QAction("Update",ui->tableWidget);
    //connect(pUpdateAction ,SIGNAL(triggered()),this,SLOT(Update()));
    QMenu *pContextMenu = new QMenu( this);
    pContextMenu->addAction(pAddAction);
    pContextMenu->addAction(pRemoveAction );
    pContextMenu->addAction(pUpdateAction );
    pContextMenu->exec( mapToGlobal(pos) );
    delete pContextMenu;
    pContextMenu = NULL;
    }

    void MainWindow::newRow()
    {
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);
    }

    void MainWindow::deleteRow()
    {
    int row = ui->tableWidget->row(item);
    ui->tableWidget->removeRow(row);
    }


    Do not forget to declare them in .h file or wherever you want....
    Last edited by katta_ashish; 9th September 2010 at 08:23.

Similar Threads

  1. Replies: 0
    Last Post: 29th July 2009, 20:13
  2. QTreeView cell as table
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2009, 10:45
  3. Making Table cell as a combobox?
    By kaushal_gaurav in forum Qt Programming
    Replies: 9
    Last Post: 1st August 2008, 13:04
  4. Replies: 4
    Last Post: 4th February 2008, 07:16
  5. Highlighting the border of cell in Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 09:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.