Solve the problem that QTableWidget does not display data

QTableWidget is usually used for data display. Through its table layout, users can see the data more clearly and filter the data more intuitively.

However, beginners and careless people will always find that their data has been added normally, but after the program runs, they can not see any data on the QTableWidget, a blank.

What's going on? Let's look at a code that can reproduce this problem:

 1 func main() {
 2     widgets.NewQApplication(len(os.Args), os.Args)
 3 
 4     table := widgets.NewQTableWidget(nil)
 5     table.SetColumnCount(3)
 6     table.SetHorizontalHeaderLabels([]string{"number", "Full name", "Age"})
 7     // Remove borders
 8     table.SetShowGrid(false)
 9 
10     // Setting data
11     num1 := widgets.NewQTableWidgetItem2("0", 0)
12     name1 := widgets.NewQTableWidgetItem2("anmi", 0)
13     age1 := widgets.NewQTableWidgetItem2("20", 0)
14     table.SetItem(0, 0, num1)
15     table.SetItem(0, 1, name1)
16     table.SetItem(0, 2, age1)
17 
18     num2 := widgets.NewQTableWidgetItem2("1", 0)
19     name2 := widgets.NewQTableWidgetItem2("terra", 0)
20     age2 := widgets.NewQTableWidgetItem2("24", 0)
21     table.SetItem(1, 0, num2)
22     table.SetItem(1, 1, name2)
23     table.SetItem(1, 2, age2)
24 
25     table.SetWindowTitle("QTableWidget")
26     table.Show()
27 
28     widgets.QApplication_Exec()
29 }
Code that causes data not to be displayed

This is its effect:

Yes, the meter displays normally, but the data is missing!

Let's take a look at the repaired Code:

 1 func main() {
 2     widgets.NewQApplication(len(os.Args), os.Args)
 3 
 4     table := widgets.NewQTableWidget(nil)
 5     table.SetColumnCount(3)
 6     table.SetRowCount(2)
 7     table.SetHorizontalHeaderLabels([]string{"number", "Full name", "Age"})
 8     // Remove borders
 9     table.SetShowGrid(false)
10 
11     // Setting data
12     num1 := widgets.NewQTableWidgetItem2("0", 0)
13     name1 := widgets.NewQTableWidgetItem2("anmi", 0)
14     age1 := widgets.NewQTableWidgetItem2("20", 0)
15     table.SetItem(0, 0, num1)
16     table.SetItem(0, 1, name1)
17     table.SetItem(0, 2, age1)
18 
19     num2 := widgets.NewQTableWidgetItem2("1", 0)
20     name2 := widgets.NewQTableWidgetItem2("terra", 0)
21     age2 := widgets.NewQTableWidgetItem2("24", 0)
22     table.SetItem(1, 0, num2)
23     table.SetItem(1, 1, name2)
24     table.SetItem(1, 2, age2)
25 
26     table.SetWindowTitle("QTableWidget")
27     table.Show()
28 
29     widgets.QApplication_Exec()
30 }
Correct code

Display effect:

In fact, the problem is very simple. Looking at the sixth line of the code, we set the number of lines.

QTableWidget needs to set how many rows of data in total can be displayed normally. If it is not set, the default is 0 rows of data, that is, nothing is displayed.

So people who need QTableWidget must not forget to use SetRowCount to tell widget how much data needs to be drawn.

Keywords: C++

Added by Jax2 on Sat, 28 Dec 2019 16:48:51 +0200