Custom push button try
-
Hi
I am trying to subclass a QPushButton to add some functionality to the custom push button. I need this push button to detect key board button 'A' pressed and change a label within my MainWindow.
I created
mypushbuttonclass as follows:
.h// mypushbutton.h #ifndef MYPUSHBUTTON_H #define MYPUSHBUTTON_H #include <QPushButton> #include <QKeyEvent> class MyPushButton : public QPushButton { Q_OBJECT public: explicit MyPushButton(QWidget *parent = nullptr); signals: void letterAPressed(); protected: void keyPressEvent(QKeyEvent *event) override; }; #endif // MYPUSHBUTTON_Hand
.cpp// mypushbutton.cpp #include "mypushbutton.h" MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent) { setFocusPolicy(Qt::ClickFocus); } void MyPushButton::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_A) { emit letterAPressed(); } else { QPushButton::keyPressEvent(event); } }and in my
mainwindowI set a widget in the form class but I have not manually promoted it tomypushbutton.hbut instead I did this:#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QObject> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); button = new MyPushButton(ui->widget); connect(button, &MyPushButton::letterAPressed, this, &MainWindow::updateLabelText); ui->widget->setFocus(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::updateLabelText() { ui->label->setText("letter A clicked!"); }I expect if I click on the
ui->widgettheui->labelchange. But I'm not getting anything. Any idea what's wrong here? -
Hi
I am trying to subclass a QPushButton to add some functionality to the custom push button. I need this push button to detect key board button 'A' pressed and change a label within my MainWindow.
I created
mypushbuttonclass as follows:
.h// mypushbutton.h #ifndef MYPUSHBUTTON_H #define MYPUSHBUTTON_H #include <QPushButton> #include <QKeyEvent> class MyPushButton : public QPushButton { Q_OBJECT public: explicit MyPushButton(QWidget *parent = nullptr); signals: void letterAPressed(); protected: void keyPressEvent(QKeyEvent *event) override; }; #endif // MYPUSHBUTTON_Hand
.cpp// mypushbutton.cpp #include "mypushbutton.h" MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent) { setFocusPolicy(Qt::ClickFocus); } void MyPushButton::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_A) { emit letterAPressed(); } else { QPushButton::keyPressEvent(event); } }and in my
mainwindowI set a widget in the form class but I have not manually promoted it tomypushbutton.hbut instead I did this:#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QObject> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); button = new MyPushButton(ui->widget); connect(button, &MyPushButton::letterAPressed, this, &MainWindow::updateLabelText); ui->widget->setFocus(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::updateLabelText() { ui->label->setText("letter A clicked!"); }I expect if I click on the
ui->widgettheui->labelchange. But I'm not getting anything. Any idea what's wrong here? -
V viniltc has marked this topic as solved on