QListView: How Qt::Key_Return has the same action as doubleClicked ?



  • Qt::Key:Arrow can move QListViewItem default, how to trigger signal with Key_Return ?

    connect(ui->listView,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(open(QModelIndex)));

    connect(new QShortcut(QKeySequence(Qt::Key_Return),this), SIGNAL(activated()),this, SLOT(open(ui->listView->selectedIndexes().at(0)))); // No such slot



  • Hi @sonichy

    you cannot write something like open(ui->listView->selectedIndexes().at(0)) into a connect statement. It's illegal syntax. You will need to write the argument type instead, like you did above with open(QModelIndex).

    -Michael.


  • Moderators

    @sonichy said in QListView: How Qt::Key_Return has the same action as doubleClicked ?:

    connect(new QShortcut(QKeySequence(Qt::Key_Return),this), SIGNAL(activated()),this, SLOT(open(ui->listView->selectedIndexes().at(0)))); // No such slot

    signal activatedhas no parameter. You are trying to assign the signal to a specific element, which is not possible as you already indicate with your comment.

    You need to define a slot routine with no parameter and handle all things in there. E.g.

    class blibla
    {
             Q_OBJECT 
    
    ...
    public slots: 
         void sltKeyReturn (); 
    }
    
    void blibla::sltKeyReturn () 
    {
          ui->listView->selectedIndexes().at(0);   // here you need to do something with it
    }
    

    blibla is basically the class you are working in, referred to in your connect with "this";



  • @sonichy
    additionaly to what the others said, this is a good situation to use a lambda.

    QShortcut *sCut = new QShortcut(QKeySequence(Qt::Key_Return),this);
    connect(sCut, &QShortcut::activated, this, [=]()->void{open(ui->listView->selectedIndexes().at(0));});
    //I'm hower not sure, if open(int) is actually a valid function -> but if its not the compiler will complain.
    


  • @koahnig
    Use slot to trigger slot solve the problem indeed !

    open(ui->listView->selectedIndexes().at(0));
    Error : 'selectedIndexes' is a protected member of 'QListView'

    Change to this:
    open(ui->listView->selectionModel()->selectedIndexes().at(0));


Log in to reply
 

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