0

I have a QTableView and as I double click I would like to open a QInputDialog.

roslaserscandialog.h

private slots:
    void on_tableView_doubleClicked(const QModelIndex &index);

roslaserscandialog.cpp

 ROSLaserScanDialog::ROSLaserScanDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ROSLaserScanDialog)
{
    ui->setupUi(this);

connect(ui->tableView,SIGNAL( doubleClicked(const QModelIndex&)), this, SLOT ( on_tableView_doubleClicked(const QModelIndex &index)));

}


void ROSLaserScanDialog::on_tableView_doubleClicked(const QModelIndex &index)
{
    QString rosMsg = QInputDialog::getText(this, "ROS Msg Name", "Enter Msg");
    Q_UNUSED(index)
}

I have tried in many different ways to have the QTableView detecting my doubleclick but what I tried so far was not successful:

1) this is one of the trial I tried, that should work as I also followed official documentation:

QObject::connect(ui->tableView, SIGNAL(doubleClicked()), this, SLOT(on_tableView_doubleClicked()));

2) I tried to open it also in the following way:

QObject::connect(ui->tableView, QOverload<int>::of(&QTableView::doubleClicked),
    [=](int index) { on_tableView_doubleClicked(const QModelIndex &index); });

The error I receive from the debugger is the following below but I don't understand how that could happen:

QObject::connect: No such signal QTableView::doubleClicked() in /home/emanuele/Desktop/ultrasound_mapper/src/ERDatabaseSystem/ui/roslaserscandialog.cpp:128 QObject::connect: (sender name: 'tableView') QObject::connect: (receiver name: 'ROSLaserScanDialog')

EDIT 2 : Verifiable example with a .ui form that carries only a QTableView. The QTableView is simply called ui->tableView:

main.cpp

#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

dialog.h

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT
public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private slots:
    void on_tableView_doubleClicked(const QModelIndex &index);
    //void onTableViewDoubleClicked(const QModelIndex &index);
private:
    Ui::Dialog *ui;
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QInputDialog>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    connect(ui->tableView, SIGNAL(doubleClicked), this,
            SLOT(on_tableView_doubleClicked(const QModelIndex &index)));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_tableView_doubleClicked(const QModelIndex &index)
{
    QString ros = QInputDialog::getText(this, "Dialog", "Enter text");
}

dialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>356</width>
    <height>292</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <widget class="QTableView" name="tableView"/>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

ui

In addition in this EDIT 2 I also tried the anonymus route which also did not work:

connect(ui->tableView, &QTableView::doubleClicked, [&]() {
    QString rosMsg = QInputDialog::getText(this, "ROS Msg Name", "Enter Msg");
});

The following also did not work:

    QObject::connect(ui->tableView, &QTableView::doubleClicked(const QModelIndex &index),
        [=](int index) { onTableViewDoubleClicked(index); });

I researched and came across this source, this other source which was useful and as the comment suggested that example was working properly. But didn't work on mine.

Another post suggested the use of this official source in particular using QAbstractItemView::doubleClicked. However I don't think I have to go down that road for this simple example and do not need to complicate the view.

This post solved the problem in the exact way I am trying to solve my problem, but I don't understand why nothing happens on my QTableView.

Please point to the right direction on how to better solve this problem.

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • change `void on_tableView_doubleClicked(const QModelIndex &index);` to `void onTableViewDoubleClicked(const QModelIndex &index);` – eyllanesc Mar 27 '20 at 01:02
  • Don't use the format `on__(...)` since it will generate false problems since by default Qt will connect them if you use the .ui (Qt Designer), probably that warning is not from your connection but to the default connection: Read https://doc.qt.io/qt-5/qobject.html#auto-connection for more information – eyllanesc Mar 27 '20 at 01:05
  • Please check your `connect` calls: [`QTableView::doubleClicked`](https://doc.qt.io/qt-5/qabstractitemview.html#doubleClicked) takes a `QModelIndex` as its parameter. – G.M. Mar 27 '20 at 10:16
  • @eyllanesc, thanks for reading the question. Unfortunately your suggestion does not work. I added to the question a small verifiable example under **EDIT 2** replicating exactly the error. Any idea of what I am missing? – Emanuele Mar 27 '20 at 14:24
  • @G.M., thanks for stopping by and reading the question. I tried also your suggestion but did not work. I added to the question a small verifiable example under **EDIT 2** replicating exactly the error. Hopeful that could be useful to understand what I am missing as nothing happens as I double click on the `QTableView`. – Emanuele Mar 27 '20 at 14:26
  • @Emanuele Did you delete (or comment on) on_tableView_doubleClicked and delete the build folder? – eyllanesc Mar 27 '20 at 14:26
  • @eyllanesc, yes I tried both. I deleted the build and tried again but still nothing happens. – Emanuele Mar 27 '20 at 14:28
  • @Emanuele share the .ui – eyllanesc Mar 27 '20 at 14:28
  • @eyllanesc, I posted the `.ui` – Emanuele Mar 27 '20 at 14:31
  • The doubleClicked signal of the QAbstractItemView are only emitted if you double-click the items but your view is empty so it will never be emitted. In this case the solution is to use eventfilter to monitor the events of the QTableView, and since QTableView is a QAbstractScrollArea then you should do the following: `bool eventFilter(QObject *watched, QEvent *event);` in .h, `ui->tableView->viewport()->installEventFilter(this);` in constructor – eyllanesc Mar 27 '20 at 14:42
  • and `bool Dialog::eventFilter(QObject *watched, QEvent *event) { if(watched == ui->tableView->viewport() && event->type() == QEvent::MouseButtonDblClick){ QString ros = QInputDialog::getText(this, "Dialog", "Enter text"); qDebug() << ros; } return QDialog::eventFilter(watched, event); }` in .cpp – eyllanesc Mar 27 '20 at 14:43
  • PLEASE read the docs: *void QAbstractItemView::doubleClicked(const QModelIndex &index) This signal is emitted when a mouse button is double-clicked. The item the mouse was double-clicked on is specified by index. **The signal is only emitted when the index is valid**.*, An empty space does not have a valid QModelIndex – eyllanesc Mar 27 '20 at 14:45
  • @eyllanesc, ok I understand, thanks for pointing to the notation and for your advise. It is working and I am reading the documentation in great detail now. I appreciate your time in pointing to the right direction! – Emanuele Mar 27 '20 at 15:19

0 Answers0