Results 1 to 3 of 3

Thread: QListView nonstandard use problem (use different model problem)

  1. #1
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QListView nonstandard use problem (use different model problem)

    I want switch different ctl in QListView when I clicked different button

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QStandardItem>
    6. #include <QStandardItemModel>
    7. #include <QList>
    8. #include <QRadioButton>
    9. #include <QCheckBox>
    10. #include <QListView>
    11.  
    12. namespace Ui {
    13. class MainWindow;
    14. }
    15.  
    16. class MainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23.  
    24. private slots:
    25. void on_pushButton_clicked();
    26.  
    27. void on_pushButton_2_clicked();
    28.  
    29. void on_listView_clicked(const QModelIndex &index);
    30.  
    31. private:
    32. Ui::MainWindow *ui;
    33. void init();
    34. QListView *plist;
    35. QStandardItemModel *modelcheck;
    36. QStandardItemModel *modelradio;
    37. QList<QRadioButton*> lvradio;
    38. QList<QCheckBox*> lvcheck;
    39.  
    40. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. init ();
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::init() {
    18.  
    19. plist = ui->listView;
    20. modelcheck = new QStandardItemModel();
    21. modelradio = new QStandardItemModel();
    22.  
    23. for (int i = 0; i < 10; ++i) {
    24. QRadioButton *p = new QRadioButton(QString("radio") + QString::number (i));
    25. lvradio.push_back (p);
    26. modelradio->appendRow (new QStandardItem(""));
    27. }
    28.  
    29. for (int i = 0; i < 11; ++i) {
    30. lvcheck.push_back (new QCheckBox(QString("check") + QString::number (i)));
    31. modelcheck->appendRow (new QStandardItem(""));
    32. }
    33. }
    34.  
    35. void MainWindow::on_pushButton_clicked() {
    36. plist->setModel (modelcheck);
    37. for (int i = 0; i < modelcheck->rowCount (); ++i) {
    38. lvcheck.at (i)->show ();
    39. plist->setIndexWidget (modelcheck->index (i, 0), lvcheck.at (i));
    40. }
    41. }
    42.  
    43. void MainWindow::on_pushButton_2_clicked() {
    44. plist->setModel (modelradio);
    45. for(int i = 0; i < modelradio->rowCount (); i++) {
    46. plist->setIndexWidget (modelradio->index (i, 0), lvradio.at (i));
    47. //here when use new QRadioButton("aa"); it works right
    48. }
    49. }
    50.  
    51. void MainWindow::on_listView_clicked(const QModelIndex &index) {
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. plist->setIndexWidget (modelcheck->index (i, 0), new QCheckBox("xxx"));
    To copy to clipboard, switch view to plain text mode 
    1.everytime I use new does it memleaks ?
    2.why setIndexwidget(...) revive a SIGTERM

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QListView nonstandard use problem (use different model problem)

    setIndexWidget() will delete the widget already set on the index, so setting a checkbox will first delete the exisiting widget. Storing the widgets in lvcheck and lvradio are no more valid (already deleted)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    mars_tl (2nd July 2013)

  4. #3
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListView nonstandard use problem (use different model problem)

    Quote Originally Posted by Santosh Reddy View Post
    setIndexWidget() will delete the widget already set on the index, so setting a checkbox will first delete the exisiting widget. Storing the widgets in lvcheck and lvradio are no more valid (already deleted)
    Thanks a lot .


    Qt Code:
    1. void qabstractitemview::setindexwidget(const qmodelindex &index, qwidget *widget)
    2. {
    3. q_d(qabstractitemview);
    4. if (!d->isindexvalid(index))
    5. return;
    6. if (qwidget *oldwidget = indexwidget(index)) {
    7. d->persistent.remove(oldwidget); //here remove the oldwidget..
    8. d->removeeditor(oldwidget);
    9. oldwidget->deletelater();
    10. }
    11. if (widget) {
    12. widget->setparent(viewport());
    13. d->persistent.insert(widget);
    14. d->addeditor(index, widget, true);
    15. widget->show();
    16. datachanged(index, index); // update the geometry
    17. if (!d->delayedpendinglayout)
    18. widget->setgeometry(visualrect(index));
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    I find some code in qt src...

Similar Threads

  1. Replies: 4
    Last Post: 18th April 2012, 18:11
  2. problem with QListView
    By penny in forum Newbie
    Replies: 6
    Last Post: 3rd January 2011, 21:34
  3. QListview Problem
    By grsandeep85 in forum Qt Programming
    Replies: 0
    Last Post: 17th August 2009, 10:55
  4. QlistView problem
    By grsandeep85 in forum Qt Programming
    Replies: 0
    Last Post: 7th August 2009, 06:19
  5. Problem with doubleClicked in QListView with Qt 4.2.2
    By gauravagarwal in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2007, 10:28

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.