how can i use mouse event on Qtablewidget



  • Am using a table widget with mouse event , everything is works fine , but coming to edit table am able to modify by using keyboard only ,not by mouse , i want both keyboard and mouse to be work on table widget , but table is transparent for mouse to not make modifies the table .

    am using this logic apart from this is there any else to operate on both

    table->setAttribute(Qt::WA_TransparentForMouseEvents);

    (sorry for my english), any help is appreciated.


  • Moderators

    I'm not sure I understand.
    Do you wan't the edit to be triggered by mouse or not?

    table->setAttribute(Qt::WA_TransparentForMouseEvents);

    This will disable all mouse events for the widget. The events will go to whatever is under it. Is that what you want?

    QTableWidget inherits QAbstractItemView, which has a setEditTriggers() method to control how editing is started - via mouse, keyboard or selection change.
    I don't see what do you need a mouse event for.



  • @Chris-Kawa how can i edit that tablewidget by using mouse and keyboard. am using table->setAttribute(Qt::WA_TransparentForMouseEvents); this logic to use mouse click events on window , by this i can't able to edit the table widget , please help me.

    my cpp file

    #include "notepad.h"
    #include <QMessageBox>
    #include <QTableView>
    #include <QMouseEvent>
    
    Notepad::Notepad() 
    {
    	table = new QTableWidget();
    	 test() ;
    		
    	add_action = new QAction(tr("Add cell"), this);
    	add_action->setIcon(QIcon("add.jpg"));
        Delete_action = new QAction(tr("Delete cell"), this);
    	Delete_action->setIcon(QIcon("delete.jpg"));
        
    	connect(Delete_action, SIGNAL(triggered()), this, SLOT(Delete()));
    	connect(add_action, SIGNAL(triggered()), this, SLOT(add()));
      	
    	//tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);
    	
    	centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
    	centralWidget()->setAttribute(Qt::WA_MouseTracking,true);
    	
    	setMouseTracking(true);
    	
    }
    void Notepad::test() 
    {	    
    	
    	QTableWidgetItem* tableItem = new QTableWidgetItem();
    	
    	table->setRowCount(1);
    	table->setColumnCount(3);
    	table->setItem(0,0,new QTableWidgetItem());
    	table->setItem(0,1,new QTableWidgetItem());
    	table->setItem(0,2,new QTableWidgetItem());
    
    	table->setMouseTracking(true);
        table->viewport()->setMouseTracking(true);
        table->installEventFilter(this);
        table->viewport()->installEventFilter(this);
    
    	table->setSelectionBehavior(QAbstractItemView::SelectRows);
        table->setSelectionMode(QAbstractItemView::SingleSelection);
    	//table->setSelectionMode( QAbstractItemView::ExtendedSelection );
    	//table->setAttribute(Qt::WA_TransparentForMouseEvents);
    
    	
    
    	table->setHorizontalHeaderItem(0, new QTableWidgetItem("combobox"));
    	table->setHorizontalHeaderItem(1, new QTableWidgetItem("spinbox"));
    	table->setHorizontalHeaderItem(2, new QTableWidgetItem("lineEdit"));
    	tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);
    	table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    	setCentralWidget(table);
    	
    }
    
    void Notepad::mouseReleaseEvent (QMouseEvent * event )
    {	
    	QMessageBox* msgBox;
    	if(event->button() == Qt::RightButton)
    	  {
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
           QMenu *menu = new QMenu(this);
           menu->addAction(add_action);
           menu->addAction(Delete_action);
           menu->exec(mouseEvent->globalPos());
    	   //msgBox->setInformativeText("u pressed right button");   	  	  
    	} 
    }
    void Notepad::add() 
    {
    					
    	table->insertRow( 1);
    	
    	setCentralWidget(table);
    	centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
    	setMouseTracking(true);
    }
    void Notepad::Delete() 
    {
    	
    	table->removeRow(1);
    
    	setCentralWidget(table);
    	centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
        setMouseTracking(true);
    

    }



  • What happens if you don't set the setAttribute(Qt::WA_TransparentForMouseEvents)?
    What problems do you get then?



  • @Jan-Willem if i didn't set the table->setAttribute(Qt::WA_TransparentForMouseEvents); this , am able to edit the tablewidget in a requried cell where i select that cell from table by using mouse but i cant able to do mouse click events .
    but if i set this table->setAttribute(Qt::WA_TransparentForMouseEvents); logic am able to perform clicking event but not able to edit or select a particular cell in table widget. please help me to get out from this.



  • Since you want to add or delete a cell in your table, it seems to me you should do this through a context menu of the QTableWidget and not through the mainwindow. Then you also don't have to meddle with setAttribute(Qt::WA_TransparentForMouseEvents).



  • @Jan-Willem Great! It's work fine! Thank you very much. Can't believe that there is people who spend their time for such noob as me. Thank you again



  • Glad it works. So this part is solved then.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.