I use setCellWidget firstly,and then call removeCellWidget,but I don't want to delete my widget,
please see my code
#include "frame.h"
#include <QDebug>
Frame::Frame(QWidget *parent) :
QFrame(parent),
row_(-1),
col_(-1)
{
}
Frame::~Frame()
{
qDebug()<<"delete frame"<<row_<<col_;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <frame.h>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(2);
ui->tableWidget->setColumnCount(2);
f = new Frame;
f->row_ = 1;
f->col_ = 1;
f->setStyleSheet("background:red");
ui->tableWidget->setCellWidget(1,1,f);
}
void MainWindow::keyPressEvent(QKeyEvent *Ae)
{
if(Ae->key()==Qt::Key_D)
{
ui->tableWidget->removeCellWidget(1,1);
//ui->tableWidget->setRowCount(1);
//ui->tableWidget->setColumnCount(1);
}
if(Ae->key()==Qt::Key_E)
{
ui->tableWidget->removeCellWidget(1,1);
ui->tableWidget->setRowCount(3);
ui->tableWidget->setColumnCount(3);
}
if(Ae->key()==Qt::Key_T)
{
ui->tableWidget->setSpan(1,1,2,2);
}
if(Ae->key()==Qt::Key_M)
{
f->setParent(0);
f->show();
// ui->tableWidget->removeCellWidget(1,1);
}
if(Ae->key()==Qt::Key_G)
{
ui->tableWidget->setCellWidget(1,1,f);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Bookmarks