Results 1 to 10 of 10

Thread: context menu not displaying

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default context menu not displaying

    I created a table inside a function and connect as below...

    filesTable= new QTableWidget(this);

    connect(filesTable,SIGNAL(customContextMenuRequest ed(QPoint)),this,SLOT(ProvideContextMenu(QPoint))) ;

    but, it is not generating signal, i am unable too see any context menu over there...
    Last edited by aurora; 16th February 2012 at 08:06.

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: context menu not displaying

    customContextMenuRequested() signal is only emitted with ContextMenuPolicy set to Qt::CustomContextMenu , so you

    need to add this:

    filesTable->setContextMenuPolicy(Qt::CustomContextMenu);

    *and why did you leave a whitespace in "SIGNAL(customContextMenuRequest_ed(QPoint)

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: context menu not displaying

    Thank u so much Alex...
    But problem is that context menu is not displaying where mouse pointer is...It is displaying somewhere else..
    What changes i need to do?
    my code as shown below...

    Qt Code:
    1. void MainWindow::ProvideContextMenu(const QPoint &pos)
    2. {
    3. QTableWidgetItem *item=filesTable->itemAt(pos);
    4.  
    5. QString cell=item->text();
    6. cout<<"EVENT GENERATED..."<<cell.toStdString()<<endl;
    7.  
    8. QAction *pAddAction= new QAction("add",filesTable);
    9. QAction *pSplitAction= new QAction("SPLIT",filesTable);
    10. QMenu *pContextMenu = new QMenu( this);
    11. pContextMenu->addAction(pAddAction);
    12. pContextMenu->addAction(pSplitAction);
    13. pContextMenu->exec(mapToGlobal(pos));
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: context menu not displaying

    Coordinate transformations might be tricky...Here is a code snippet i used long time ago...and the thing is that i'm not sure if i figured it out completely back then...cuz i had the same problem.

    Qt Code:
    1. //..
    2.  
    3. view = new QTableView;
    4.  
    5. // ...
    6.  
    7. void Widget::customContextMenuRequested(const QPoint &point)
    8. {
    9.  
    10. QModelIndex index = view->indexAt(point);
    11.  
    12. if( index.isValid() )
    13. {
    14.  
    15. QPoint viewPortPosition = view->pos();
    16.  
    17. int headerOffset = view->horizontalHeader()->sectionSize(0);
    18.  
    19. int adjustedHeight = viewPortPos.y() + headerOffset;
    20.  
    21. viewPortPos.setY(adjustedHeight);
    22.  
    23. QMenu menu;
    24.  
    25. // ...
    26.  
    27. menu.exec( mapToGlobal(QPoint( viewPortPos + point ) ) );
    28.  
    29. }
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    Even thought i've used QTableView , i'm sure it's gonna give you an idea...If you find that there is something wrong with my implementation (i'm sure there is) , i'd be glad to hear the right way to do it!^^

  5. #5
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: context menu not displaying

    hmmm...not working...

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: context menu not displaying

    Quote Originally Posted by AlexSudnik View Post
    *and why did you leave a whitespace in "SIGNAL(customContextMenuRequest_ed(QPoint)
    The space is inserted by the forum to break up long "words" when the poster cannot be bothered to use [code][/code] tags around code.

  7. #7
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: context menu not displaying

    You still have it appearing in the wrong space ? If so...you just want to use QTableWidget's context menu , right ?

    P.S : ChrisW67 - thanks^^ , i see now^^

  8. #8
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: context menu not displaying

    ya right now i'm using QTable widget but thinking to change it table view.....

  9. #9
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: context menu not displaying

    Hi !
    I use :
    Qt Code:
    1. menu.exec(QCursor::pos())
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: context menu not displaying

    Hmm , try this:

    Qt Code:
    1. void Widget::ProvideContextMenu( const QPoint& position )
    2. {
    3.  
    4. QModelIndex index = indexAt(position);
    5.  
    6. if( index.isValid( ) ) // not neccessary , if you're about to perform item-independent actions
    7. {
    8.  
    9. QMenu menu;
    10.  
    11.  
    12. QAction menuAction(this);
    13.  
    14. menuAction.setText("Demonstrational Text");
    15.  
    16.  
    17. menu.addAction(&menuAction);
    18.  
    19. menu.exec( mapToGlobal( position ) );
    20.  
    21.  
    22. }
    23.  
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    Since initial point coordinates are given in the widget's local space (i mean , not in QWidget * QAbstractScrollArea::viewport () coordinates ) , we have to display it in the same space , using mapToGlobal function.

Similar Threads

  1. Replies: 11
    Last Post: 14th December 2010, 14:35
  2. Replies: 2
    Last Post: 20th September 2009, 02:52
  3. Replies: 1
    Last Post: 28th January 2009, 09:21
  4. Replies: 6
    Last Post: 3rd September 2008, 14:27
  5. Replies: 5
    Last Post: 5th November 2007, 10:13

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
  •  
Qt is a trademark of The Qt Company.