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 WIDGET
Forum Updated to NodeBB v4.3 + New Features

CUSTOM WIDGET

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 3.4k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SHUBHAM SINGH RAO
    wrote on last edited by VRonin
    #1

    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);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • S SHUBHAM SINGH RAO

      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);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SHUBHAM SINGH RAO
        wrote on last edited by SHUBHAM SINGH RAO
        #3

        @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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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

          S 1 Reply Last reply
          1
          • mrjjM mrjj

            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

            S Offline
            S Offline
            SHUBHAM SINGH RAO
            wrote on last edited by SHUBHAM SINGH RAO
            #5

            @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

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 :)

              S 1 Reply Last reply
              0
              • mrjjM mrjj

                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 :)

                S Offline
                S Offline
                SHUBHAM SINGH RAO
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  S 1 Reply Last reply
                  0
                  • mrjjM mrjj

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

                    S Offline
                    S Offline
                    SHUBHAM SINGH RAO
                    wrote on last edited by
                    #9

                    @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?

                    mrjjM 1 Reply Last reply
                    0
                    • S SHUBHAM SINGH RAO

                      @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?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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 :)

                      S 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @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 :)

                        S Offline
                        S Offline
                        SHUBHAM SINGH RAO
                        wrote on last edited by SHUBHAM SINGH RAO
                        #11

                        @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

                        1 Reply Last reply
                        1
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          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

                          S 1 Reply Last reply
                          2
                          • mrjjM mrjj

                            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

                            S Offline
                            S Offline
                            SHUBHAM SINGH RAO
                            wrote on last edited by SHUBHAM SINGH RAO
                            #13

                            @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

                            mrjjM 1 Reply Last reply
                            0
                            • S SHUBHAM SINGH RAO

                              @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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @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.

                              1 Reply Last reply
                              0

                              • Login

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