Написал свой виджет, унаследованный от QWidget. В классе определил лишь переопределённую виртуальную функцию mousePressEvent, которая печатает отладочную информацию в консоль при нажатии мышкой по виджету. Эта функция работала в классе MainWindow, унаследованного от QMainWindow, но совершенно не работает в моём виджете.
Фон виджета закрасил красным цветом, кликаю по этой красной области, но в консоль ничего не выводится... Почему?
mainwindow.h#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
//protected:
// virtual void mousePressEvent(QMouseEvent *event);
};
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr);
~MyWidget();
protected:
virtual void mousePressEvent(QMouseEvent *event);
};
#endif // MAINWINDOW_H
mainwindow.cpp#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto widget = new QWidget(this);
widget->setStyleSheet("background-color: red;");
}
MainWindow::~MainWindow()
{
delete ui;
}
//void MainWindow::mousePressEvent(QMouseEvent *event)
//{
// qDebug() << "MainWindow::mousePressEvent";
//}
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
}
MyWidget::~MyWidget()
{
}
void MyWidget::mousePressEvent(QMouseEvent *event)
{
qDebug() << "MyWidget::mousePressEvent";
}
mainwindow.ui<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>791</width>
<height>457</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton_rectangle">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>101</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Прямоугольник</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_triangle">
<property name="geometry">
<rect>
<x>120</x>
<y>10</y>
<width>81</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Треугольник</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_ellipse">
<property name="geometry">
<rect>
<x>210</x>
<y>10</y>
<width>71</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Эллипс</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_connection">
<property name="geometry">
<rect>
<x>310</x>
<y>10</y>
<width>71</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Связь</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_move">
<property name="geometry">
<rect>
<x>410</x>
<y>10</y>
<width>91</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Переместить</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_delete">
<property name="geometry">
<rect>
<x>510</x>
<y>10</y>
<width>71</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Удалить</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_load">
<property name="geometry">
<rect>
<x>630</x>
<y>10</y>
<width>71</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Загрузить</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_save">
<property name="geometry">
<rect>
<x>710</x>
<y>10</y>
<width>71</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Сохранить</string>
</property>
</widget>
<widget class="QGraphicsView" name="graphicsView_canvas">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>771</width>
<height>361</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>791</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
main.cpp#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
TESTDELETE16123.proQT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target