Results 1 to 4 of 4

Thread: CustomContextMenu with arguments

  1. #1
    Join Date
    Jun 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation CustomContextMenu with arguments

    Hello,

    Goal: Display a context menu under a clicked column header in QTableView(with a horizontal QViewHeader being the 'parent' of the actions), with one of the actions being "Hide Column" which would hide the selected/clicked on column.

    Issue:I know how to have a context menu appear with regular actions that trigger functions with no arguments but how do I pass an argument(a column number specifically) to this slot function?

    Approach:My approach thus far has been to set the horziontal header's setContextMenuPolicy as Qt::CustomContextMenu, and then trigger a function like showContextMenu(const QPoint&).

    showContextMenu(..)
    QAction* hide = new QAction(tr("Hide Column"),this);
    QMenu* popUp = new QMenu("Column Menu", this);
    popUp->addAction(hide);
    popUp->popup(mapToGlobal(p));

    My goal is determine if I can get something like connect(hide, SIGNAL(triggered()), this, SLOT(hideColumn(int))); to work but my attempts have been unsuccessful. How do I do this or is there a better way to do this?

    I hope I've made my issue clear.

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: CustomContextMenu with arguments

    take a look at QSignalMapper

  3. #3
    Join Date
    Jun 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: CustomContextMenu with arguments

    If I have only one menu item "Hide Column" under the context menu, how would I know "signalMapper->setMapping(hideAction, ?...?);" what to pass in the 2nd argument of that function? There is only one hide action for all the columns, now the issue is to determine under which column it was pressed. The custom context menu would pass a const QPoint& which makes this slightly harder but I still don't see how its done.

    What do I do ?

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: CustomContextMenu with arguments

    before you show the popup find out what column was clicked. then connect your hide action to QSignalMapper map slot and set mapping of your action to the column id. then connect QSignalMapper's mapped signal to your slot with one argument of type int. you can find QSignalMapper usage sample in the docs.

    or maybe easier would be to call setData passing the obtained column id instead of using QSignalMapper. also you may save the clicked column id to some member variable and use it in your slot.

Similar Threads

  1. slots with arguments
    By marco.stanzani in forum Newbie
    Replies: 5
    Last Post: 24th March 2011, 11:05
  2. QApplication Arguments
    By ToddAtWSU in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2011, 16:34
  3. how to use arguments in Qt?
    By mr_kazoodle in forum Newbie
    Replies: 5
    Last Post: 26th January 2011, 20:04
  4. QProcess Arguments
    By doggrant in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2010, 16:45
  5. QVector arguments
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2007, 10:47

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.