Key press event to choose list
-
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?
-
@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); }
-
@Kinesis Can't you just use http://doc.qt.io/qt-5/qlistwidget.html#currentRowChanged ?
-
@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()){ ---------------- ---------------- ---------------- } });
-
@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 -
@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.
-
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 ?
-
@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(); ... }
-
My bad, I misread from where the signal came from.