Seite 1 von 1

[Problem gelöst] Slot wird mehrfach ausgelöst -- warum?

Verfasst: 15. Juni 2010 23:02
von MSchuster
Hy @ all

habe folgendes Problem:
Habe ein Contexmenü in meinem TableView erstellt und mit einem Slot conected.
Wenn ich jetzt den Menüpunkt im Contexmenü klicke erscheint auch wunderbar meine Messagebox.
Wenn ich dann nochmals den Menüpunkt im Contexmenü klicke erscheint die Messagebox 2x hintereinander beim nächsten mal 3x usw. keine Ahnung warum.

Thx für Lösungen

Hier mein Code:
Header

Code: Alles auswählen

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QModelIndex>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);
    bool eventFilter(QObject *object, QEvent *event);
    void contextMenuEvent(QContextMenuEvent *event);
    void customContextMenuRequested(const QPoint &pos);     //für TablePerson Contexmenü

private:
    Ui::MainWindow *ui;
    void PersoncreateActions();
    QAction *showAdresse;

public slots:
    void showAdress();

private slots:
    void zeigeAdresse(const QPoint&);
   // void on_tableView_Person_clicked(QModelIndex index);
    void on_Btn_Kd_Suchen_clicked();
};

#endif // MAINWINDOW_H
cpp

Code: Alles auswählen

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tableView_Person->viewport()->installEventFilter(this);
    ui->tableView_Person->setContextMenuPolicy(Qt::CustomContextMenu);

    PersoncreateActions();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::customContextMenuRequested(const QPoint &pos)
{

}
void MainWindow::zeigeAdresse(const QPoint &pos)
{
    QMenu contextMenu(this);
    contextMenu.addAction(showAdresse);  //Action hinzufügen
    connect(showAdresse, SIGNAL(triggered()),this,SLOT(showAdress()));//Action mit Slot verbinden
    QPoint globalPos = ui->tableView_Person->mapToGlobal(pos);//rechtsklick auf TableView beschränken
    contextMenu.exec(globalPos);
   // QMessageBox::information(0,"Title..","zeige Adresse");
}

void MainWindow::PersoncreateActions()                                            //ContexMenu Person Actions erzeugen
{
    showAdresse = new QAction(tr("Zeige Adresse"),this);
    showAdresse->setStatusTip("Zeigt alle Adressen des Kunden an.");
    showAdresse->setIcon(QIcon(":/icons/icons/door.png"));
    connect(ui->tableView_Person, SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(zeigeAdresse(const QPoint&)));
}

void MainWindow::showAdress()
{
    QMessageBox::information(0,"Adresse","zeige Adresse");
}

Verfasst: 16. Juni 2010 06:29
von Christian81
Wenn Du mehr als einmal die Signals und Slots verbindest wird es auch mehr als einmal ausgeführt.

Verfasst: 16. Juni 2010 10:23
von Himbeer-Toni
Vielleicht klappts so?

Code: Alles auswählen

void MainWindow::PersoncreateActions()                                            //ContexMenu Person Actions erzeugen
{
    showAdresse = new QAction(tr("Zeige Adresse"),this);
    showAdresse->setStatusTip("Zeigt alle Adressen des Kunden an.");
    showAdresse->setIcon(QIcon(":/icons/icons/door.png"));
    disconnect(ui->tableView_Person, SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(zeigeAdresse(const QPoint&)));
connect(ui->tableView_Person, SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(zeigeAdresse(const QPoint&)));
} 

Verfasst: 16. Juni 2010 10:36
von franzf
Wenn man in der Doku mal genau hinschaut, finder man unter Qt::ConnectionType auch Qt::UniqueConnection.
Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6.
Einfach beim connect() mitgeben und eigene "Hacks" werden überflüssig..

Verfasst: 16. Juni 2010 10:59
von TSS
Himbeer-Toni hat geschrieben:Vielleicht klappts so?

Code: Alles auswählen

void MainWindow::PersoncreateActions()                                            //ContexMenu Person Actions erzeugen
{
    showAdresse = new QAction(tr("Zeige Adresse"),this);
    showAdresse->setStatusTip("Zeigt alle Adressen des Kunden an.");
    showAdresse->setIcon(QIcon(":/icons/icons/door.png"));
    disconnect(ui->tableView_Person, SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(zeigeAdresse(const QPoint&)));
connect(ui->tableView_Person, SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(zeigeAdresse(const QPoint&)));
} 
Ich denke das Problem lag eher an folgender Stelle:

Code: Alles auswählen

void MainWindow::zeigeAdresse(const QPoint &pos)
{
    QMenu contextMenu(this);
    contextMenu.addAction(showAdresse);  //Action hinzufügen
    connect(showAdresse, SIGNAL(triggered()),this,SLOT(showAdress()));//Action mit Slot verbinden
    QPoint globalPos = ui->tableView_Person->mapToGlobal(pos);//rechtsklick auf TableView beschränken
    contextMenu.exec(globalPos);
   // QMessageBox::information(0,"Title..","zeige Adresse");
} 
Das connect() aus dieser Funktion einfach in die Funktion PersoncreateActions() verschieben.

Problem gelöst

Verfasst: 16. Juni 2010 21:45
von MSchuster
Thx TSS so funkt es einwandfrei. Verstehe es zwar noch nicht aber Problem ist gelöst :D