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. Key press event to choose list
Forum Updated to NodeBB v4.3 + New Features

Key press event to choose list

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqt5.5qt 5.7qkeypressevent
14 Posts 3 Posters 4.2k Views 2 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.
  • K Kinesis

    Hi , I would like to add up and down key press event functions in my code . There are directory lists in a widget , when I click a directory the items (inside this selected directory) will be displayed. In that case I want to add up and down key press action , if I press down key from keyboard , the below directory will be selected and display every items and same function for up key.How can I add key press event?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Kinesis What widget is this? QListWidget?

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

    K 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Kinesis What widget is this? QListWidget?

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

      @jsulm
      Yes widget is QListWidget.Here is the current code.

       connect(listWidget_dirList, & QListWidget::itemClicked,[directory,listWidget_main,this](QListWidgetItem *item)
              {
                  listWidget_main->show();
                  listWidget_main->clear();
                  // QDir directory(item->text());
                  QDir dir = directory.absolutePath() + '/' + item->text();
      
      dir.setNameFilters({"*.png", "*.jpg"});
      
                  for(const QFileInfo & finfo: dir.entryInfoList()){
      
                      QPixmap image(finfo.absoluteFilePath());
                      auto imageLabel = new QLabel();
                      imageLabel->setMinimumSize(320,320);
                      imageLabel->setScaledContents(true);
                      imageLabel->setPixmap(image);
      
      
      
                      auto    item = new QListWidgetItem("",listWidget_main);
                      auto    widget = new QWidget;
                      auto    label = new QLabel(finfo.fileName());
                      auto    vb = new QVBoxLayout;
      
                      vb->addWidget(label);
                      vb->addWidget(imageLabel);
      
                      widget->setLayout(vb);
                      widget->setMinimumSize(340,340);
                      listWidget_main->setItemWidget(item,widget);
      }
      
      jsulmJ 1 Reply Last reply
      0
      • K Kinesis

        @jsulm
        Yes widget is QListWidget.Here is the current code.

         connect(listWidget_dirList, & QListWidget::itemClicked,[directory,listWidget_main,this](QListWidgetItem *item)
                {
                    listWidget_main->show();
                    listWidget_main->clear();
                    // QDir directory(item->text());
                    QDir dir = directory.absolutePath() + '/' + item->text();
        
        dir.setNameFilters({"*.png", "*.jpg"});
        
                    for(const QFileInfo & finfo: dir.entryInfoList()){
        
                        QPixmap image(finfo.absoluteFilePath());
                        auto imageLabel = new QLabel();
                        imageLabel->setMinimumSize(320,320);
                        imageLabel->setScaledContents(true);
                        imageLabel->setPixmap(image);
        
        
        
                        auto    item = new QListWidgetItem("",listWidget_main);
                        auto    widget = new QWidget;
                        auto    label = new QLabel(finfo.fileName());
                        auto    vb = new QVBoxLayout;
        
                        vb->addWidget(label);
                        vb->addWidget(imageLabel);
        
                        widget->setLayout(vb);
                        widget->setMinimumSize(340,340);
                        listWidget_main->setItemWidget(item,widget);
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Kinesis Can't you just use http://doc.qt.io/qt-5/qlistwidget.html#currentRowChanged ?

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

        K 1 Reply Last reply
        2
        • jsulmJ jsulm

          @Kinesis Can't you just use http://doc.qt.io/qt-5/qlistwidget.html#currentRowChanged ?

          K Offline
          K Offline
          Kinesis
          wrote on last edited by
          #5

          @jsulm
          Well , as you said I tried to use currentRow Changed , but I need to take the text from current row , to get directory path. How can I get the text from current row.There may be some mistakes in my code.Take a look please.

          connect(listWidget_dirList, & QListWidget::currentRowChanged,[directory,listWidget_main,this](int currentRow)
                  {
                      listWidget_main->show();
                      listWidget_main->clear();
                      // QDir directory(item->text());
                      QDir dir = directory.absolutePath() + '/' + currentRow->text();
                      dir.setNameFilters({"*.png", "*.jpg"});
          
                      for(const QFileInfo & finfo: dir.entryInfoList()){
                ----------------
                ----------------
                ----------------
          }
          });
          
          jsulmJ SGaistS 2 Replies Last reply
          0
          • K Kinesis

            @jsulm
            Well , as you said I tried to use currentRow Changed , but I need to take the text from current row , to get directory path. How can I get the text from current row.There may be some mistakes in my code.Take a look please.

            connect(listWidget_dirList, & QListWidget::currentRowChanged,[directory,listWidget_main,this](int currentRow)
                    {
                        listWidget_main->show();
                        listWidget_main->clear();
                        // QDir directory(item->text());
                        QDir dir = directory.absolutePath() + '/' + currentRow->text();
                        dir.setNameFilters({"*.png", "*.jpg"});
            
                        for(const QFileInfo & finfo: dir.entryInfoList()){
                  ----------------
                  ----------------
                  ----------------
            }
            });
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #6

            @Kinesis As you can see currentRow is an integer - you can't call any methods on it, right?
            If you check the documentation you will find this: http://doc.qt.io/qt-5/qlistwidget.html#item

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

            K 1 Reply Last reply
            1
            • jsulmJ jsulm

              @Kinesis As you can see currentRow is an integer - you can't call any methods on it, right?
              If you check the documentation you will find this: http://doc.qt.io/qt-5/qlistwidget.html#item

              K Offline
              K Offline
              Kinesis
              wrote on last edited by Kinesis
              #7

              @jsulm
              So how can I get the text from selected directroy ? Is there another method to get the text ? I think I can use "QListWidget::itemSelectionChanged()". But if I use that how can I get the text(directory path)?

              jsulmJ 1 Reply Last reply
              0
              • K Kinesis

                @jsulm
                So how can I get the text from selected directroy ? Is there another method to get the text ? I think I can use "QListWidget::itemSelectionChanged()". But if I use that how can I get the text(directory path)?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Kinesis I already suggested what to do to get the text. Did you read it? Again: you can get the current item using currentRow and http://doc.qt.io/qt-5/qlistwidget.html#item, then you simply call http://doc.qt.io/qt-5/qlistwidgetitem.html#text on the item to get the text.

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

                K 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Kinesis I already suggested what to do to get the text. Did you read it? Again: you can get the current item using currentRow and http://doc.qt.io/qt-5/qlistwidget.html#item, then you simply call http://doc.qt.io/qt-5/qlistwidgetitem.html#text on the item to get the text.

                  K Offline
                  K Offline
                  Kinesis
                  wrote on last edited by
                  #9

                  @jsulm
                  I tried to get the current row with "QListWidgetItem *item(int row)" and call"item->text()" on the item to get the text . It still doesn't work.But I dont't know where I made mistake.

                  jsulmJ 1 Reply Last reply
                  0
                  • K Kinesis

                    @jsulm
                    Well , as you said I tried to use currentRow Changed , but I need to take the text from current row , to get directory path. How can I get the text from current row.There may be some mistakes in my code.Take a look please.

                    connect(listWidget_dirList, & QListWidget::currentRowChanged,[directory,listWidget_main,this](int currentRow)
                            {
                                listWidget_main->show();
                                listWidget_main->clear();
                                // QDir directory(item->text());
                                QDir dir = directory.absolutePath() + '/' + currentRow->text();
                                dir.setNameFilters({"*.png", "*.jpg"});
                    
                                for(const QFileInfo & finfo: dir.entryInfoList()){
                          ----------------
                          ----------------
                          ----------------
                    }
                    });
                    
                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Hi,

                    @Kinesis said in Key press event to choose list:

                    listWidget_main->show();
                    listWidget_main->clear();

                    Aren't you emptying the content of your listWidget before getting the value you are interested in ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    K 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      @Kinesis said in Key press event to choose list:

                      listWidget_main->show();
                      listWidget_main->clear();

                      Aren't you emptying the content of your listWidget before getting the value you are interested in ?

                      K Offline
                      K Offline
                      Kinesis
                      wrote on last edited by
                      #11

                      @SGaist
                      Yes , I first clear the widget and show items from other dirs. If I don't clear the widget , that's still fine. I just want to test it clearly.

                      1 Reply Last reply
                      0
                      • K Kinesis

                        @jsulm
                        I tried to get the current row with "QListWidgetItem *item(int row)" and call"item->text()" on the item to get the text . It still doesn't work.But I dont't know where I made mistake.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #12

                        @Kinesis said in Key press event to choose list:

                        QListWidgetItem *item(int row)

                        connect(listWidget_dirList, & QListWidget::currentRowChanged,[directory,listWidget_main,this](int currentRow)
                        {
                            listWidget_main->show();
                            listWidget_main->clear();
                        
                            QListWidgetItem *item = listWidget_dirList->item(currentRow);
                            if (item)  {
                                QString text = item->text();
                                ...
                            }
                        

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

                        K 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @Kinesis said in Key press event to choose list:

                          QListWidgetItem *item(int row)

                          connect(listWidget_dirList, & QListWidget::currentRowChanged,[directory,listWidget_main,this](int currentRow)
                          {
                              listWidget_main->show();
                              listWidget_main->clear();
                          
                              QListWidgetItem *item = listWidget_dirList->item(currentRow);
                              if (item)  {
                                  QString text = item->text();
                                  ...
                              }
                          
                          K Offline
                          K Offline
                          Kinesis
                          wrote on last edited by
                          #13

                          @jsulm
                          I am really sorry . I misunderstood your explanation . Now it works . Thanks

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            My bad, I misread from where the signal came from.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            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