Results 1 to 3 of 3

Thread: does removeCellWidget in QTableWidget, will delete the widget?

  1. #1
    Join Date
    Mar 2012
    Location
    china
    Posts
    54
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Exclamation does removeCellWidget in QTableWidget, will delete the widget?

    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;
    }

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: does removeCellWidget in QTableWidget, will delete the widget?

    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.

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

    waiter (15th March 2012)

  4. #3
    Join Date
    Mar 2012
    Location
    china
    Posts
    54
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: does removeCellWidget in QTableWidget, will delete the widget?

    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,,,

Similar Threads

  1. add and delete row dynamically in QTableWidget
    By SIFE in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2011, 07:47
  2. QTableWidget will delete its QMenu twice?
    By liuyanghejerry in forum Qt Programming
    Replies: 2
    Last Post: 17th May 2011, 20:28
  3. Does QTableWidget::clear() delete pointers?
    By Slewman in forum Qt Programming
    Replies: 5
    Last Post: 21st January 2011, 16:09
  4. Replies: 2
    Last Post: 13th May 2009, 10:35
  5. How to delete header on QTableWidget
    By jlbrd in forum Qt Programming
    Replies: 2
    Last Post: 18th July 2006, 21:00

Tags for this Thread

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.