Results 1 to 2 of 2

Thread: QtableWidget custom menu - information from cell

  1. #1
    Join Date
    May 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtableWidget custom menu - information from cell

    so I made connection to create a popup menu

    this->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(provideContexMenu(const QPoint&)));

    and the slot to print the menu:

    void ListView:rovideContexMenu(const QPoint &position) {

    QMenu menu(this);

    QAction* open = new QAction("Open", this);
    connect(open, SIGNAL(triggered()), this, SLOT(open()));

    menu.addAction(open);
    menu.exec(QCursor:os());

    }

    void ListView:pen() {
    // action here depends on info
    }

    how to actually have the information from the cell or the row ??? if the provideContextMenu passing just the const QPoint &position ?

    Please advise, super thank you


    Added after 1 32 minutes:


    I am trying to get the item using itemAt but the app crashes when I right click on different column then I want to.

    when I click on column 1 working fine, anything else the app crashes.

    Qt Code:
    1. void ListView::provideContexMenu(const QPoint &position) {
    2. selectedItem = this->itemAt(position.x(), position.y());
    3.  
    4. if (selectedItem->column() == 1) {
    5.  
    6. menu->addAction(shareAct);
    7. menu->addAction(openAct);
    8. menu->addAction(viewAct);
    9.  
    10. menu->exec(QCursor::pos());
    11. }
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    what I do wrong here ? is there another way to handle right click on specific column and receive the item info ??

    thanks for help

    It actually crashes on column 0 but on column 1 and 2 still prints the menu
    Last edited by migel; 1st June 2011 at 13:40.

  2. #2
    Join Date
    May 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtableWidget custom menu - information from cell

    Solved:

    the problem that the app crashed on column 0 was that I set
    Qt Code:
    1. this->setCellWidget(row, col, status);
    To copy to clipboard, switch view to plain text mode 
    so there wasn't any item there

    I just put an empty item and it worked

    Qt Code:
    1. this->setItem(row, col, statusItem);
    2. this->setCellWidget(row, col, status);
    To copy to clipboard, switch view to plain text mode 

    The reason that it shows menu on column 2 was that I simply printed it anyway, I just missed that.

    Qt Code:
    1. void ListView::provideContexMenu(const QPoint &position) {
    2. selectedItem = this->itemAt(position.x(), position.y());
    3.  
    4. if (selectedItem->column() == 1) {
    5.  
    6. menu->addAction(shareAct);
    7. menu->addAction(openAct);
    8. menu->addAction(viewAct);
    9.  
    10. menu->exec(QCursor::pos());
    11. }
    12. menu->exec(QCursor::pos()); // HERE , STUPID ...
    13. }
    To copy to clipboard, switch view to plain text mode 


    also this works either
    Qt Code:
    1. selectedItem = this->itemAt(position);
    To copy to clipboard, switch view to plain text mode 
    instead
    Qt Code:
    1. selectedItem = this->itemAt(position.x(), position.y());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableWidget cell clicked
    By Archa4 in forum Newbie
    Replies: 5
    Last Post: 6th April 2011, 13:08
  2. qtablewidget cell's x() , y()
    By BalaQT in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2010, 13:23
  3. Open a pop up menu when right click on any cell of the table
    By katta_ashish in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2010, 08:06
  4. Replies: 1
    Last Post: 3rd August 2010, 06:59
  5. Replies: 1
    Last Post: 7th December 2009, 19:56

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.