Domanda

I am trying to call some function (or slot) when the mouse leaves the space of my QListView (tableView). Normally, you could use the leaveEvent() function. So for example I could write

void MainWindow::leaveEvent(QEvent * event){
    qApp->quit();
}

This works as intended. When the mouse leaves the MainWindow widget, the application quits. However, what if I wanted to quit the application when the mouse leaves my QListView object which is INSIDE of my MainWindow widget?

How do I reimplement a function for this QListView when it was created within Qt Creator's form designer?

Here is what I have (unsuccessfully) tried:

void Ui::tableView::leaveEvent(){
    qApp->quit();
}

And below, I have tried using leaveEvent() as a signal, and it says leaveEvent is undefined (can you even use events as SIGNALs?)

connect(ui->tableView, SIGNAL(leaveEvent(QEvent *event)), this, SLOT(testSlot()));

Basically, I am trying to call some function when the mouse leaves my tableView which was created with Qt Creator's form designer. The QListView class seems to have a mouseEntered() SIGNAL, but not mouseLeave() SIGNAL.

È stato utile?

Soluzione

Subclass QListView and reimplement the leaveEvent (example):

class MyListView : public QListView
{
     Q_OBJECT

    void MyListView::leaveEvent(QEvent *e){
        QListView::leaveEvent(e);
        anyOtherAction();
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top