PDA

View Full Version : QListView nonstandard use problem (use different model problem)



mars_tl
1st July 2013, 11:14
I want switch different ctl in QListView when I clicked different button



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QList>
#include <QRadioButton>
#include <QCheckBox>
#include <QListView>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

void on_listView_clicked(const QModelIndex &index);

private:
Ui::MainWindow *ui;
void init();
QListView *plist;
QStandardItemModel *modelcheck;
QStandardItemModel *modelradio;
QList<QRadioButton*> lvradio;
QList<QCheckBox*> lvcheck;

};




#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init ();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::init() {

plist = ui->listView;
modelcheck = new QStandardItemModel();
modelradio = new QStandardItemModel();

for (int i = 0; i < 10; ++i) {
QRadioButton *p = new QRadioButton(QString("radio") + QString::number (i));
lvradio.push_back (p);
modelradio->appendRow (new QStandardItem(""));
}

for (int i = 0; i < 11; ++i) {
lvcheck.push_back (new QCheckBox(QString("check") + QString::number (i)));
modelcheck->appendRow (new QStandardItem(""));
}
}

void MainWindow::on_pushButton_clicked() {
plist->setModel (modelcheck);
for (int i = 0; i < modelcheck->rowCount (); ++i) {
lvcheck.at (i)->show ();
plist->setIndexWidget (modelcheck->index (i, 0), lvcheck.at (i));
}
}

void MainWindow::on_pushButton_2_clicked() {
plist->setModel (modelradio);
for(int i = 0; i < modelradio->rowCount (); i++) {
plist->setIndexWidget (modelradio->index (i, 0), lvradio.at (i));
//here when use new QRadioButton("aa"); it works right
}
}

void MainWindow::on_listView_clicked(const QModelIndex &index) {

}




plist->setIndexWidget (modelcheck->index (i, 0), new QCheckBox("xxx"));

1.everytime I use new does it memleaks ?
2.why setIndexwidget(...) revive a SIGTERM

Santosh Reddy
1st July 2013, 13:03
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)

mars_tl
2nd July 2013, 04:04
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 .





void qabstractitemview::setindexwidget(const qmodelindex &index, qwidget *widget)
{
q_d(qabstractitemview);
if (!d->isindexvalid(index))
return;
if (qwidget *oldwidget = indexwidget(index)) {
d->persistent.remove(oldwidget); //here remove the oldwidget..
d->removeeditor(oldwidget);
oldwidget->deletelater();
}
if (widget) {
widget->setparent(viewport());
d->persistent.insert(widget);
d->addeditor(index, widget, true);
widget->show();
datachanged(index, index); // update the geometry
if (!d->delayedpendinglayout)
widget->setgeometry(visualrect(index));
}
}



I find some code in qt src...