[Qt-interest] problem with signal slot in QTableWidget

Riccardo Roasio riccardo.roasio at gmail.com
Fri Jun 17 12:14:33 CEST 2011


HI,

i have a class derived from QTableWidget and i would like to catch
when the user click on a cell.

I'm trying to use:

connect(this, SIGNAL(cellClicked(int,int)), this,
SLOT(SetCurrentCell(int,int)));

but it seems not to work.

Should be because i'm also catching mousePressEvent ?

This is the code:


#include "customtablewidget.h"

CustomTableWidget::CustomTableWidget(QString t,Configuration *c)
{
    activeConfiguration=c;
    type=t;

    connect(this, SIGNAL(cellClicked(int,int)), this,
SLOT(SetCurrentCell(int,int)));
}

void CustomTableWidget::mousePressEvent( QMouseEvent * event )
{
    if(event->button()==Qt::RightButton)
    {
        QMenu *tmpMenu=new QMenu(this);

        QAction* newItemAction = new QAction(tr("&Add New"),this);

        connect(newItemAction, SIGNAL(triggered()), this, SLOT(AddNewItem()));

        tmpMenu->addAction(newItemAction);
        tmpMenu->exec(event->globalPos());
    }
    else
    {
        QWidget::mousePressEvent( event );
    }


}

void CustomTableWidget::SetCurrentCell(int r,int c)
{
    int currentRow=r;
    int currentColumn=c;
}

void CustomTableWidget::AddNewItem()
{
    if(type=="INPUT")
    {
        InputItem *tmpInputItem=new InputItem(this);
        tmpInputItem->SetConfiguration(activeConfiguration);

        int maxRows=this->rowCount();
        int maxCols=this->columnCount();
        if(maxCols==0)
        {
            setColumnCount(1);
        }
        this->setRowCount(maxRows+1);
        this->setCellWidget(maxRows,0,tmpInputItem);
        this->scrollToBottom();
    }
    else if(type=="OUTPUT")
    {
        OutputItem *tmpOutputItem=new OutputItem(this);
        tmpOutputItem->SetConfiguration(activeConfiguration);

        int maxRows=this->rowCount();
        int maxCols=this->columnCount();
        if(maxCols==0)
        {
            setColumnCount(1);
        }
        this->setRowCount(maxRows+1);
        this->setCellWidget(maxRows,0,tmpOutputItem);
        this->scrollToBottom();
    }
}


Thanks,
Riccardo



More information about the Qt-interest-old mailing list