Results 1 to 10 of 10

Thread: Context Menu on QTableWidget

  1. #1
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Context Menu on QTableWidget

    hi all,

    i've made a dll to provide some table functions ( excel like ) to my main project. It works fine with linkage and all. The code to make context menu and calling it is also in the dll part, in the constructor.

    My problem is, in my table in the main project, i get context menu at all places, except at the headers. Like in excel on right click on the headers select all rows/columns and giv a context menu there also. My context menu is not coming on the header.

    what can be the problem.
    below is my dll code:

    Qt Code:
    1. //QGrid.cpp
    2.  
    3. #include <QtGui>
    4. #include "QGrid.h"
    5. #include <QMessageBox>
    6.  
    7.  
    8. QGrid::QGrid(int r, int c,QWidget *parent) : QTableWidget(r,c,parent)
    9. {
    10. createActions();
    11. setUpContextMenu();
    12. }
    13.  
    14.  
    15. QGrid::QGrid(QWidget *parent) : QTableWidget(parent)
    16. {
    17. createActions();
    18. setUpContextMenu();
    19. }
    20.  
    21. void QGrid::setUpContextMenu()
    22. {
    23. addAction(copyAct);
    24. addAction(pasteAct);
    25. ...
    26. setContextMenuPolicy(Qt::ActionsContextMenu);
    27. }
    28.  
    29. void QGrid::createActions()
    30. {
    31. copyAct = new QAction(QIcon(":/images/copy.png"), "&Copy", this);
    32. copyAct->setShortcut(tr("Ctrl+C"));
    33. copyAct->setStatusTip(tr("Copy the current selection's contents to the "
    34. "clipboard"));
    35. connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
    36.  
    37. pasteAct = new QAction(QIcon(":/images/paste.png"), "&Paste", this);
    38. pasteAct->setShortcut(tr("Ctrl+V"));
    39. pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
    40. "selection"));
    41. connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
    42.  
    43. ....
    44. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //QGrid.h
    2.  
    3. #ifndef QGRID_H
    4. #define QGRID_H
    5. #include <QtCore/qglobal.h>
    6. #include <QtGui/QDialog>
    7. #include <QAction>
    8. #include <QTableWidget>
    9.  
    10. #ifdef BUILD_QGRID
    11. # ifdef Q_CC_MSVC
    12. # define QGRID_EXPORT Q_DECL_EXPORT
    13. # else
    14. # define QGRID_EXPORT Q_DECL_IMPORT
    15. # endif
    16. #else
    17. # define QGRID_EXPORT
    18. #endif
    19.  
    20. class QGRID_EXPORT QGrid : public QTableWidget
    21. {
    22. Q_OBJECT
    23. public:
    24. QGrid(int c,int r,QWidget *parent = 0);
    25. QGrid(QWidget *parent = 0);
    26. QClipboard *clip;
    27. QTableWidget *table;
    28.  
    29. private:
    30. QAction *copyAct;
    31. QAction *pasteAct;
    32. ....
    33. public slots:
    34.  
    35. void copy();
    36. void paste();
    37. ....
    38. public:
    39. void setUpContextMenu();
    40. void createActions();
    41. };
    42. #endif
    To copy to clipboard, switch view to plain text mode 

    please help ....
    Do what u r afraid to do, and the death of fear is sure.

  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: Context Menu on QTableWidget

    Put the context menu also for the headers:
    QHeaderView* QTableView::horizontalHeader() const
    QHeaderView* QTableView::verticalHeader() const
    J-P Nurmi

  3. #3
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Context Menu on QTableWidget

    hi jpn,
    thanx 4 replying but can u explain a bit more ... u c i m a novice ... i m also already trying to solve this the same way u say. but how shall i call contextmenu functions on the header . please also hav a look at the code in 1st post.
    i hav defined 2 functions there, how shall i call those functions....
    Do what u r afraid to do, and the death of fear is sure.

  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: Context Menu on QTableWidget

    Ok, sorry for not being clear and understandable enough.
    I meant something like this:
    Qt Code:
    1. void QGrid::setUpContextMenu()
    2. {
    3. ...
    4. horizontalHeader()->addAction(someAct);
    5. horizontalHeader()->setContextMenuPolicy(Qt::ActionsContextMenu);
    6. // and the same goes for verticalHeader..
    7. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    ankurjain (8th April 2006)

  6. #5
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Context Menu on QTableWidget

    thank you jpn,

    my problem is solved ( except that my program crashes on clicking any items of context menu, i will solve that ).

    I am also having one more problem, i had assigned shortcutkeys to these actions via

    Qt Code:
    1. cutAct->setShortcut(tr("Ctrl+X"));
    To copy to clipboard, switch view to plain text mode 

    But this doesn't seem to work. is there some mistake in that ?
    Do what u r afraid to do, and the death of fear is sure.

  7. #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: Context Menu on QTableWidget

    Hmm, I don't see anything wrong with that..
    Have you tried to change the shortcut context for that action? Try setting it to Qt::ApplicationShortcut.
    J-P Nurmi

  8. #7
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Shortcuts not working

    hi jpn,

    i tried what u told, but still no use. FYI i am also using some delegates in the table. can it cause some problems ?
    Do what u r afraid to do, and the death of fear is sure.

  9. #8
    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: Context Menu on QTableWidget

    Hmm, if you are using the actions context menu policy I'm not really sure what's the problem.
    As the action is created somewhere in the constructor or so, that it exists during the lifetime of the widget, the action should be triggered as well.
    Maybe the shortcut event goes to a wrong widget or something.

    Anyway, how about this way, does it work?
    Qt Code:
    1. QShortcut* copyShortcut = new QShortcut(QKeySequence("Ctrl+C"), this);
    2. connect(copyShortcut, SIGNAL(activated()), copyAct, SLOT(trigger()));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. #9
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Shortcuts on TableWidget headers

    Still not working
    Do what u r afraid to do, and the death of fear is sure.

  11. #10
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Context Menu on QTableWidget

    Hi ankur,

    Did your concept worked? Could you please let me know some coding part regarding the copying into clipboard and pasting back from clipboard. Even my requirement is same. It would be great if yo can share your dll/code.

    Thanks in advance,
    Nikhil

Similar Threads

  1. Shortcut key for context menu
    By darshan.hardas in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 21:32
  2. QItemDelegate Context Menu
    By aekilic in forum Qt Programming
    Replies: 16
    Last Post: 3rd December 2008, 10:29
  3. context menu problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 14:18
  4. Context menu works slowly on QTableWidget
    By THRESHE in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2008, 20:54
  5. Replies: 4
    Last Post: 25th June 2007, 21:40

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.