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. Switching QWidgets using QTreeView
Forum Update on Monday, May 27th 2025

Switching QWidgets using QTreeView

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.4k 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.
  • J Offline
    J Offline
    jazzyeagle
    wrote on 13 May 2011, 02:15 last edited by
    #1

    Hello -

    I'm trying to create a program where there is a QTreeView on the left side and a panel (QWidget) on the right hand side, and the QWidget is switched depending on the QTreeView. For those of you who use Qt Creator, think of it like clicking on the file name on the left (but single-click instead of double-click) and then the appropriate QWidget opens on the right.

    The issue I'm having is that my program is unexpectedly closing what appears to be randomly as I jump around the tree on the left. The program exits with Code 0, and there are no errors listed anywhere that I can, so I'm having a hard time figuring out what exactly is wrong, but I was able to step through to the point that I determined exactly at what line the code is exiting at, but what I can't figure out is why.

    My original problem was that I was about to start implementing SIGNALs and SLOTs for the QWidgets and realized I forgot to add Q_OBJECT on all of them. I added it, had issues with compilation and found on the forum that I needed to delete the build directory so I would get a clean compilation. Once I did this, I got the compilation, but I also had this issue. It is easiest to reimplement this problem by using the arrow keys, going down the tree and then trying to go back up.

    I'm going to post four classes below in hopes that someone can help me figure out what I'm doing wrong (headers not included, but I can post if you think it necessary). Due to the size, I posted it to "pastebin.":http://pastebin.com/g3uUsVnu

    I'm sorry it's so much, but not being sure what exactly is causing the issue, I wasn't sure what I needed to post. I did leave in my debugging QMessageBox's and whatnot. Being relatively new to Qt, I wouldn't doubt that there's a better way to do what I'm trying to do and am open to suggestions on how to revamp the code. System info: Using Qt Creator 4.7.0 64-bit on ArchLinux using the 2.6.38 kernel w/ Gnome 2.32.1, although I did have the same issue on Windows 7 as well.

    Any help that can be provided would be GREATLY appreciated. Thanks!!

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on 13 May 2011, 07:41 last edited by
      #2

      I would create all pages on creation of the containing widget, then associate the page with a certain tree item using some userdata object. If you then use a stacked widget, showing the widget is trivial. Have a look at "the QtCreator settings dialog code":http://qt.gitorious.org/qt-creator/qt-creator/blobs/v1.2.0/src/plugins/coreplugin/dialogs/settingsdialog.cpp for an example implementation. AFAIK the linked 1.2.0 version does what you want to achieve.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jazzyeagle
        wrote on 13 May 2011, 11:11 last edited by
        #3

        Ok, I tried looking at the code, and it seems way more complex than what I wrote, and there are little to no comments throughout to help me understand what exactly they're doing. I'm going to keep looking into it, though, to see if I can't figure it out and get some light bulbs to go off in my head.

        I'm also not sure what you were talking about with AFAIK. I tried to Google it and came up with a bunch of forum posts about audio stuff, and couldn't really find a page that specifically talked about what AFAIK truly is. If you could please help me to understand this, I would greatly appreciate it.

        Also, if anyone has other suggestions, I would love to hear those as well. I'm hoping there's a much simpler answer without requiring me to replace 90% of my code with code that I don't fully understand. I had enough of that with the QAbstractItemModel

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on 13 May 2011, 12:43 last edited by
          #4

          [quote author="jazzyeagle" date="1305285111"]Ok, I tried looking at the code, and it seems way more complex than what I wrote, and there are little to no comments throughout to help me understand what exactly they're doing. I'm going to keep looking into it, though, to see if I can't figure it out and get some light bulbs to go off in my head.[/quote]

          The answer is rather simple. They set up a tree, and each item has a bit of user data that associates each tree item with a widget that is stored in a stacked widget. If an item is selected, the user data is checked, the stacked widget is told to display the associated widget. It is more or less what you do, but the difference lies in the fact that your code has the knowledge 1. hard coded, 2. probably available on multiple places. The QtCreator code only has the knowledge on one location; being where the tree is set up.

          [quote author="jazzyeagle" date="1305285111"]I'm also not sure what you were talking about with AFAIK. [/quote]
          http://www.urbandictionary.com/define.php?term=afaik

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0

          1/4

          13 May 2011, 02:15

          • Login

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