Results 1 to 7 of 7

Thread: problem with Context Menu in QTableWidget

  1. #1
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation problem with Context Menu in QTableWidget

    Hello. I am trying to make a right click context menu appear over an item inside a QTableWidget.
    I tried several things, but somehow it is not working or, not working properly.
    Qt Code:
    1. void fileHandler::ctxMenu(const QPoint &pos)//started
    2. {
    3. QMenu *menu = new QMenu;
    4. menu->addAction(tr("Test Item"), this, SLOT(test_slot()));
    5. menu->addSeparator();
    6. menu->addAction(tr("Test Item"), this, SLOT(test_slot()));
    7. menu->exec(m_ui->tableWidget->mapToGlobal(pos));
    8. }
    To copy to clipboard, switch view to plain text mode 
    In the constructor i am doing this:
    Qt Code:
    1. connect(m_ui->tableWidget, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &)));
    To copy to clipboard, switch view to plain text mode 
    The problem with this thing is that when i right click on an item the popup menu appears not next to the x,y coordinates of where i clicked but somewhat x+50 px or so(only for the example i have). i also want to have a context menu only on items. I couldn't find anything to work like i want.
    I want to add operations with fast navigation using the context menu for selected item or items. Thank you.

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with Context Menu in QTableWidget

    Did you try this? :

    Qt Code:
    1. menu->exec(QCursor::pos());
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to victor.fernandez for this useful post:

    waynew (10th January 2010)

  4. #3
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with Context Menu in QTableWidget

    genius... it works like a charm ! thank you.
    now for the other part.. how do you think i should check and allow the context menu to appear only when i click on a TableWidgetItem?

  5. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with Context Menu in QTableWidget

    You may get the index at the cursor position and check whether it's valid:

    Qt Code:
    1. void fileHandler::ctxMenu(const QPoint &pos)//started
    2. {
    3. QModelIndex index = m_ui->tableWidget->indexAt(pos);
    4. if(!index.isValid())
    5. return;
    6.  
    7. QMenu *menu = new QMenu;
    To copy to clipboard, switch view to plain text mode 

    Another way, getting the QTableWidgetItem:

    Qt Code:
    1. void fileHandler::ctxMenu(const QPoint &pos)//started
    2. {
    3. QTableWidgetItem *item = m_ui->tableWidget->itemAt(pos);
    4. if(!item)
    5. return;
    6.  
    7. QMenu *menu = new QMenu;
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to victor.fernandez for this useful post:

    andreime (3rd August 2009)

  7. #5
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with Context Menu in QTableWidget

    The documentation for customContextMenuRequested states:
    Normally this is in widget coordinates. The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport() .
    Note that QTableWidget indirectly derives from QAbstractScrollArea. So you have to use
    Qt Code:
    1. menu->exec(m_ui->tableWidget->viewport()->mapToGlobal(pos));
    To copy to clipboard, switch view to plain text mode 
    This is a little neater than using the current mouse position as the cursor may have moved on by the time you receive the event.

  8. The following user says thank you to numbat for this useful post:

    mattc (7th September 2010)

  9. #6
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with Context Menu in QTableWidget

    thank you alot for the help

  10. #7
    Join Date
    Jul 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: problem with Context Menu in QTableWidget

    Many Thanks numbat !
    Only menu->exec(m_ui->tableWidget->viewport()->mapToGlobal(pos)); works correctly.

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 10:52
  2. Replies: 3
    Last Post: 5th March 2009, 08:27
  3. Menu problem using Designer
    By JimBrown in forum Qt Tools
    Replies: 1
    Last Post: 19th February 2007, 22:47
  4. context menu advice
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2006, 08:55
  5. Problem inserting in QTableWidget
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 01:10

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.