1 Divide the columns evenly
Tablewidget->horizontalheader ()->setstretchlastsection (true); This is the place.
Tablewidget->horizontalheader ()->setresizemode (Qheaderview::stretch);
2 Entire row selected:
Tablewidget->setselectionbehavior (qabstractitemview::selectrows); Set the selection behavior to the behavior unit
Tablewidget->setselectionmode (qabstractitemview::singleselection); Set selection mode, select Single line
3 Delete The entire row selected
int rowIndex = M_pattrbutelist->currentrow ();
if (rowIndex! =-1)
Tablewidget->removerow (RowIndex);
To make the data more visible in the interface, where the Tablewidget control is applied, the code for deleting the specified row in the control is described below:
There is a removerow (int i) function in qtable to delete the current row, but if we do not have any selection for the table, then how is Qtable returned?
Use QT Creator to drag a tablewidget, add a button, click Delete to execute
int i = Ui->tablewidget->currentrow ();
Qmessagebox msg;
Msg.settext (Qstring::number (i));
Msg.exec ();
Ui->tablewidget->removerow (i);
If you do not select a row, you will see that the first row is deleted after each click, and the output of I is 0.
The reason is simply because the table's current focus is caused by the first cell in the first, and the solution is to let the table lose focus and add Ui->tablewidget->setfocuspolicy to the constructor (Qt:: Nofocus);
Then try again, find the Click Delete button if the line is not selected can not be deleted, the returned I at this time is-1.
The control starts with a serial number on the left, and if you want to remove the line number from the left, add the following code.
qheaderview* Headerview = name of Table->verticalheader ();
Headerview->sethidden (TRUE);
4 Adding an entire row
int rowIndex = M_pattrbutelist->rowcount ();
Tablewidget->setrowcount (RowIndex + 1);//total number of rows increased by 1
Tablewidget->setrowheight (RowIndex, 24);//Set the height of the row
--------------------------------------------------------------------------------
Qtablewidget *table = new Qtablewidget (this);
Table->setcolumncount (5); Set Number of columns
Table->setrowcount (3); Set number of lines/
/* Set Column name */
Qstringlist headers;
headers<< "column name 1" << "column Name 2" << "column name 3";
Table->sethorizontalheaderlabels (headers);
/* Add content to cell */
void additemcontent (int row, int column, QString content)
{
Qtablewidgetitem *item = new Qtablewidgetitem (content);
Table->setitem (row, column, item);
}
/* Add an icon to the cell */
Qtablewidgetitem *item = new Qtablewidgetitem (Qicon ("myimage.jpg"), NULL); Add only icons, no strings
Qtablewidgetitem *item = new Qtablewidgetitem (Qicon ("myimage.jpg"), myString); adding icons and strings
Table->setitem (row, column, item);
/* Insert a line */
int row = Table->rowcount ();
Table->insertrow (row);
/* Insert a column */
int column = Table->columncount ();
Table->insertcolumn (column);
Make the row and column head adaptive to the width, and the last one will fill in the empty part
Table->horizontalheader ()->setstretchlastsection (true);
Make the row and column head adaptive width, all columns evenly divided to fill the blank part
Table->horizontalheader ()->setresizemode (Qheaderview::strtch);
Make the row adaptive height, if the line is many, the height of the row will not always decrease, when a certain value will be automatically generated a Qscrollbar
Table->verticalheader ()->setresizemode (Qheaderview::strtch);
Set Click to select a row
Table->setselectionbehuavior (qabstractitemview::selectrows);
Set the contents of each line not editable
Table->setedittriggers (qabstractitemview::noedittriggers);
Set to select only one row, cannot select multiple rows
Table->setselectionmode (qabstractitemview::singleselection);
/* Remove the line number per line */
Qheaderview *headerview = Table->verticalheader ();
Headerview->sethidden (TRUE);
/* Set to have a cell or a row selected */
Selected cells: Table->setcurrentcell (row, column, qitemselectionmodel::select);
Select one line: Table->setcurrentcell (Row, qitemselectionmodel::select);(Note that the column here has no value)
Qtablewidget Row Check/delete/Add row