Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how can i use mouse event on Qtablewidget
QtWS25 Last Chance

how can i use mouse event on Qtablewidget

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetqmouseevent
8 Posts 3 Posters 6.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    srikanth
    wrote on 26 Aug 2016, 12:31 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 26 Aug 2016, 20:44 last edited by
      #2

      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.

      S 1 Reply Last reply 29 Aug 2016, 04:54
      1
      • C Chris Kawa
        26 Aug 2016, 20:44

        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.

        S Offline
        S Offline
        srikanth
        wrote on 29 Aug 2016, 04:54 last edited by
        #3

        @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);
        

        }

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jan-Willem
          wrote on 29 Aug 2016, 07:26 last edited by
          #4

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

          S 1 Reply Last reply 29 Aug 2016, 09:07
          1
          • J Jan-Willem
            29 Aug 2016, 07:26

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

            S Offline
            S Offline
            srikanth
            wrote on 29 Aug 2016, 09:07 last edited by srikanth
            #5

            @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.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jan-Willem
              wrote on 29 Aug 2016, 10:23 last edited by
              #6

              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).

              S 1 Reply Last reply 29 Aug 2016, 10:50
              1
              • J Jan-Willem
                29 Aug 2016, 10:23

                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).

                S Offline
                S Offline
                srikanth
                wrote on 29 Aug 2016, 10:50 last edited by
                #7

                @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

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jan-Willem
                  wrote on 29 Aug 2016, 10:51 last edited by
                  #8

                  Glad it works. So this part is solved then.

                  1 Reply Last reply
                  1

                  4/8

                  29 Aug 2016, 07:26

                  • Login

                  • Login or register to search.
                  4 out of 8
                  • First post
                    4/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved