Important: Please read the Qt Code of Conduct -

KeyPressEvent to string to qlistview

  • Dear,

    I would like to read pressed keys and combine them to a string. The string will be checked in a database and the database will send some info back. That info must been show in qlistview.
    My problem is that I can catch keypresses and enterkey if there is no widget in the screen. But when there is a widget I only can detect a enter. So what should be the problem? Or how can i do it better
    void Buy::keyPressEvent(QKeyEvent* event)
    int i;
    char c;
    i = event->key();

    std::string str ;
    if((event->key() == Qt::Key_Enter)||(event->key() == Qt::Key_Return))
            //enter or return was pressed
            QMessageBox::critical(this,tr("Enter"), tr("Enter"));


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You'll need to provide more information. On what widget are you trying to get the keyPressEvent ? Does your widget have focus when the other widget is on screen etc. ?

  • Thank you for your reply.

    I want to use QlistWidget or QlistView.
    So when the window opens there is one of the above widget and some buttons. The characters that I want to catch are from a barcode reader. The reader act like a keyboard and the last character is a enter.

    So if the window is open I want to read a barcode that will be convert to a string. That string will be checked in a database and give back information as price and name. The string, price and name I want to display in the Qlist.

  • Lifetime Qt Champion

    To avoid problems, you should use a dedicated widget e.g. a QLineEdit that when you set the focus on will get the scanner input and make your logic from there. Otherwise you will have to set a filter on all your widgets but you won't be able to know when the user is typing on a keyboard or using the scanner.

    Hope it helps

Log in to reply