April 20, 2012

Octani Octani
Lab Rat
22 posts

[SOLVED] QTableView - Resize of several columns

 

Hello,

i would like to know if it exists any way to change the width of several columns at the same time. I mean that the user can select several columns and when he resizes one of this, it resizes other columns selected. I don’t know if i am understandable.

10 replies

April 20, 2012

d2uriel d2uriel
Lab Rat
68 posts

You are understandable – you are talking about something that exists in MS Excel for example, right? AFAIK there’s no such option available straight out of the box. But implementing one shouldn’t be too much of a hassle. Just derive from QTableView and create your own class where you can implement that option.

Edit: typos.

 Signature 

“Do not judge, and you will never be mistaken.” - Jean-Jacques Rousseau

April 20, 2012

Octani Octani
Lab Rat
22 posts

Yes, exactly the same option exists in MS Excel. The problem is I don’t find any signal called when a column is resized but only a slot :

  1. void QTableView::columnResized ( int column, int oldWidth, int newWidth ) [protected slot]

So I don’t understand how exploit such slot in order to realize what i mean.

April 20, 2012

felipe.c.sousa felipe.c.sou..
Lab Rat
37 posts

Its not what you want, but using that you resize the columns.

  1. ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

 Signature 

From all, to all.

April 20, 2012

d2uriel d2uriel
Lab Rat
68 posts

There is a signal emitted after resizing the columns:

  1. void QHeaderView::sectionResized ( int logicalIndex, int oldSize, int newSize ) [signal]

You can connect to that one and create a slot where you will resize all selected columns accordingly.

 Signature 

“Do not judge, and you will never be mistaken.” - Jean-Jacques Rousseau

April 20, 2012

Octani Octani
Lab Rat
22 posts

Perfect, it works now but the user must select all the column not only some rows:

the connect :

  1. connect (tableView->horizontalHeader(), SIGNAL(sectionResized(int,int,int)), this, SLOT(onColumnResized(int, int, int)));

the slot :

  1. void MyClass::onColumnResized (int logicalIndex, int oldSize, int newSize)
  2. {
  3.     unsigned int d;
  4.  
  5.     if( sender() == tableView->horizontalHeader())
  6.     {
  7.         int nbColSelected = tableView->selectionModel()->selectedColumns(0).size();
  8.         int size  = tableView->columnWidth(tableView->horizontalHeader()->visualIndex(logicalIndex));
  9.        
  10. for(d = 0;  d < nbColSelected; ++d)
  11.         {
  12.             tableView->setColumnWidth( tableView->selectionModel()->selectedColumns(0)[d].column(), size );
  13.         }
  14.     }
  15. }

April 20, 2012

d2uriel d2uriel
Lab Rat
68 posts

Umm, do you still have a problem? Cause I can’t really tell from your post if this problem is solved or not ;-).

 Signature 

“Do not judge, and you will never be mistaken.” - Jean-Jacques Rousseau

April 20, 2012

Octani Octani
Lab Rat
22 posts

Hum no it’s perfect :)

Where can I put “SOLVED” my topic? I don’t find.

April 20, 2012

d2uriel d2uriel
Lab Rat
68 posts

Just edit your first post.

 Signature 

“Do not judge, and you will never be mistaken.” - Jean-Jacques Rousseau

April 20, 2012

Octani Octani
Lab Rat
22 posts

Thanks for all d2uriel.

April 20, 2012

d2uriel d2uriel
Lab Rat
68 posts

You are welcome. Glad I could be of help.

 Signature 

“Do not judge, and you will never be mistaken.” - Jean-Jacques Rousseau

 
  ‹‹ QGLViewer subclass: linker says undefined reference to `vtable of QGLViewer’      Writing 3D viewer application using Qt ››

You must log in to post a reply. Not a member yet? Register here!