[Qt-interest] List widget item - gaining focus

linux newbie linux.newbie79 at gmail.com
Thu Mar 18 09:22:10 CET 2010


Thanks for your reply.

*listview->setFocus();at the and of Dialog::display.*
*
*
*if I set listWidget->setFocus(), still the same behaviour i.e it is not
highlighted.*
*
*
*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.*
*
*
on key press, my data model will be changed. hence a signal is emitted from
event Filter routine to change the display.
I tried without deleting the layout, but the previous widgets are displayed
on the screen. Hence I deleted the widgets.
*
*

On Thu, Mar 18, 2010 at 7:35 AM, Tibor Kiss <tibor at wiesen.at> wrote:

> 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
>>
>>
>
> _______________________________________________
> 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/20100318/5881d940/attachment.html 


More information about the Qt-interest-old mailing list