CUSTOM WIDGET



  • Hi there!
    I have developed a basic keypad (source code attached below).
    My aim is to attach this keypad with every GUI screen in my main program (just like a pop-up keypad in a smart phone).

    I have gone through the documentation of Qt and the possible solution which I got is " to make a keypad widget with a separate class and import it on every screen"

    But I am unable to implement it.
    Kindly help me out.

    MAINWINDOW:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    QString a=0;
    
    void MainWindow::on_pushButton_10_clicked()  //0 BUTTON
    {
       a=a.append("0");
       ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_7_clicked()   //1 BUTTON
    {
        a=a.append("1");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_8_clicked()  //2
    {
        a=a.append("2");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_9_clicked()  //3
    {
        a=a.append("3");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_4_clicked()  //4
    {
        a=a.append("4");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_5_clicked()  //5
    {
        a=a.append("5");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_6_clicked()  //6
    {
        a=a.append("6");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_clicked()  //7
    {
        a=a.append("7");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_2_clicked()    //8
    {
        a=a.append("8");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_3_clicked()  //9
    {
        a=a.append("9");
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_12_clicked()  //clr button
    {
        a="0";
        ui->label->setText(a);
    }
    
    void MainWindow::on_pushButton_11_clicked() // backspace button
    {
      a=a.remove(a.length()-1,a.length());
      ui->label->setText(a);
    }
    

  • Moderators

    @SHUBHAM-SINGH-RAO Why not simply use http://doc.qt.io/qt-5/qtvirtualkeyboard-index.html ?
    What you did isn't a keyboard widget.



  • @jsulm Thanx for ur help. I have gone through the 'Virtual Keyboard' link being mentioned by you. But it won't serve my purpose because it requires licensing and other constraints.

    I am attaching the GUI of keypad being made by me in Qt whose source code I have already mentioned in the previous post.I have used the Qpushbuttons with signal and slot connections.

    Now I want it to import in every GUI screen of main programme.

    Do I need to make it a custom widget or a use it as a plugin or make a separate class for it ?

    How can I achieve it?

    0_1523013141159_calc_cropped.png


  • Qt Champions 2017

    Hi
    If you had used QWidget as base class instead of QMainwindow it would be a
    custom widget already.
    While you can use QMainWindow like a widget ( as it is) its not so common but does
    work. So basically you already done it.

    You only need to make the extra plugin code , if you want to be able to add properties
    and edit them visually in designer.

    If just to use your custom widget in a ui form, then you can use the promotion feature
    http://doc.qt.io/qt-5/designer-using-custom-widgets.html



  • @mrjj thanks a lot for ur help. Taking into account ur guidance I have reached at this state.
    Present State: I have added a new class (named as keypad) in my main programme under the 'Qt Designer Forum' with 'Widget' as a template.
    I have added my keypad code in 'keypad.cpp' subclass and made the same keypad gui in 'keypad.ui' subclass.
    Objective: I want to call (or add) this keypad GUI in my main GUI by just pressing a QPushButton (just like a keyboard which comes in an Android Phone)
    0_1523339790462_2018-04-10-081623_1366x768_scrot.png

    After Pressing CALL KEYPAD :

    0_1523340252594_11.png


  • Qt Champions 2017

    Hi
    Super . good work.

    You want to replace the button with keypad or
    jut cover it ?

    In anyway, you can just create a keypad and show it there.

    You can also add it there in advance and hide it.
    Then on button press, show() it.
    The promotion feature could help you do it. ( shown in link) else ask :)



  • @mrjj thanx for appreciating the work.
    I am going thoroughly through ur instructions.

    I have used 'Stacked Widget ' in my main GUI which contains many pages.

    My aim is to replace the 'Call Keypad' button with 'Keypad'
    But I need to achieve this task in every page of 'Stacked Widget' in the main GUI through a single 'Call Keypad' button.


  • Qt Champions 2017

    Hi
    it would not be possible to have the keypad next to the stacked ?
    So only one keypad for all pages ?



  • @mrjj hi...
    The reason why I need only one keypad everywhere is that I want to make my work modular.
    So any changes (as per requirement) being done in keypad at one time will be reflected throughout the entire project.

    Is there any solution u would like to suggest to achieve this task?


  • Qt Champions 2017

    @SHUBHAM-SINGH-RAO

    Hi
    Since you made a custom widget, you only need to change the keypad.cpp
    and all screen that uses it will be updated.
    So you got that covered already :)



  • @mrjj hello...
    This is to inform u that I have accomplished my task successfully
    Thanks for ur help.
    I have displayed the same keypad on all the pages of main GUI by creating a separate 'Qt Designer Form Class' where I created the GUI of keypad. And I promoted the main GUI to this keypad class.

    I am attaching the GUI images of my work alongwith it .

    My next challenge is to enable keyboard functionality.
    I want to connect the signals emitted by keypad buttons to the mainwindow.

    That is, on pressing the keys of keyboard, the text should be displayed in the 'lineedit' available immediately on the current page ( of the 'Stacked Widget').
    In other words, on pressing the keys of keypad, the text should be displayed on the 'lineedit ' where the cursor has been placed.

    I am presently studying the 'Connect' and 'Emit' signals in Qt to find solution.
    But what to know ur opinion also!

    Post Script: PFA gui images for better understanding of problem.

    1. Page-1 :
      0_1523603620758_2018-04-13-090845_1366x768_scrot.png

    2. Page-2 :
      0_1523603688225_2018-04-13-090912_1366x768_scrot.png

    3. Page-3 :
      0_1523603717374_2018-04-13-090930_1366x768_scrot (1).png


  • Qt Champions 2017

    Hi
    Good work.
    and +1 for using images to make sure we understand what being asked.

    One way to make the keyboard actually work is to use sendEvent and
    QApp. focusObject() to know where to send keys.

    #include <QCoreApplication>
    #include <QGuiApplication>
    #include <QKeyEvent>
    
    void KeyboardAlphaWidget::buttonClicked(int key) {
      
      QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString());
      QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier);
      QCoreApplication::sendEvent(QGuiApplication::focusObject(), &pressEvent);
      QCoreApplication::sendEvent(QGuiApplication::focusObject(), &releaseEvent);
    
    }
    

    note that you need to know what a buttons send as key etc in some sort of table or otherwise.
    That is the int key you see in sample.
    i found inspiration in
    https://github.com/KDAB/virtual-keyboard-demo/blob/master/server/keyboard.cpp



  • @mrjj Hi...its becoming hard for me to implement it...Can u help me out!
    Because QSignalMapper is an obsolete class, that may lead to errors in mapping


  • Qt Champions 2017

    @SHUBHAM-SINGH-RAO said in CUSTOM WIDGET:

    @mrjj Hi...its becoming hard for me to implement it...Can u help me out!
    Because QSignalMapper is an obsolete class, that may lead to errors in mapping

    No, it wont lead to errors. Its just that with c++11 and lambdas,
    we now got other options.

    Since i dont know how u store the mappings for the keys etc.
    Its hard to say what you should do.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.