PDA

View Full Version : does removeCellWidget in QTableWidget, will delete the widget?



waiter
13th March 2012, 07:34
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;
}

Spitfire
14th March 2012, 12:13
Adding widget to the table passes ownership of that widget to the table view.
The view will manage life time of that widget from now on.
Calling QTableWidget::removeCellWidget( row, column ) is equal to calling QTableWidget::setCellWidget( row, column, NULL ).
Setting new widget on the cell causes old widget to be deleted.

To summarise - QTableWidget::removeCellWidget() deletes the widget.

As far as I'm aware there's no way of reclaiming ownership of a widget from the table.

Besides, I'm not sure what you're trying to achieve (I coulnd't be bothered to read the code carefuly as it's difficult without [CODE] tags).
If all you need is that cell changes background color or is disabled then use QTableWidgetItem::setBackground() and Qt::ItemFlags.

waiter
15th March 2012, 08:48
I use SDL to render 16x16 or 8x8(....) video,the cell is a QX11EmbedContainer which I embeded a QFrame,the QFrame is a SDL window,(you know SDL couldn't support subwindow!),it is to heavy to recreate the cell,
in all ,thank you for your answer
(I am a chinese colleage student,I am poor at English,sorry)

what should I do if I want to render a video,I want to use opengl in linux and d3d in windows,,???

may be sfml do,,but you know sfml can't support yuv picture format,,,