[Qt-interest] List widget item - gaining focus
Tibor Kiss
tibor at wiesen.at
Thu Mar 18 19:52:24 CET 2010
Hi!
Your goal is still a bit unclear for me. You can empty the QListWidget and
repopulate it without the deletion of the QListWidget or the layout for
sure. You should rather go back to this problem, than doing this layout and
widget deletion routine.
I suggest you to have a look to the model/view framework, where you can have
all of your data in a QStringListModel, and you can connect the QListView to
display them. You can do all your data manipulation on the QStringListModel,
and the view will display it, without any extra efforts.
I can help you with this, though it's pretty basic in Qt. Feel free to write
me a mail directly, if you don't want to share this with the list...
Tibor
2010/3/18 linux newbie <linux.newbie79 at gmail.com>
> 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/d5220ec8/attachment.html
More information about the Qt-interest-old
mailing list