Results 1 to 15 of 15

Thread: 3 Questions on QListView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 3 Questions on QListView

    Hi everybody,
    i was learning qlistview and writing simple application for testing. First of all the sample code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QVBoxLayout>
    3. #include <QListView>
    4. #include <QLabel>
    5. #include <QStringListModel>
    6. #include <QStringList>
    7.  
    8. class MyWidget: public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MyWidget(QWidget *parent=0);
    14.  
    15. private slots:
    16. void currentSelected(const QModelIndex &);
    17.  
    18. private:
    19. QListView *listView;
    20. QLabel *selectedItemText;
    21. };
    22.  
    23. MyWidget::MyWidget(QWidget *parent)
    24. :QWidget(parent)
    25. {
    26. listView = new QListView;
    27. selectedItemText = new QLabel;
    28.  
    29. model = new QStringListModel();
    30. list << "lemons" << "melons" << "oranges" << "pears"
    31. << "grapes" << "apples" << "potatoes";
    32. model->setStringList(list);
    33.  
    34. listView->setModel(model);
    35.  
    36. QVBoxLayout *layout = new QVBoxLayout;
    37. layout->addWidget(listView);
    38. layout->addWidget(selectedItemText);
    39. setLayout(layout);
    40.  
    41. connect(listView, SIGNAL(clicked(const QModelIndex&)),
    42. this, SLOT(currentSelected(const QModelIndex &)));
    43. }
    44.  
    45. void MyWidget::currentSelected(const QModelIndex &current)
    46. {
    47. selectedItemText->setText(current.data().toString());
    48. }
    49.  
    50. int main(int argc, char *argv[])
    51. {
    52. QApplication app( argc, argv );
    53.  
    54. QWidget *window = new QWidget;
    55. window->setWindowTitle("Sample ListView");
    56.  
    57. MyWidget *myWidget = new MyWidget;
    58.  
    59. QVBoxLayout *mainLayout = new QVBoxLayout;
    60. mainLayout->addWidget(myWidget);
    61. window->setLayout(mainLayout);
    62. window->show();
    63.  
    64. return app.exec();
    65. }
    66.  
    67. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    My questions are:

    1) How start the application with some item already selected?
    With the code above when an item is selected the QLabel is filled with the item data; but when the application starts no item is selected and the label is empty.

    2) How bind the function currentSelected(const QModelIndex &) with up/down arrows keys?
    In the application if I move the selection with the up/arrow keys the highlighted item changes but the label is not updated with the text of the highlighted item. I want the behavior of mouse single click with up/arrow keys.

    3) How change the double-click event?
    Now when an item is double-clicked the item goes in edit mode; i don't want this. Double-click like single-click is sufficient for me.

    Hoping i was clear, thanks in advance for helping me.
    Giuseppe

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Questions on QListView

    1. Use setCurrentIndex(model()->index(0));

    2.
    Qt Code:
    1. connect(listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
    2. this, SLOT(currentSelected(const QModelIndex &)));
    3. }
    To copy to clipboard, switch view to plain text mode 
    The last parameter in the signal will be ignored by the slot.

    3. listView->setEditTriggers(QAbstractItemView::NoEditTriggers );

    Regards

  3. The following user says thank you to marcel for this useful post:

    jiveaxe (1st August 2007)

  4. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 3 Questions on QListView

    Thanks marcel for your help; I noticed that code 2) solves question 1) too.

    Can I post you a new Question? How word wrap text in a listview? I have used

    Qt Code:
    1. listView->setWordWrap(true);
    To copy to clipboard, switch view to plain text mode 

    but nothing happens; what's wrong? missing something else?

    Thanks a lot,
    Giuseppe

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Questions on QListView

    What do you expect it to do?
    wordWrap : bool This property holds the item text word-wrapping policy.
    If this property is true then item text text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property is false by default.
    So, if you have a really long word, then it won't be wrapped. If you have multiple words, then they will be wrapped as necessary.

    A possible solution is( that is guaranteed to work), is to create your own item delegate ( a delegate dictates the look and feel of an item ).

    When you paint the delegate, then you can take care of the wrapping too.

    Regards

  6. #5
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 3 Questions on QListView

    Ok, marcel; now open the manual and search for delegate to learn using it.

    Again thanks

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Questions on QListView

    OK.
    A hint: I think you can use QStyleOptionViewItem::rect. This is the dimension of the item. You can do the wrapping relative to its size. A QStyleOptionViewItem object is passed to the delegate's paint method.

    Regards

  8. #7
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 3 Questions on QListView

    I have to return on marcel' replay to my first post; i have noticed that using, as in answer to question 1, this code:

    Qt Code:
    1. currentSelected(model->index(2));
    To copy to clipboard, switch view to plain text mode 

    (with 2 instead of 0 (zero)) the item selected is the first (with index = 0). If, instead, putting a comment in front of the marcel's code in answer 2 the correct item is displayed in the label.

    Where is the problem?

    Bye

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Questions on QListView

    Could you post your code?
    Only the relevant sections.

    Regards

  10. #9
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 3 Questions on QListView

    Take as sample the code in my first post; i have inserted after the connect statement in constructor

    Qt Code:
    1. connect(listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
    2. this, SLOT(currentSelected(const QModelIndex &)));
    3.  
    4. currentSelected(model->index(2));
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Questions on QListView

    Do you change the selection programatically after this code?

    Regards

  12. #11
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 3 Questions on QListView

    No, i hope.
    Here the complete code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QVBoxLayout>
    3. #include <QListView>
    4. #include <QLabel>
    5. #include <QStringListModel>
    6. #include <QStringList>
    7.  
    8. class MyWidget: public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MyWidget(QWidget *parent=0);
    14.  
    15. private slots:
    16. void currentSelected(const QModelIndex &);
    17.  
    18. private:
    19. QListView *listView;
    20. QLabel *selectedItemText;
    21. };
    22.  
    23. MyWidget::MyWidget(QWidget *parent)
    24. :QWidget(parent)
    25. {
    26. listView = new QListView;
    27. listView->setWordWrap(true);
    28. listView->setEditTriggers(QAbstractItemView::NoEditTriggers );
    29. selectedItemText = new QLabel;
    30.  
    31. model = new QStringListModel();
    32. list << "lemons" << "melons" << "oranges" << "pears"
    33. << "grapes" << "apples" << "potatoes";
    34. model->setStringList(list);
    35.  
    36. listView->setModel(model);
    37.  
    38. QVBoxLayout *layout = new QVBoxLayout;
    39. layout->addWidget(listView);
    40. layout->addWidget(selectedItemText);
    41. setLayout(layout);
    42.  
    43. connect(listView, SIGNAL(clicked(const QModelIndex&)),
    44. this, SLOT(currentSelected(const QModelIndex &)));
    45. connect(listView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
    46. this, SLOT(currentSelected(const QModelIndex &)));
    47.  
    48. currentSelected(model->index(2));
    49. }
    50.  
    51. void MyWidget::currentSelected(const QModelIndex &current)
    52. {
    53. selectedItemText->setText(current.data().toString());
    54. }
    55.  
    56. int main(int argc, char *argv[])
    57. {
    58. QApplication app( argc, argv );
    59.  
    60. QWidget *window = new QWidget;
    61. window->setWindowTitle("Sample ListView");
    62.  
    63. MyWidget *myWidget = new MyWidget;
    64.  
    65. QVBoxLayout *mainLayout = new QVBoxLayout;
    66. mainLayout->addWidget(myWidget);
    67. window->setLayout(mainLayout);
    68. window->show();
    69.  
    70. return app.exec();
    71. }
    72.  
    73. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Giuseppe

Similar Threads

  1. QListView word wrap
    By serega in forum Qt Programming
    Replies: 17
    Last Post: 30th August 2007, 03:13
  2. QDialog / QListView problem
    By harakiri in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2007, 18:31
  3. Subclass QListView to show two colums in one
    By Mookie in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2007, 02:12
  4. Memory management questions (im new to Qt)
    By scarvenger in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 07:41
  5. Keeping focus at bottom of QListView
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2006, 14:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.