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;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
void init();
QList<QRadioButton*> lvradio;
QList<QCheckBox*> lvcheck;
};
#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;
};
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
init ();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init() {
plist = ui->listView;
for (int i = 0; i < 10; ++i) {
lvradio.push_back (p);
}
for (int i = 0; i < 11; ++i) {
}
}
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
) {
}
#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) {
}
To copy to clipboard, switch view to plain text mode
plist
->setIndexWidget
(modelcheck
->index
(i,
0),
new QCheckBox("xxx"));
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
Bookmarks