void MainWindow::focusAssistant() { QListWidget* widget = qobject_cast<QListWidget*>(m_assistantToolBox->currentWidget()); if (widget) { widget->setFocus(); if (widget->selectedItems().count() == 0) { widget->setCurrentItem(widget->itemAt(0, 0)); } } }
void tst_QMenu::widgetActionFocus() { //test if the focus is correctly handled with a QWidgetAction QMenu m; QListWidget *l = new QListWidget(&m); for (int i = 1; i<3 ; i++) l->addItem(QString("item%1").arg(i)); QWidgetAction *wa = new QWidgetAction(&m); wa->setDefaultWidget(l); m.addAction(wa); m.setActiveAction(wa); l->setFocus(); //to ensure it has primarily the focus QAction *menuitem1=m.addAction("menuitem1"); QAction *menuitem2=m.addAction("menuitem2"); m.popup(QPoint()); QVERIFY(m.isVisible()); QVERIFY(l->hasFocus()); QVERIFY(l->currentItem()); QCOMPARE(l->currentItem()->text(), QString("item1")); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down); QVERIFY(l->currentItem()); QCOMPARE(l->currentItem()->text(), QString("item2")); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down); QVERIFY(m.hasFocus()); QCOMPARE(m.activeAction(), menuitem1); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down); QVERIFY(m.hasFocus()); QCOMPARE(m.activeAction(), menuitem2); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Up); QVERIFY(m.hasFocus()); QCOMPARE(m.activeAction(), menuitem1); QTest::keyClick(QApplication::focusWidget(), Qt::Key_Up); QVERIFY(l->hasFocus()); QCOMPARE(m.activeAction(), (QAction *)wa); }
// **** keyboard selection dialog ********************************************* int KNHelper::selectDialog(QWidget *parent, const QString &caption, const QStringList &options, int initialValue) { KDialog *dlg = new KDialog( parent ); dlg->setCaption( caption ); dlg->setButtons( KDialog::Ok|KDialog::Cancel ); dlg->setDefaultButton( KDialog::Ok ); QFrame *page = new QFrame( dlg ); dlg->setMainWidget( page ); QHBoxLayout *pageL = new QHBoxLayout(page); pageL->setSpacing(5); pageL->setMargin(8); QListWidget *list = new QListWidget( page ); pageL->addWidget(list); QString s; for ( QStringList::ConstIterator it = options.begin(); it != options.end(); ++it ) { s = (*it); // remove accelerators s.replace( QRegExp( "&" ), "" ); // krazy:exclude=doublequote_chars list->addItem( s ); } list->setCurrentRow( initialValue ); list->setFocus(); QObject::connect( list, SIGNAL( itemActivated( QListWidgetItem* ) ), dlg, SLOT( accept() ) ); restoreWindowSize("selectBox", dlg, QSize(247,174)); int ret; if (dlg->exec()) ret = list->currentRow(); else ret = -1; saveWindowSize("selectBox", dlg->size()); delete dlg; return ret; }