6

I am creating an application that saves the data of an object sending service.

I created that with Qt, a model of type QStandardItemModel that I want to display with QtableView.

But QtableView shows me the line level on the left. I want to delete it or hide it if possible.

I also have a problem with a header that I want to divide into two horizontally then divide the corresponding part of the bottom in two vertically. The reason for these division is that I have two headers with similar beginnings (date of correspondence and correspondence number)

Thank you for your reply because it is really important for me.

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
weird.ly
  • 185
  • 1
  • 7
  • Here's how you can hide the line numbers: `myTableView->verticalHeader()->hide();` But the second part of your question is much harder. I think the only feasible way would be to subclass `QHeaderView` and implement rendering the upper parts of two adjacent columns' headers as they are one singe column header. – Dmitry Sep 28 '17 at 13:00
  • As for my first question your answer works perfectly but the second one is a bit fuzzy for me, I think I will understand better with a visual also I will post mine (if I find the way) because I think the layout that I ' I chose is logical in this order but I am open to all suggestions thank you. – weird.ly Sep 28 '17 at 14:41
  • @eyllanesc How it must look like https://i.imgur.com/ITXNWNc.jpg – weird.ly Sep 28 '17 at 15:21

1 Answers1

9

This type of QHeaderView does not exist, but we can create it for it we must create a class that inherits from QHeaderView and rewrite mainly the method paintSection which is the method in charge of drawing the sections of the QHeaderView.

But to do the generic project for any type of visual design we have to keep the information of the position and size of each section, for this we will create a model, to understand why of the overwritten classes I recommend you read content of the following link.

Explain the logic of each method is extensive so only place the link of the project that implements the above and describe the task of each class:

  • TableHeaderItem: It is responsible for saving the information of each item, mainly rowspan and columnspan in addition to the label.

  • GridTableHeaderModel: Model class that provides access to each item so that we can edit and read each item

  • GridTableHeaderView: This class is the custom QHeaderView where the main methods are overwritten to get the desired look.

  • GridTableView(optional): is a TableView that has methods to work directly with GridTableHeaderView.

Output:

enter image description here

Note: to hide the vertical header it is only necessary to use the hide() method:

horizontalHeader()->hide();
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you very much eyllanesc I tried to compile your code source there were some errors I managed to fix some that was due to files not included and I am currently reading your source code to try to understand much more your approach thanks again – weird.ly Sep 30 '17 at 07:13
  • The result that you get with the screenshot is exactly what I want. the screenshot for error https://i.imgur.com/8DH1h1J.png – weird.ly Sep 30 '17 at 07:22
  • I cloned your Github directory then and tried again but the same problems persist it's probably a due to my version of Qt as you say. A qmake--version from my terminal gives me as a result: QMake version 2.01 A Using Qt version 4.8.7 in/usr/lib/i386-linux-gn – weird.ly Sep 30 '17 at 11:58
  • The first error is: G ++-C-pipe-O2-Wall-W-D_REENTRANT-DQT_DEPRECATED_WARNINGS-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/share/qt4/Mkspecs/Linux-G ++-I.-I/usr/include/qt4/qtcore.pyd-I/usr/include/qt4/QtGui-I/usr/include/qt4-I.-O main. Main .cpp In file included from main .cpp: 1:0: Gridtableheaderview. H:10:95: Error: ' Q_NULLPTR ' was not declared in this scope :: Orientation guidance, int rows, int columns, QWidget * parent = Q_NULLPTR; ^~~~~~~~~ Makefile: 233: The recipe for the target "main. O" failed Make: *** [Main. o] Error 1 – weird.ly Sep 30 '17 at 11:59
  • I just replaced the Q_NULLPTR by 0 and it works now very well – weird.ly Sep 30 '17 at 12:07
  • @eyllanesc It looks like your github link is dead, do you have a copy of this code I could view? I'm trying to make a multilevel header in PyQt5 and your picture is exactly what I want. – pyjamas Feb 13 '19 at 21:25
  • @eyllanesc Great got it now, thanks. This will be very helpful – pyjamas Feb 13 '19 at 21:32
  • 3
    @eyllanesc you mark my quastion as duplicate, but im asqking how to do same thing in python not c++. Im do not understand c++ code. And many peoples who will need same soluthion will googling that question by request with word "python". Many of they do not understand c++ too. Please reopen my question. (https://stackoverflow.com/questions/62089638/double-header-using-qtableview-in-pyqt5?noredirect=1#comment109816496_62089638) – Andrey Topoleov May 29 '20 at 19:10
  • Awesome work. Have you considered making a small library of this an create a repo? I would very much like to post some questions/improvements on this – brahmin Sep 08 '22 at 16:15