3

I want to be able to double click a QPushbutton instead of a single click.

What I tried:

connect(pb, SIGNAL(doubleClicked()), this, SLOT(comBtnPressed()));

Error says "QObject::connect: No such signal QPushButton::doubleClicked()"

I chose QPushButton initially, but for my purpose, you can suggest change to other object if it can make a doubleclick event. Not necessarily be a push button.

Thank you Masters of Qt and C++.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • `QPushButton` doesn't have a signal for `doubleClicked` (hence the error). In fact I don't think anything in QT has double-click handling, as it seems off (I can't think of an application that responds to double clicking, except operating systems). You could look towards [`QWidget::mouseDoubleClickEvent`](http://doc.qt.io/qt-5/qwidget.html#mouseDoubleClickEvent), but I suspect you might just need a timer in your `clicked()`, or use `pressed()` and `released()` to capture a double click. Not an answer as this is pure guesswork. – Tas Nov 20 '17 at 01:05
  • Do you know any other object that can support doubleclick? Image maybe or other? I dont mind replacing the button with other thing as long as it can doubleclick. – GeneCode Nov 20 '17 at 01:07
  • visually you want it to look like a button or any other widget? – eyllanesc Nov 20 '17 at 01:07
  • visually, it is a small rect (20x20) ish. i dont mind other object as long as it can be around this size. – GeneCode Nov 20 '17 at 01:09

1 Answers1

8

A simple solution is to create our own widget so we overwrite the mouseDoubleClickEvent method, and you could overwrite paintEvent to draw the widget:

#ifndef DOUBLECLICKEDWIDGET_H
#define DOUBLECLICKEDWIDGET_H

#include <QWidget>
#include <QPainter>

class DoubleClickedWidget : public QWidget
{
    Q_OBJECT
public:
    explicit DoubleClickedWidget(QWidget *parent = nullptr):QWidget(parent){
        setFixedSize(20, 20);
    }

signals:
    void doubleClicked();
protected:
    void mouseDoubleClickEvent(QMouseEvent *){
        emit doubleClicked();
    }
    void paintEvent(QPaintEvent *){
        QPainter painter(this);
        painter.fillRect(rect(), Qt::green);
    }
};

#endif // DOUBLECLICKEDWIDGET_H

If you want to use it with Qt Designer you can promote as shown in the following link.

and then connect:

//new style
connect(ui->widget, &DoubleClickedWidget::doubleClicked, this, &MainWindow::onDoubleClicked);
//old style
connect(ui->widget, SIGNAL(doubleClicked), this, SLOT(onDoubleClicked));

In the following link there is an example.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241