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>
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.