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 get Delegate for QTableWidget?
Forum Update on Monday, May 27th 2025

how can i get Delegate for QTableWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetqmouseevent
30 Posts 4 Posters 15.9k 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.
  • V VRonin
    26 Aug 2016, 09:13

    @mrjj The view does not take ownership of the delegate so you have to give a parent to comDel or it will leak

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 26 Aug 2016, 09:26 last edited by mrjj
    #9

    @VRonin
    Thanks. I thought setItemDelegateForColumn would - so thats a very good point;
    (Docs clearly states it do not so assumption is the mother of all fups :)

    1 Reply Last reply
    1
    • S srikanth
      26 Aug 2016, 09:07

      @mrjj but again same problem raising.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 26 Aug 2016, 09:28 last edited by
      #10

      @srikanth
      Hi
      You changed to use new?
      So what is the exact issue?
      No deletegate is shown, or ?

      S 1 Reply Last reply 26 Aug 2016, 10:42
      0
      • M mrjj
        26 Aug 2016, 09:28

        @srikanth
        Hi
        You changed to use new?
        So what is the exact issue?
        No deletegate is shown, or ?

        S Offline
        S Offline
        srikanth
        wrote on 26 Aug 2016, 10:42 last edited by
        #11

        @mrjj the error is showing like this "Unhandled exception at 0x778e3560 in add or remove colmns.exe:0xC00000FD:Stack overflow. "
        and i make changes like this as you said

        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);
        	
        	QStandardItemModel model((table->rowCount()),(table->columnCount()));
        	//SpinBoxDelegate sdelegate;
        	SpinBoxDelegate *sdelegate = new SpinBoxDelegate ();
        	ComboBoxDelegate * comDel = new ComboBoxDelegate ();
        	//ComboBoxDelegate comDel;
        	 for (int row = 0; row < 4; ++row)
        	{
        		QWidget *parent;
        		QStyleOptionViewItem option;
                for (int column = 0; column < 1; ++column)
        		{
        			
        			table->setItemDelegateForColumn( column, comDel);
        			QModelIndex index = model.index(row, column, QModelIndex());
                    model.setData(index, QVariant((row + 1) * (column + 1)));
                }
        		for (int column = 1; column < 2; ++column) 
        		{
        		
        		table->setItemDelegateForColumn( column, sdelegate  );
        		QModelIndex index = model.index(row, column, QModelIndex());
                model.setData(index, QVariant((row + 1) * (column + 1)));
        		}
        	}
        
        
        	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);
        	
        }
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 26 Aug 2016, 11:11 last edited by
          #12

          Hi
          That is a bug in your code, most likely.

          Please use debugger and single step to find the line
          that gives it.

          Its very hard to guess by looking at code.

          You get a Stack overflow - which often is a infinite loop or something like that.

          S 2 Replies Last reply 29 Aug 2016, 04:59
          1
          • M mrjj
            26 Aug 2016, 11:11

            Hi
            That is a bug in your code, most likely.

            Please use debugger and single step to find the line
            that gives it.

            Its very hard to guess by looking at code.

            You get a Stack overflow - which often is a infinite loop or something like that.

            S Offline
            S Offline
            srikanth
            wrote on 29 Aug 2016, 04:59 last edited by
            #13
            This post is deleted!
            1 Reply Last reply
            0
            • M mrjj
              26 Aug 2016, 11:11

              Hi
              That is a bug in your code, most likely.

              Please use debugger and single step to find the line
              that gives it.

              Its very hard to guess by looking at code.

              You get a Stack overflow - which often is a infinite loop or something like that.

              S Offline
              S Offline
              srikanth
              wrote on 29 Aug 2016, 05:02 last edited by
              #14

              @mrjj 0_1472446858318_Untitled.jpg

              am getting error like this am not able to find where the error is , please help me.

              M 1 Reply Last reply 29 Aug 2016, 06:14
              0
              • S srikanth
                29 Aug 2016, 05:02

                @mrjj 0_1472446858318_Untitled.jpg

                am getting error like this am not able to find where the error is , please help me.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 29 Aug 2016, 06:14 last edited by
                #15

                @srikanth
                Most likely in your delegate.
                You can insert break point in each function and single step when you reach it.
                That way you can find where it crashes.
                No way i can just guess it, sadly.

                if you think it crash in Notepad::test()
                single step should find it.

                S 1 Reply Last reply 29 Aug 2016, 06:58
                0
                • J Offline
                  J Offline
                  Jan-Willem
                  wrote on 29 Aug 2016, 06:52 last edited by
                  #16

                  Just wondering, but it seems you add a columnDelegate for every row you pass through. If I'm not mistaken, you should add a columnDelegate just once for the entire column.

                  And you seem to use a for-statement for setting the columDelegate, while you only execute it once. That doesn't make sense to me.

                  I'm not sure if this would fix anything of your problem, but I think you're code would benefit from it if you if you fixed this.

                  S 1 Reply Last reply 29 Aug 2016, 07:07
                  2
                  • M mrjj
                    29 Aug 2016, 06:14

                    @srikanth
                    Most likely in your delegate.
                    You can insert break point in each function and single step when you reach it.
                    That way you can find where it crashes.
                    No way i can just guess it, sadly.

                    if you think it crash in Notepad::test()
                    single step should find it.

                    S Offline
                    S Offline
                    srikanth
                    wrote on 29 Aug 2016, 06:58 last edited by
                    #17

                    @mrjj i did debug by using break points, everything is working fine , in .cpp file up to return app.exec(); in main.cpp , here it is getting crash and showing this error.

                    M 1 Reply Last reply 29 Aug 2016, 07:08
                    1
                    • J Jan-Willem
                      29 Aug 2016, 06:52

                      Just wondering, but it seems you add a columnDelegate for every row you pass through. If I'm not mistaken, you should add a columnDelegate just once for the entire column.

                      And you seem to use a for-statement for setting the columDelegate, while you only execute it once. That doesn't make sense to me.

                      I'm not sure if this would fix anything of your problem, but I think you're code would benefit from it if you if you fixed this.

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

                      @Jan-Willem thanks a lot the error is fix, when i put for loop in a comment , now how can i assign ComboBoxDelegate for 1st column and SpinBoxDelegate to 2nd cloumn.

                      1 Reply Last reply
                      0
                      • S srikanth
                        29 Aug 2016, 06:58

                        @mrjj i did debug by using break points, everything is working fine , in .cpp file up to return app.exec(); in main.cpp , here it is getting crash and showing this error.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 29 Aug 2016, 07:08 last edited by
                        #19

                        @srikanth
                        hi
                        Sounds like you crash when close down then/cleaning up.
                        I think
                        @Jan-Willem has a point of you setting multiple times.
                        setItemDelegateForColumn( column, comDel);
                        It might delete it multiple times.
                        So please try set only once. (for test)

                        J 1 Reply Last reply 29 Aug 2016, 07:17
                        2
                        • M mrjj
                          29 Aug 2016, 07:08

                          @srikanth
                          hi
                          Sounds like you crash when close down then/cleaning up.
                          I think
                          @Jan-Willem has a point of you setting multiple times.
                          setItemDelegateForColumn( column, comDel);
                          It might delete it multiple times.
                          So please try set only once. (for test)

                          J Offline
                          J Offline
                          Jan-Willem
                          wrote on 29 Aug 2016, 07:17 last edited by
                          #20

                          In addidtion to @mrjj, I would suggest before or after the entire

                          for (int row = 0; row < 4; ++row)
                          
                          S 1 Reply Last reply 29 Aug 2016, 09:10
                          1
                          • J Jan-Willem
                            29 Aug 2016, 07:17

                            In addidtion to @mrjj, I would suggest before or after the entire

                            for (int row = 0; row < 4; ++row)
                            
                            S Offline
                            S Offline
                            srikanth
                            wrote on 29 Aug 2016, 09:10 last edited by srikanth
                            #21

                            @Jan-Willem now i get clear idea this error is coming when am trying to insert a delegate into column , so i want to know is there any thing wrong to insert delegates into the tablewidget, please help me to get out from this task . @mrjj

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Jan-Willem
                              wrote on 29 Aug 2016, 09:49 last edited by
                              #22

                              How does your code look like now, something like this?

                              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);
                              	
                              	QStandardItemModel model((table->rowCount()),(table->columnCount()));
                              	SpinBoxDelegate *sdelegate = new SpinBoxDelegate ();
                              	ComboBoxDelegate * comDel = new ComboBoxDelegate ();
                              
                              	 for (int row = 0; row < 4; ++row) {
                                             for (int col = 0; col < 2; ++col) {
                                                   QModelIndex index = model.index(row, col, QModelIndex());
                                                   model.setData(index, QVariant((row + 1) * (col + 1)));
                              	}
                                      table->setItemDelegateForColumn(0,  comDel);
                                      table->setItemDelegateForColumn(1, sdelegate);
                              
                              	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);
                              }
                              
                              S 1 Reply Last reply 29 Aug 2016, 10:02
                              2
                              • J Jan-Willem
                                29 Aug 2016, 09:49

                                How does your code look like now, something like this?

                                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);
                                	
                                	QStandardItemModel model((table->rowCount()),(table->columnCount()));
                                	SpinBoxDelegate *sdelegate = new SpinBoxDelegate ();
                                	ComboBoxDelegate * comDel = new ComboBoxDelegate ();
                                
                                	 for (int row = 0; row < 4; ++row) {
                                               for (int col = 0; col < 2; ++col) {
                                                     QModelIndex index = model.index(row, col, QModelIndex());
                                                     model.setData(index, QVariant((row + 1) * (col + 1)));
                                	}
                                        table->setItemDelegateForColumn(0,  comDel);
                                        table->setItemDelegateForColumn(1, sdelegate);
                                
                                	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);
                                }
                                
                                S Offline
                                S Offline
                                srikanth
                                wrote on 29 Aug 2016, 10:02 last edited by
                                #23

                                @Jan-Willem yeah my code is like this only, but delegates are not assigning to the columns of table widget?

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

                                  The comDel is assigned to the entire column at index 0 with table->setItemDelegateForColumn(0, comDel).

                                  S 1 Reply Last reply 29 Aug 2016, 10:38
                                  0
                                  • J Jan-Willem
                                    29 Aug 2016, 10:27

                                    The comDel is assigned to the entire column at index 0 with table->setItemDelegateForColumn(0, comDel).

                                    S Offline
                                    S Offline
                                    srikanth
                                    wrote on 29 Aug 2016, 10:38 last edited by
                                    #25

                                    @Jan-Willem but that delegate is not assigned to columns of table widget .

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

                                      Just for my curiosity?
                                      Which version of Qt are you using?

                                      S 1 Reply Last reply 29 Aug 2016, 10:57
                                      0
                                      • J Jan-Willem
                                        29 Aug 2016, 10:49

                                        Just for my curiosity?
                                        Which version of Qt are you using?

                                        S Offline
                                        S Offline
                                        srikanth
                                        wrote on 29 Aug 2016, 10:57 last edited by
                                        #27

                                        @Jan-Willem qt 4.8.3 on Visual Studio 2010

                                        and my code looks like this

                                        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);
                                        	
                                        	QStandardItemModel model((table->rowCount()),(table->columnCount()));
                                        	//SpinBoxDelegate sdelegate;
                                        	//ComboBoxDelegate comDel;
                                        	int newrow =table->rowCount();
                                        	int newcol =table->columnCount();
                                        
                                        
                                        	SpinBoxDelegate *sdelegate = new SpinBoxDelegate ();
                                        	ComboBoxDelegate * comDel = new ComboBoxDelegate ();
                                        
                                        	 for (int row = 0; row < 1; ++row) {
                                                       for (int col = 0; col < 3; ++col) {
                                        				 
                                                             QModelIndex index = model.index(row, col, QModelIndex());
                                                             model.setData(index, QVariant((row + 1) * (col + 1)));
                                        	table->setItemDelegateForColumn( newcol, comDel);
                                        			   }
                                          
                                        	 }
                                        	/*for (int row = 0; row < 4; ++row)
                                        	{
                                        table->setItemDelegateForColumn( 2, &sdelegate);
                                        	}*/
                                        
                                             	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);
                                        }
                                        
                                        1 Reply Last reply
                                        0
                                        • J Offline
                                          J Offline
                                          Jan-Willem
                                          wrote on 29 Aug 2016, 12:29 last edited by
                                          #28

                                          Alright. Frankly I think you are making things more difficult than they have to be.

                                          First of all you shouldn't set the columndelegate inside the for-statement, since you set it multiple times for no reason. Once will do for the entire column, so do it before or after the for-statement.

                                          Second, you create a QStandardItemModel and use it with a QTabelWidget. A QTableWidget has it's own internal model.
                                          The QModelIndex returned by model.index(row, col, QModelIndex()) is not in your table, but in your QStandardItemModel.

                                          Perhaps you should read this first: Model/View Programming

                                          S 1 Reply Last reply 30 Aug 2016, 05:20
                                          3

                                          18/30

                                          29 Aug 2016, 07:07

                                          • Login

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