Some problems and solutions of qtablewidget form merging

Source: Internet
Author: User

QT provides qtablewidget as a table class to implement the basic functions of the table, and each cell loaded in the table is provided by the class Qtablewidgetitem. This is a basic class provided by the table-based implementation of QT, and if you want to implement custom tables and cell functions, you need to derive overrides, using the QT classic MV structure Qtableview+qabstractitemmode.

Introduction to How to use Qtablewidget+qtablewidgetitem:

Typically when you use this pair of combinations to implement the basic functionality of a table, the usual practice is to:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////

Example code one:

qtablewidget* tablewidget = new Qtablewidget;

Tablewidget->setrowcount (11); Set the number of rows in a table

Tablewidget->setcolumncount (11); Set the number of columns in a table

Set an item that can load data for each column in each row of the table

for (int nrow = 0; nrow < nrow++)


for (int ncolumn = 0; ncolumn < one; ncolumn++);


qtablewidgetitem* item = new Qtablewidgetitem;

Tablewidget->setitem (Nrow, Ncolumn, item);



Accessing cells in a table by index and assigning values

qtablewidgetitem* item = Tablewidget->itemat (3, 3);

Item->settext ("Mytableitem");

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////

Ok! If you're just manipulating the cell in the table, there's no problem with the above code, but if you're merging the cells in the table, then the table will go wrong!

Here are some of the problems and related solutions that I encountered when I implemented the table merging in the development process:

Question one: If you create tables and cells for table merging by following the example code one way

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////

Tablewidget->setspan (0, 0, 1, 11);

qtablewidgetitem* itemget = Tablewidget->itemat (0, 0);

Itemget->settextalignment (Qt::alignhcenter);

Itemget->settext ("mytableitem (0, 0)");

/* ok! The above code is OK, implement the first row of cell merging and setting text */

Itemget = NULL;

Itemget = Tablewidget->itemat (1, 1);

Itemget->settextalignment (Qt::alignhcenter);

Itemget->settext ("Mytableitem (1, 1)");

/* error! Here's the problem, Itemat (1, 1) is still the cell that the Itemat (0, 0) points to. Is the problem caused by the table merging function Tablewidget->setspan (), and the table merging makes the related index functions such as Itemat () problematic. (The specific reason is not clear) */

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////


(1) since the Setspan () function destroys the index of Itemat (), we can access the method by re-establishing the new item as follows:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////

qtablewidgetitem* newitem = new Qtablewidgetitem;

Tablewidget->setitem (1, 1, newitem);

Newitem->settextalignment (Qt::alignhcenter);

Newitem->settext ("Mytableitem (1, 1)");

/* This enables the assignment of cells to other locations in the table.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////

(2) Do not dynamically create qtablewidgetitem for cells when creating qtablewidget, but instead create qtablewidgetitem dynamically when using a cell. (This method is recommended to avoid memory leaks.)

Problem two: The same situation as the problem one is due to the index, this time due to the Selectedrange () index error.

After using Tablewidget to generate the table, you want to merge the cells with the mouse selection, you will find that only the first call to Selectedrange () can correctly return the range of cells selected by the mouse and perform the merge successfully, the second time after the selectedranged () Returns the first cell that is always the mouse selection range.

WORKAROUND: Use the function selectedranges (). This function will return the Qlist object, which contains the position of all the cells selected by the mouse, so that we can implement multiple merges. The code is as follows:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////

Tablewidget->setspan (Tablewidget->selectedranges (). First (). Toprow (),

Tablewidget->selectedranges (). First (). Leftcolumn (),

Tablewidget->selectedranges (). Last (). Bottomrow () –

Tablewidget->selectedranges (). First (). Toprow () + 1,

Tablewidget->selectedranges (). Last (). Rightcolumn ()-

Tablewidget->selectedranges (). First (). Leftcolumn () + 1);



Qt: When merging cells multiple times, be aware that
If a qtableview is merged with Setspan, after the data has been updated, it will continue to merge again using Setspan (mainly to re-merge the entire table, and to re-merge the rows that have already been merged, since the data has been updated), At this time can not directly use Setspan, but to first Qtableview row to revert to the original no merged rows and columns, again use Setspan, otherwise the display is likely to occur error:
if (model! = 0) {
Revert to the Qtableview before the merge
for (int i = 0; i < Model->rowcount (); ++i) {
Ui->tableview->setspan (i, 1, 1, 1);
Ui->tableview->setspan (i, 10, 1, 1);

Merge cells
if (currentrow-firstrow! = 1) {
Ui->tableview->setspan (FirstRow, 1, RowSpan, 1);
Ui->tableview->setspan (FirstRow, ten, RowSpan, 1);


Some problems and solutions of qtablewidget form merging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.