Results 1 to 6 of 6

Thread: QTableWidget right click menu

  1. #1
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableWidget right click menu

    Hi.
    Im starting to use Qt, and after installation/building/starting_project probelm i have a problem for which i can't find solution searching the forums..

    I need to add a (right mouse button click) menu to QTableWidget, but i can't get it working.
    Qt Code:
    1. frmSettings::frmSettings(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. context= new QMenu(this);
    7. context->addAction("Add");
    8. context->addAction("Delete");
    9.  
    10. connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(QPoint* position)), SLOT(frmSettings::ProvideContexMenu(position)));
    11. }
    12. void frmSettings::ProvideContexMenu(const QPoint* position)
    13. {
    14. context->exec(*position);
    15. }
    To copy to clipboard, switch view to plain text mode 

    tableOptimizationVariables is QTableWidget

    Thanx

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget right click menu

    You cannot put variable names but just types inside SIGNAL() and SLOT() macros and there is no such signal as QWidget::customContextMenuRequested(QPoint*). It's a reference, not a pointer.

    So it should be:
    Qt Code:
    1. connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));
    To copy to clipboard, switch view to plain text mode 

    Also, did you switch the context menu policy to Qt::CustomContextMenu?
    J-P Nurmi

  3. #3
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget right click menu

    thank you, but its not working.
    Yes ContextMenuPolicy was set to CustomContextMenu in .ui file. I treid to do this in constructor but without success:
    Qt Code:
    1. frmSettings::frmSettings(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. context= new QMenu(this);
    7. context->addAction("Add");
    8. context->addAction("Delete");
    9. ui.tableOptimizationVariables->setContextMenuPolicy(Qt::CustomContextMenu);
    10. // connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(QPoint* position)),frmSettings, SLOT(ProvideContexMenu(position)));
    11. connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));
    12. }
    13. void frmSettings::ProvideContexMenu(const QPoint* position)
    14. {
    15. context->exec(*position);
    16. }
    To copy to clipboard, switch view to plain text mode 

    Can somebody give me complete code (it shoud be couple of lines) for calling context menu? Is it diferent for different controls? Maybe some QTableWidget-specific stuff must be done?

    //LATER ADDED TEXT

    I tried to link contect menu to QLineEdit control (to see if default copy/cut/paste menu is invoked):
    Qt Code:
    1. ui.tableOptimizationVariables->setContextMenuPolicy(Qt::CustomContextMenu);
    2. ui.txtName->setContextMenuPolicy(Qt::CustomContextMenu);
    3. connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));
    4. connect(ui.txtName, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));
    To copy to clipboard, switch view to plain text mode 

    So i lost "Copy/Cut/Paste/|/Select All" menu, which mean that context menu is set to custom? But when i debugged I noticed that slot void frmSettings::ProvideContexMenu(const QPoint* position) is never entered. What am I doing wrong?
    Last edited by stefan; 23rd June 2008 at 19:34.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget right click menu

    You forgot to adjust frmSettings::ProvideContexMenu(). It still takes a pointer.

    The easiest way to construct a context menu is to use Qt::ActionsContextMenu. All it takes is to add actions via QWidget::addAction() and the menu is constructed automatically for you:
    Qt Code:
    1. setContextMenuPolicy(Qt::ActionsContextMenu);
    2. QAction* fooAction = new new QAction("foo");
    3. QAction* barAction = new new QAction("bar");
    4. connect(fooAction, SIGNAL(triggered()), this, SLOT(doSomethingFoo()));
    5. connect(barAction, SIGNAL(triggered()), this, SLOT(doSomethingBar()));
    6. addAction(fooAction);
    7. addAction(barAction);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget right click menu

    Quote Originally Posted by jpn View Post
    You forgot to adjust frmSettings::ProvideContexMenu(). It still takes a pointer.
    YES! thank you. I'm used to .NET warnings for every litle thing.. so im kinda strugling with QT..
    Replies on my post where fast and exact nice forum guys!

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget right click menu

    Quote Originally Posted by stefan View Post
    YES! thank you. I'm used to .NET warnings for every litle thing.. so im kinda strugling with QT..
    Signal slot connections are established at run time, that's why there is no error/warning at compilation time. Notice that QObject::connect() outputs a detailed warning in the debug output in case establishing the connection fails.

    Replies on my post where fast and exact nice forum guys!
    Thanks, you're welcome.
    J-P Nurmi

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  2. Replies: 2
    Last Post: 2nd April 2008, 17:28
  3. Context menu works slowly on QTableWidget
    By THRESHE in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2008, 19:54
  4. Right click menu
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:22
  5. QTableWidget click in empty space results in error?
    By Giel Peters in forum Qt Programming
    Replies: 4
    Last Post: 21st January 2006, 00:07

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.