[Qt-interest] List widget item - gaining focus

Tibor Kiss tibor at wiesen.at
Wed Mar 17 21:35:47 CET 2010


Hi!

My apologies, but your code is a bit confusing.

The short answer is, you can try

listview->setFocus();

at the and of Dialog::display.

The not so short is, that your sample code has some strange and ugly
realization... I don't see the point to delete the layout and it's children
one by one. Probably you have the right reason to do it in your real code...
I don't know. If you insist to delete the layout (not a good solution), as
far as I know, you don't need to delete it's children. The layout takes the
ownership when you add widgets to it, and when you delete it, it will delete
all of it's children too.

But my suggestion is, that you should post your reasons behind this, and
probably we will show you a much better way to do it.

Tibor


2010/3/16 linux newbie <linux.newbie79 at gmail.com>

> *
> HI,
>
> I am having issue in focusing list widget item.
>
> I created a simple test project. The display routine creates a new layout
> and adds the listwidget into it. When this routine is called first time, the
> list widget item is highlighted as  expected. Event filter is attached to
> this dialog and on pressing "Return" key, dataChanged() signal is emitted
> which in turn calls my display() routine.
>
> Inside display() routine, previous layout is deleted along with its
> widget(list widget). But now, the item in listwidget is not highlighted i.e
> list widget is not gained focus, but when I click the key, it gets focus.
> Can anyone let me know the mistake I made in the below code.
>
> Dialog::Dialog(QWidget *parent):QDialog(parent)*
>
> {
>
> this->installEventFilter(this);
>
>  connect(this, SIGNAL(dataChanged()), this,
>
> SLOT(display()));
>
> this->display();
>
> }
>
> *void Dialog::display()*
>
> {
>
>  QLayout *currLayout = this->layout();
>
> if (currLayout != NULL)
>
> {
>
> QLayoutItem *child;
>
> while ((child = currLayout->takeAt(0)) != 0)
>
> {
>
> QWidget *childWidget = child->widget();
>
> childWidget->deleteLater();
>
>  }
>
> delete currLayout;
>
> }
>
>  QListWidget *listWidget = new QListWidget();
>
>  listWidget->addItem(QString("1"));
>
> listWidget->addItem(QString("2"));
>
> listWidget->addItem(QString("3"));
>
>  listWidget->setCurrentRow(1);
>
> QVBoxLayout *layout = new QVBoxLayout();
>
> layout->addWidget(listWidget);
>
>  this->setLayout(layout);
>
> this->show();
>
> }
>
> *bool Dialog::eventFilter(QObject *obj, QEvent *event)*
>
> {
>
> if (event->type() == QEvent::KeyPress)
>
> {
>
> QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
>
>  int key = keyEvent->key();
>
>  if (key == Qt::Key_Return)
>
> {
>
> emit dataChanged();
>
> }
>
> }
>
> return false;
>
> }
>
>
>
> Thanks
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100317/46d088c1/attachment.html 


More information about the Qt-interest-old mailing list