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. QListView: How Qt::Key_Return has the same action as doubleClicked ?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.8k 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    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

    https://github.com/sonichy

    m.sueM K J.HilkJ 3 Replies Last reply
    0
    • sonichyS sonichy

      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

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • sonichyS sonichy

        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

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @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";

        Vote the answer(s) that helped you to solve your issue(s)

        sonichyS 1 Reply Last reply
        3
        • sonichyS sonichy

          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

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by VRonin
          #4

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

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • K koahnig

            @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";

            sonichyS Offline
            sonichyS Offline
            sonichy
            wrote on last edited by
            #5

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

            https://github.com/sonichy

            1 Reply Last reply
            2

            • Login

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