Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Same function for different widget

Same function for different widget

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 632 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Imhotep
    wrote on last edited by
    #1

    I have two different QTableWidget, MyTableWidgetOne and MyTableWidgetTwo, and I want connect the event that drops the context menu to each of them:

    @
    void MainWindow::ProvideContextMenu(const QPoint& pos) // this is a slot
    {
    QMenu *menu = new QMenu;
    menu->addAction(QString("Test Item"), this, SLOT(test_slot()));
    menu->exec(MyTableWidgetOne->mapToGlobal(pos));
    }
    @

    Now, there is a way to not rewrite the function twice, once for each of the two tables?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      There are couple of ways of course.
      One of them is to make the QTableWidget pointer a parameter of the slot. You can then use sind or lambda to connect to appropriate widget.

      Another option, if the signal is emitted by the table widgets, is to use sender() method inside your slot. It will point to the sending object. Cast is to QTableWidget and there you go.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved