I'm new with qt5 in python and struggling al lot to get a decent layout in qt5. Below you find my code to create the tabs with a table in it. The code works fine to set the data in each table. But the layout isn't what I want. Can someone help me to get it straight. P.S. I don't have qt designer.
The python code This is what I tried.
import sys
import locale
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QBrush, QColor, QWindow
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
# constructor
def __init__(self, flnm, data):
super(MainWindow, self).__init__()
self.flnm = flnm
self.setWindowTitle('Table View : ' + self.flnm)
self.data = data
self.ptabs = list(self.data.keys())
layout = QVBoxLayout()
self.setLayout(layout)
self.tabs = QTabWidget()
self.tabs.setTabsClosable(True)
self.add_tabs(self.ptabs, self.data)
self.setCentralWidget(self.tabs)
self.setLayout(layout)
# Fill table with data
def set_table(self, headers, row_data, types):
self.table_widget.setRowCount(len(row_data))
self.table_widget.setColumnCount(len(row_data[0]))
if len(row_data) > 0:
self.table_widget.setHorizontalHeaderLabels(headers)
row_index = 0
for row in row_data:
col_index = 0
for value in row:
typ = types.get(col_index, None)
if typ == 'float':
try:
strval = locale.format_string('%.3f', value, grouping=True)
item = QTableWidgetItem(strval)
if value < 0:
item.setForeground(QBrush(QColor('red')))
except:
item = QTableWidgetItem(str(value))
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
elif typ == 'integer':
try:
strval = locale.format_string(f'%.0f', value, grouping=True)
item = QTableWidgetItem(strval)
if value < 0:
item.setForeground(QBrush(QColor('red')))
except:
item = QTableWidgetItem(str(value))
item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
elif typ == 'date_time':
#strval = dt.fromtimestamp(value)
item = QTableWidgetItem(str(value))
elif typ == 'string':
try:
item = QTableWidgetItem(value)
except:
continue
else:
item = QTableWidgetItem(value)
self.table_widget.setItem(row_index, col_index, item)
col_index += 1
row_index += 1
# method for adding the tabs with a table
def add_tabs(self, ptabs, datd):
if len(ptabs) > 0:
for tab in ptabs:
types = datd[tab]['types']
headers = datd[tab]['headers']
row_data = datd[tab]['row_data']
self.tab = QTabWidget()
self.tabs.addTab(self.tab, tab)
self.table_widget = QTableWidget()
self.table_widget.setParent(self.tab)
self.set_table(headers, row_data, types)
self.table_widget.resizeColumnsToContents()
self.table_widget.resizeRowsToContents()
This is what I expect. Full size window (screen) table(s) but then in a tab as shown below.

This is what I get on the moment

The data for testing. file type JSON dictionary
{
"Blad1": {
"types": {
"0": "string",
"1": "float",
"2": "float",
"3": "float",
"4": "float",
"5": "integer"
},
"headers": [
"date",
"open",
"low",
"high",
"close",
"volume"
],
"row_data": [
[
"2015-11-20",
18.18,
18.0,
18.43,
18.35,
38392898
],
[
"2015-11-23",
18.45,
18.215,
18.7,
18.61,
3352514
],
[
"2015-11-24",
18.7,
18.37,
18.8,
18.8,
4871901
],
[
"2015-11-25",
18.85,
18.77,
19.5,
19.45,
4802607
],
[
"2015-11-26",
19.48,
19.41,
19.67,
19.43,
1648481
],
[
"2015-11-27",
19.45,
19.255,
19.79,
19.65,
1428854
],
[
"2015-11-30",
19.74,
19.55,
19.94,
19.88,
5982957
],
[
"2015-12-01",
19.9,
19.6,
19.95,
19.7,
1511970
],
[
"2015-12-02",
19.645,
19.52,
19.89,
19.8,
4623032
],
[
"2015-12-03",
19.75,
19.185,
19.94,
19.35,
1707701
],
[
"2015-12-04",
19.4,
19.28,
19.585,
19.425,
1096498
],
[
"2015-12-07",
19.5,
19.425,
19.85,
19.7,
2559985
],
[
"2015-12-08",
19.75,
19.05,
19.75,
19.185,
975463
],
[
"2015-12-09",
19.4,
19.11,
19.47,
19.235,
696757
],
[
"2015-12-10",
19.26,
19.25,
19.555,
19.31,
726421
],
[
"2015-12-11",
19.375,
19.06,
19.4,
19.35,
699670
],
[
"2015-12-14",
19.35,
19.15,
19.59,
19.19,
536032
],
[
"2015-12-15",
19.33,
19.155,
19.49,
19.155,
542349
],
[
"2015-12-16",
19.24,
19.07,
19.33,
19.19,
549069
],
[
"2015-12-17",
19.275,
19.27,
19.81,
19.7,
760246
],
[
"2015-12-18",
19.65,
19.55,
20.0,
20.0,
1299310
],
[
"2015-12-21",
19.91,
19.82,
20.0,
19.96,
692244
],
[
"2015-12-22",
19.95,
19.69,
20.065,
20.01,
496124
],
[
"2015-12-23",
20.1,
20.09,
20.415,
20.395,
512478
],
[
"2015-12-24",
20.45,
20.245,
20.46,
20.4,
189821
],
[
"2015-12-28",
20.4,
20.2,
20.45,
20.26,
140675
],
[
"2015-12-29",
20.35,
20.275,
20.76,
20.76,
630806
],
[
"2015-12-30",
20.71,
20.57,
20.8,
20.795,
310286
],
[
"2016-01-04",
20.6,
20.56,
21.0,
20.67,
898265
],
[
"2016-01-05",
20.87,
20.55,
20.9,
20.63,
847726
],
[
"2016-01-06",
20.65,
20.03,
20.65,
20.13,
757729
],
[
"2016-01-07",
19.965,
19.425,
19.965,
19.695,
1498763
],
[
"2016-01-08",
19.9,
19.835,
20.2,
19.97,
801778
],
[
"2016-01-11",
19.91,
19.56,
19.91,
19.65,
742877
],
[
"2016-01-12",
19.62,
19.4,
19.795,
19.43,
1050981
],
[
"2016-01-13",
19.53,
19.415,
19.86,
19.425,
643534
],
[
"2016-01-14",
19.3,
19.01,
19.34,
19.13,
755248
],
[
"2016-01-15",
19.16,
18.525,
19.2,
18.55,
1022030
],
[
"2016-01-18",
18.515,
18.08,
18.7,
18.08,
1098012
],
[
"2016-01-19",
18.5,
18.25,
18.75,
18.655,
814737
],
[
"2016-01-20",
18.34,
18.0,
18.49,
18.11,
980031
],
[
"2016-01-21",
18.15,
17.95,
18.36,
18.15,
905925
],
[
"2016-01-22",
18.5,
18.36,
19.0,
18.9,
841434
],
[
"2016-01-25",
19.19,
18.915,
19.25,
19.105,
635352
],
[
"2016-01-26",
18.97,
18.75,
19.37,
19.185,
643071
],
[
"2016-01-27",
19.2,
18.985,
19.42,
19.055,
631634
],
[
"2016-01-28",
18.985,
18.945,
19.36,
19.0,
1105512
],
[
"2016-01-29",
19.185,
18.995,
19.24,
19.24,
593734
],
[
"2016-02-01",
19.44,
19.21,
19.505,
19.22,
614105
],
[
"2016-02-02",
19.22,
18.22,
19.365,
18.25,
973538
],
[
"2016-02-03",
18.35,
17.975,
18.39,
18.0,
996655
],
[
"2016-02-04",
18.31,
18.135,
18.99,
18.93,
1537462
],
[
"2016-02-05",
18.99,
18.555,
18.99,
18.785,
971200
],
[
"2016-02-08",
18.97,
17.31,
18.97,
17.45,
1546073
],
[
"2016-02-09",
17.5,
15.915,
17.75,
16.27,
2801400
],
[
"2016-02-10",
16.41,
16.165,
17.47,
17.085,
1475325
],
[
"2016-02-11",
17.0,
16.35,
17.385,
16.86,
1398721
],
[
"2016-02-12",
15.9,
15.225,
16.69,
16.605,
4202723
],
[
"2016-02-15",
17.06,
17.015,
17.45,
17.26,
1057664
],
[
"2016-02-16",
17.5,
16.805,
17.65,
17.035,
1435767
],
[
"2016-02-17",
16.485,
16.15,
17.14,
16.51,
2701741
],
[
"2016-02-18",
17.095,
16.875,
17.43,
17.095,
2165956
],
[
"2016-02-19",
17.2,
17.01,
17.4,
17.1,
838582
],
[
"2016-02-22",
17.345,
17.265,
17.615,
17.615,
544539
],
[
"2016-02-23",
17.455,
17.41,
18.05,
17.565,
612696
],
[
"2016-02-24",
17.5,
17.08,
17.79,
17.25,
788044
],
[
"2016-02-25",
17.4,
17.28,
18.14,
18.0,
750660
],
[
"2016-02-26",
18.08,
18.055,
18.735,
18.61,
745206
],
[
"2016-02-29",
18.4,
18.03,
18.51,
18.24,
670097
]
]
},
"Blad2": {
"types": {
"0": "string",
"1": "float",
"2": "float",
"3": "float",
"4": "float",
"5": "integer"
},
"headers": [
"date",
"open",
"low",
"high",
"close",
"volume"
],
"row_data": [
[
"2010-01-04",
10.19,
10.19,
10.795,
10.66,
1052090
],
[
"2010-01-05",
10.745,
10.5,
10.76,
10.615,
768369
],
[
"2010-01-06",
10.57,
10.56,
10.86,
10.825,
887358
],
[
"2010-01-07",
10.87,
10.65,
10.87,
10.77,
984580
],
[
"2010-01-08",
10.77,
10.675,
10.87,
10.79,
274790
],
[
"2010-01-11",
10.86,
10.86,
11.095,
10.95,
533742
],
[
"2010-01-12",
11.015,
10.605,
11.04,
10.67,
353301
],
[
"2010-01-13",
10.67,
10.61,
10.855,
10.775,
475021
],
[
"2010-01-14",
10.8,
10.62,
10.805,
10.725,
397186
],
[
"2010-01-15",
10.72,
10.325,
10.75,
10.525,
362661
],
[
"2010-01-18",
10.48,
10.13,
10.595,
10.53,
438216
],
[
"2010-01-19",
10.5,
10.26,
10.52,
10.445,
477906
],
[
"2010-01-20",
10.48,
10.46,
11.0,
10.74,
932673
],
[
"2010-01-21",
10.85,
10.53,
10.94,
10.535,
401719
],
[
"2010-01-22",
10.45,
10.15,
10.52,
10.31,
402507
],
[
"2010-01-25",
10.26,
10.15,
10.485,
10.39,
350598
],
[
"2010-01-26",
10.345,
10.15,
10.39,
10.335,
186321
],
[
"2010-01-27",
10.29,
10.17,
10.43,
10.255,
292840
],
[
"2010-01-28",
10.38,
10.28,
10.53,
10.325,
338669
],
[
"2010-01-29",
10.4,
10.31,
10.61,
10.565,
406514
],
[
"2010-02-01",
10.545,
10.31,
10.67,
10.575,
330978
],
[
"2010-02-02",
10.62,
10.55,
10.84,
10.78,
328143
],
[
"2010-02-03",
10.79,
10.73,
10.89,
10.775,
208177
],
[
"2010-02-04",
10.765,
10.445,
10.9,
10.5,
471353
],
[
"2010-02-05",
10.34,
9.77,
10.35,
9.94,
775423
],
[
"2010-02-08",
10.01,
9.61,
10.1,
9.715,
433498
],
[
"2010-02-09",
9.6,
9.531,
9.987,
9.87,
428356
],
[
"2010-02-10",
9.95,
9.793,
10.0,
9.828,
249427
],
[
"2010-02-11",
9.887,
9.62,
10.0,
9.691,
408412
],
[
"2010-02-12",
9.8,
9.302,
9.847,
9.405,
764981
],
[
"2010-02-15",
9.405,
9.353,
9.585,
9.48,
193093
],
[
"2010-02-16",
9.5,
9.42,
9.6,
9.54,
179712
],
[
"2010-02-17",
9.601,
9.601,
9.86,
9.787,
342145
],
[
"2010-02-18",
9.87,
9.76,
9.882,
9.801,
161145
],
[
"2010-02-19",
9.787,
9.718,
9.95,
9.91,
352534
],
[
"2010-02-22",
9.9,
9.845,
10.0,
9.869,
247318
],
[
"2010-02-23",
9.973,
9.849,
10.0,
9.88,
336112
],
[
"2010-02-24",
9.959,
9.645,
9.959,
9.732,
487428
],
[
"2010-02-25",
9.88,
9.784,
10.19,
10.055,
920198
],
[
"2010-02-26",
10.1,
10.005,
10.22,
10.21,
651660
],
[
"2010-03-01",
10.2,
10.175,
10.38,
10.35,
584601
],
[
"2010-03-02",
10.395,
10.39,
10.685,
10.685,
339538
],
[
"2010-03-03",
10.7,
10.6,
10.89,
10.845,
533918
],
[
"2010-03-04",
10.84,
10.74,
10.95,
10.885,
446942
],
[
"2010-03-05",
10.89,
10.89,
11.1,
11.025,
494925
],
[
"2010-03-08",
11.0,
10.93,
11.1,
10.98,
273210
],
[
"2010-03-09",
10.98,
10.8,
10.98,
10.88,
235355
],
[
"2010-03-10",
10.82,
10.81,
11.05,
11.0,
542717
],
[
"2010-03-11",
10.95,
10.91,
11.6,
11.44,
950116
],
[
"2010-03-12",
11.425,
11.375,
11.59,
11.46,
322906
],
[
"2010-03-15",
11.49,
11.165,
11.49,
11.235,
308413
],
[
"2010-03-16",
11.3,
11.13,
11.35,
11.205,
244175
],
[
"2010-03-17",
11.22,
11.18,
11.395,
11.215,
326796
],
[
"2010-03-18",
11.17,
10.95,
11.25,
11.06,
370445
],
[
"2010-03-19",
11.11,
10.905,
11.15,
10.945,
648604
],
[
"2010-03-22",
10.82,
10.53,
11.03,
11.0,
522460
],
[
"2010-03-23",
11.135,
11.13,
11.545,
11.515,
628970
],
[
"2010-03-24",
11.57,
11.53,
11.715,
11.64,
401246
],
[
"2010-03-25",
11.62,
11.57,
11.94,
11.87,
496576
],
[
"2010-03-26",
11.85,
11.75,
12.0,
11.935,
350853
],
[
"2010-03-29",
12.0,
11.845,
12.1,
11.945,
442058
],
[
"2010-03-30",
11.95,
11.75,
12.0,
11.755,
272615
],
[
"2010-03-31",
11.775,
11.61,
11.875,
11.81,
319240
],
[
"2010-04-01",
11.86,
11.86,
12.22,
12.16,
580117
],
[
"2010-04-06",
12.25,
12.23,
12.68,
12.6,
604284
],
[
"2010-04-07",
12.6,
12.45,
12.695,
12.57,
708197
],
[
"2010-04-08",
12.5,
12.3,
12.54,
12.38,
611246
],
[
"2010-04-09",
12.41,
12.41,
12.525,
12.5,
267080
],
[
"2010-04-12",
12.56,
12.525,
12.615,
12.55,
330393
]
]
}
}