We help IT Professionals succeed at work.

QT TableWIdget Resizing issue

arjoshi77 asked
Hello. I have written a program which displays a TableWidget. The table widget has 4 columns The first column is a checkbox, the 2nd and 3rd columns are text and the 4th column is again another QTable widget.  My problem is concerned with management of row and column sizes/geometry.
WHen I shoot up my app it shows the UI as shown in the following image.  
current UI
After I manually resize the window and row/ column headers I get to the following image. Note column 4 is not visible.  
 intermediate UI
After I again resize the rows and columns of the 2nd image (above)..I get to the final desired UI layout.  
 final desired UIHow do I programmatically ensure that on running the app. image 3, the desired UI is rendered??
Watch Question

HI i can certainly help you in this. But I prefer to see code.
Can you please send me code of the Gui you are having ai will modify your code to get the desired result. I guess I posted code in your old question where you had problem with signal slot, can you please post code like that. I mean compilable example.
Top Expert 2016

in the wrong grid, the row height doesn't seem to be properly adjusted before the other QTable was inserted into 4th column. so i would suggest, first change the row height, second set the cell contents.



irfan :

Here are the files compressed using winzip. I am using windows.
Please highlight the code that you will add so I understand.

I got it, Thanx for sending me compilable example. I will work on it and will post back the code. I am at work now so you wont be getting the solution untill i get back to home and then will help you, meanwhile can you please let me know thing do you want your window to show up to column 4 when you start the the application?
When you click "Add Drug" you want your row should automatically resize to show another table in Cell "When to Take" but again do you also want the row size again resized automatically when you click "Add When"??



yes to all of your questions.


each time the "AddDrug" button is clicked, it should show all the first 3 columns ( i.e. the checkbox, DrugName and Dosage)...and populate the 4th column with the DrugWhen Table which in turn would have the AddWhen and RemoveWhen buttons. Again, when a "AddWhen" or a "RemoveWHen" button is pressed the row height should change automatically.

In the Object Oriented parlance, Each Drug has a composition relationship with DrugWHen. I.e. Drug is composed of DrugWHen. This is being articulated in the UI. Each new row added using the "Add Row" button, will have its 4th column (in addition to columns 1,2,&3) to be rendered as the DrugWhen UI .


what I mean by DrugWHen is the table being rendered in the 4th column, i.e. the WHenToTake Column.


Each Table is a member variable of a derivative of the QDialog Class. This QDialog derivative is being added to the QTableWidget Cell using (setCellWIdget).. Hope this helps in clarifying the theory behind the code.


cprrection to my above post:: : Not "each Table". The DrugWhenTable is a member variable of a derivative of the QDIalog Class called DrugWHenDialog. This Dialog is added to the DrugTable WIdget ( which is a derivatiove of QTableWidget) using the setCellWidget Method.


I would appreciate if you not just provide me code as a solution but also explain to me as elaborately as possible what you are doing and why just so that I have knowledge about this paticular area of knowledge of QT. So in the future when faced with a similar situation ,, I would know what to do.

Tried to use QTableWidget::resizeColumnsToContent() and QTableWidget::resizeRowsToContent() when populating the model?
Here I am under MacOS, and the application doesn't works correctly - The subform isn't reparented correctly - QT 4.7.4
Sry I am little late, The best way to do what you want is to use delegate, this gives you hell lot of control over the look of your view but i know whole model view of Qt  is kind of little complex.
following is bare minimum code you can change in your function addRow and that should solve the issue. if you further want to make change in size of row or colum you can override sizeHintforRow which is actually virtual function.

void DrugTable::addCustomRow()
	int row = this->rowCount();
	QMessageBox::information(this,"addCustomRow","It really got called");
	QCheckBox *mCheck = new QCheckBox();

	this->setItem(row,1,new QTableWidgetItem("DrugName"));
	this->setItem(row,2,new QTableWidgetItem("Dosage"));	
	DrugWhenDialog* mDrugWhenDialog = new DrugWhenDialog();


//// Following Two line of change will do the job
       // this->resizeRowsToContents();


Open in new window


thanks to both of you.