Reputation: 667
Okay, so the code is very short, seen below:
MainWindow::MainWindow() :
QMainWindow(),
ui(new Ui::MainWindow)
{
//ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: mouseDoubleClickEvent ( QMouseEvent * event )
{
cout << "being double clicked" << endl;
}
void MainWindow:: mousePressEvent(QMouseEvent *){
cout << "being clicked once " << endl;
}
the setupUi is being slashed, in this case the output is fine, when I double click, it displays the "being double clicked " message.
However, !!! when I when I setup the ui, it stops working!! The message will no longer be displayed... !!!
Can someone please explain why! Thank you very much, this is urgent!!!!!!!!!
Upvotes: 1
Views: 3367
Reputation: 41
It may be strange for some people, but I think it is worth answering unanswered problems, because I was looking for a solution to my problem and solved it myself. And questions left unanswered litter the Internet.
Your solution appeared in the search engine because I have exactly the same problem, and actually I already solved it.
Honestly, reloading the virtual function doesn't work for me either. Maybe someone with more knowledge could comment on why the following function does not want to work?
virtual void mouseDoubleClickEvent(QMouseEvent *event)
<- why doesn't this feature work?
only reloading the virtual function worked
virtual bool event(QEvent *event)
file mycalendarwidget.h
#define MYCALENDARWIDGET_H
#include <QCalendarWidget>
#include <QObject>
#include <QMouseEvent>
class MyCalendarWidget : public QCalendarWidget
{
Q_OBJECT
public:
MyCalendarWidget(QWidget *parent=nullptr);
// QObject interface
public:
virtual bool event(QEvent *event) override;
signals:
void myClicked(bool checked=false);
};
#endif // MYCALENDARWIDGET_H
file mycalendarwidget.cpp
MyCalendarWidget::MyCalendarWidget(QWidget *parent) : QCalendarWidget(parent)
{
}
bool MyCalendarWidget::event(QEvent *event)
{
if(event->type()==QEvent::MouseButtonRelease){
emit myClicked(true);
return true;
}
return false;
}
somewhere in mainwindow.cpp
myCalendar = new MyCalendarWidget(this);
myCalendar->setGridVisible(false);
QObject::connect(myCalendar, &MyCalendarWidget::myClicked, this, [](){qInfo()<< "slot two mouse clicked";});
screenshot of the program running
Upvotes: 0
Reputation: 12600
The mouse click event will always be sent to the object you click on. This means if you have e.g. a button spanned over your whole mainwindow and you double click that button, QPushButton::mouseDoubleClickEvent()
will be called rather than the event in your main window.
If you don't want to implement these functions in subclasses of your child widgets, or your child widgets don't offer something like a clicked()
signal, you can use the Event Filter technique:
http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#installEventFilter
Upvotes: 2