Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subclassing QWidget or QTreeView - which one to choose?
QtWS25 Last Chance

Subclassing QWidget or QTreeView - which one to choose?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 526 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fuchsiaff
    wrote on last edited by
    #1

    I want to make a directory tab.

    If i subclass QWidget and make an object called view in directory.h file:
    QTreeView *view = new QTreeView;
    and add that to a layout in another file where the class i made is Directory *dir_view = new Directory;
    like this: tab_layout->addWidget(dir_view->view);
    Then everything works like it should, I get this nice tree view in my mainwindow

    Now comes the problem, since I have to capture keypressevents when user is focused on QTreeView, then I need to subclass QTreeView instead of QWidget

    I changed my directory class to subclass QTreeView, instead of QWidget and changed all other stuff accordingly, but now tab_layout->addWidget(dir_view); is not displaying any widget.
    when i do dir_view.show() after that statement then a window appears and dissapears instantly.

    What could be the cause of this?

    JonBJ 1 Reply Last reply
    0
    • F Fuchsiaff

      I want to make a directory tab.

      If i subclass QWidget and make an object called view in directory.h file:
      QTreeView *view = new QTreeView;
      and add that to a layout in another file where the class i made is Directory *dir_view = new Directory;
      like this: tab_layout->addWidget(dir_view->view);
      Then everything works like it should, I get this nice tree view in my mainwindow

      Now comes the problem, since I have to capture keypressevents when user is focused on QTreeView, then I need to subclass QTreeView instead of QWidget

      I changed my directory class to subclass QTreeView, instead of QWidget and changed all other stuff accordingly, but now tab_layout->addWidget(dir_view); is not displaying any widget.
      when i do dir_view.show() after that statement then a window appears and dissapears instantly.

      What could be the cause of this?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Fuchsiaff
      Sub-classing QWidget in itself is not going to get you anywhere. If you need to sub-class, it will be QTreeView you will need to do it on. I think we would need to see what you did for that before understanding how it does not display or appears & disappears.

      BTW, what is your "directory tab"/tab_layout? A QBoxLayout? A QStackedLayout? A QTabWidget? A QStackedWidget ... ?

      1 Reply Last reply
      1
      • F Offline
        F Offline
        Fuchsiaff
        wrote on last edited by
        #3

        Alright so:

        I have a Tabs class that handles showing directory and adding a new tab to the layout:
        https://github.com/Fuchsiaff/PyPad_2/blob/master/Sources/tabs.cpp

        Directory tab is just a QWidget in that code, and that works but the way I need to use it (capture keypressevents when user selects a file in the directory tab and presses a certain key) then i need to make Directory a QTreeView

        I hope this is enough information!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          If only key press events en depending on what you want to do with them, an event filter might be enough.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved