Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom push button try

Custom push button try

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 176 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by
    #1

    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 mypushbutton class 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_H
    
    

    and .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 mainwindow I set a widget in the form class but I have not manually promoted it to mypushbutton.h but 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->widget the ui->label change. But I'm not getting anything. Any idea what's wrong here?

    JonBJ 1 Reply Last reply
    0
    • V viniltc

      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 mypushbutton class 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_H
      
      

      and .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 mainwindow I set a widget in the form class but I have not manually promoted it to mypushbutton.h but 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->widget the ui->label change. But I'm not getting anything. Any idea what's wrong here?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @viniltc

          if (event->key() == Qt::Key_A) {
              emit letterAPressed();
      

      So is this hit?

      V 1 Reply Last reply
      2
      • JonBJ JonB

        @viniltc

            if (event->key() == Qt::Key_A) {
                emit letterAPressed();
        

        So is this hit?

        V Offline
        V Offline
        viniltc
        wrote on last edited by
        #3

        @JonB hi, It works now! I forgot that I have setFocusPolicy in the mypushbutton class. So I need to click the widget and press A.

        1 Reply Last reply
        0
        • V viniltc has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved