reading barcode from barcode scanner

  • Hi,

    I have an Android tablet and it has a build in barcode scanner. The tablet also has an barcode scanner helper, which i manage to scan (by pressing the button next to the tablet) a barcode and display the barcode on text file or the webpage as a normal string. I'm bit stuck how to my qt application read the barcode when i press the scan button (next to the tablet).

    My dialog has the CDialog::eventFilter(QObject*, QEvent*) does called when i pressed the button next to tablet which it's fine. But I don't know how to retrieve the barcode from the scanner. What i know is the barcode scanner will act as a "keyboard". So how do i read from this "keyboard"? any guide please?

    thanks in advance.


  • Lifetime Qt Champion

    Hi and welcome
    Normally if a scanner acts like a keyboard - the way to read it, is to have
    a edit control focused so all input goes there.
    It simply sends the barcode data as it was typed on keyboard.
    This is on Desktop though.
    Could be different on android.

    Should be easy to test. Just take any program that accept text input and
    press scan.

  • Hi mrjj,

    thanks. Yes, I have created a dummy application. It's just a dummy Windows Dialog. and I have a create a QTextEdit. It seems works if i touch/select QTextEdit box on the application. When I pressed button on the tablet, the MainWindow::KeyPressEvent print out the correct information.

    The problem i have is I don't want to press on the screen and select the QTextEdit widget, is there anyway to connect m_barcode as the default input for the 'keyboard'?

    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        m_barcode = new QTextEdit(ui->centralWidget);
        qDebug()<< "Setting up focus";
       // grabKeyboard();
    void MainWindow::keyPressEvent(QKeyEvent *event){
        qDebug() <<"key:" << m_barcode->toPlainText();


  • Lifetime Qt Champion

    The "keyboard" goes to the widget that has focus.
    Setting focus should do it

    but remove the
    setFocus(); << set focus to main window..

    You can check if it is as expected - if you can start app and just type and it shows it.
    Else something else stole the focus :)

  • cool. thanks. it works on my desktop where i can type properly. I guess it doesn't work on the android. I do have the sdk from the manufacturer. probably will use the sdk instead of using their build-in barcode scanner application. but the sdk is using Java.

    thank btw :) I'll close off this topic anyway and will create a separate for the question on the loading sdk on the qt project.

  • Lifetime Qt Champion


    wait, sometime setting focus in constructor is not optimal.

    Last test would be
    void MainWindow::showEvent( QShowEvent* event ) {
    Q MainWindow::showEvent( event );

  • Hi mrjii,

    Thanks for the tips. Still not quite working. although at it startup, the cursor is shows on the m_barcode textbox. The m_barcode will only pick up the 'keyboard input' from the barcode scanner when the keyboard pop-up on the screen.

    I'm looking is the application doesn't shows the keyboard on the screen but automatically read from the barcode scanner when i pressed on the button next to the tablet. I guess using their sdk is my last resort.

    thanks for all the great tips btw. :)

  • Lifetime Qt Champion

    Good testing :)
    Ok, seems that focus is not enough. If cursor is show in edit then it should have the focus.
    Guess it checks for keyboard open or something like that.

    So if you want this to work without keyboard being shown then
    yes, the SDK might help.

Log in to reply

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