customContextMenuRequested(const QPoint&) want to have customContextMenuRequested(const QPoint&, QTableView)

StackOverflow https://stackoverflow.com/questions/23673415

  •  23-07-2023
  •  | 
  •  

Domanda

I'm stuck on a issue I have and I dont know how to solve it. The issue is that I want to have the QTableView object that I'm working with to be sent to the next function. In this way I don't need to copy the function for all the tables that I have. I can make a generic function and reuse it. But to do that I need to send the QTableView so that I can add the row I want to add... Please see how my code looks. The code is how I would like to have it.

void InputWindow::on_pushButton_ViewMilestone_clicked()
{

    QTableView *viewMilestone = new QTableView;
    viewMilestone->setModel(sourceMilestonesModel);
    viewMilestone->setContextMenuPolicy(Qt::CustomContextMenu);
    viewMilestone->resizeColumnsToContents();
    viewMilestone->setMinimumWidth(getVerticalSizeOfQTableView(viewMilestone));
    viewMilestone->show();

    connect(viewMilestone, SIGNAL(customContextMenuRequested(const QPoint&)),
            this, SLOT(showMenu(const QPoint&)));

}

void InputWindow::showMenu(const QPoint &pos, QTableView *tableView)
{
    QAction *insertRowAction = new QAction(this);
    insertRowAction->setText("Insert new row");

    connect(insertRowAction, SIGNAL(triggered()),
            this, SLOT(addNewRow()));

    QMenu *rightClickMenu = new QMenu(this);
    rightClickMenu->addAction(insertRowAction);
    rightClickMenu->popup(tableView->viewport()->mapToGlobal(pos));

}

void InputWindow::addNewRow(QTableView *qTblView)
{
    QAbstractItemModel *tableModel = qTblView->model();
    int rows = tableModel->rowCount();
    tableModel->insertRow(rows);
}
È stato utile?

Soluzione

It sounds like a QSignalMapper could be helpful here. It's designed to link multiple QObjects to a single slot and to provide context as to what was the originating source. It's generally more reliable than MarioBlueSkies sender() mechanism as you're more explicitly controlling what gets registered against the Mapper.

Something like this...

QSignalMapper *mapper = new QSignalMapper(this);
connect(mapper, SIGNAL(mapped(const QObject&)),
        this, SLOT(clicked(const QObject&)));

void InputWindow::on_pushButton_ViewMilestone_clicked()
{

    QTableView *viewMilestone = new QTableView;
    viewMilestone->setModel(sourceMilestonesModel);
    viewMilestone->setContextMenuPolicy(Qt::CustomContextMenu);
    viewMilestone->resizeColumnsToContents();
    viewMilestone->setMinimumWidth(getVerticalSizeOfQTableView(viewMilestone));
    viewMilestone->show();
    connect(viewMilestone, SIGNAL(customContextMenuRequested(const QPoint&)),                 signalMapper, SLOT(map()));
    signalMapper->setMapping(viewMilestone, viewMilestone);
}

Altri suggerimenti

You could use QObject::sender() to get the object that emitted the signal in showMenu() and then use qobject_cast<QTableView*> to cast the returned object into QTableView. (Don't forget to check the result of the cast before accessing it - qobject_cast returns 0 if it fails)

see http://qt-project.org/doc/qt-4.8/qobject.html#sender

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top